Skip to content

Commit

Permalink
fix(handler): Support both utf-8 and utf8 charsets
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Apr 10, 2024
1 parent 6ba0005 commit 94100d6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,11 +575,11 @@ export function createHandler<
// TODO: handle the weight parameter "q"
const [mediaType, ...params] = accept.split(';');
const charset =
params?.find((param) => param.includes('charset=')) || 'charset=utf8'; // utf-8 is assumed when not specified;
params?.find((param) => param.includes('charset=')) || 'charset=utf-8'; // utf-8 is assumed when not specified;

if (
mediaType === 'application/graphql-response+json' &&
charset === 'charset=utf8'
charset === 'charset=utf-8'
) {
acceptedMediaType = 'application/graphql-response+json';
break;
Expand All @@ -590,7 +590,7 @@ export function createHandler<
(mediaType === 'application/json' ||
mediaType === 'application/*' ||
mediaType === '*/*') &&
charset === 'charset=utf8'
(charset === 'charset=utf-8' || charset === 'charset=utf8')
) {
acceptedMediaType = 'application/json';
break;
Expand Down
42 changes: 42 additions & 0 deletions tests/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,45 @@ it('should use the default if nothing is returned from the custom request params
]
`);
});

it('should accept both utf-8 and utf8 charsets ', async () => {
const { request } = createTHandler();

await expect(
request(
'GET',
{ query: '{ __typename }' },
{ accept: 'application/json; charset=utf8' },
),
).resolves.toMatchInlineSnapshot(`
[
"{"data":{"__typename":"Query"}}",
{
"headers": {
"content-type": "application/json; charset=utf-8",
},
"status": 200,
"statusText": "OK",
},
]
`);

await expect(
request(
'GET',
{ query: '{ __typename }' },
{ accept: 'application/json; charset=utf-8' },
),
).resolves.toMatchInlineSnapshot(`
[
"{"data":{"__typename":"Query"}}",
{
"headers": {
"content-type": "application/json; charset=utf-8",
},
"status": 200,
"statusText": "OK",
},
]
`);
});

0 comments on commit 94100d6

Please sign in to comment.