Skip to content

Commit 94100d6

Browse files
committed
fix(handler): Support both utf-8 and utf8 charsets
Closes #120 https://encoding.spec.whatwg.org/#names-and-labels
1 parent 6ba0005 commit 94100d6

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/handler.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -575,11 +575,11 @@ export function createHandler<
575575
// TODO: handle the weight parameter "q"
576576
const [mediaType, ...params] = accept.split(';');
577577
const charset =
578-
params?.find((param) => param.includes('charset=')) || 'charset=utf8'; // utf-8 is assumed when not specified;
578+
params?.find((param) => param.includes('charset=')) || 'charset=utf-8'; // utf-8 is assumed when not specified;
579579

580580
if (
581581
mediaType === 'application/graphql-response+json' &&
582-
charset === 'charset=utf8'
582+
charset === 'charset=utf-8'
583583
) {
584584
acceptedMediaType = 'application/graphql-response+json';
585585
break;
@@ -590,7 +590,7 @@ export function createHandler<
590590
(mediaType === 'application/json' ||
591591
mediaType === 'application/*' ||
592592
mediaType === '*/*') &&
593-
charset === 'charset=utf8'
593+
(charset === 'charset=utf-8' || charset === 'charset=utf8')
594594
) {
595595
acceptedMediaType = 'application/json';
596596
break;

tests/handler.test.ts

+42
Original file line numberDiff line numberDiff line change
@@ -415,3 +415,45 @@ it('should use the default if nothing is returned from the custom request params
415415
]
416416
`);
417417
});
418+
419+
it('should accept both utf-8 and utf8 charsets ', async () => {
420+
const { request } = createTHandler();
421+
422+
await expect(
423+
request(
424+
'GET',
425+
{ query: '{ __typename }' },
426+
{ accept: 'application/json; charset=utf8' },
427+
),
428+
).resolves.toMatchInlineSnapshot(`
429+
[
430+
"{"data":{"__typename":"Query"}}",
431+
{
432+
"headers": {
433+
"content-type": "application/json; charset=utf-8",
434+
},
435+
"status": 200,
436+
"statusText": "OK",
437+
},
438+
]
439+
`);
440+
441+
await expect(
442+
request(
443+
'GET',
444+
{ query: '{ __typename }' },
445+
{ accept: 'application/json; charset=utf-8' },
446+
),
447+
).resolves.toMatchInlineSnapshot(`
448+
[
449+
"{"data":{"__typename":"Query"}}",
450+
{
451+
"headers": {
452+
"content-type": "application/json; charset=utf-8",
453+
},
454+
"status": 200,
455+
"statusText": "OK",
456+
},
457+
]
458+
`);
459+
});

0 commit comments

Comments
 (0)