mirror of
https://github.com/logto-io/logto.git
synced 2024-12-30 20:33:54 -05:00
refactor: add tests for content-type in oidc apis (#6380)
This commit is contained in:
parent
8e9f6e4a0b
commit
c6a1cab399
2 changed files with 36 additions and 1 deletions
|
@ -403,7 +403,7 @@ export default function initOidc(
|
||||||
// eslint-disable-next-line no-restricted-syntax
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
ctx.request.body = trySafe(() => JSON.parse(body) as unknown);
|
ctx.request.body = trySafe(() => JSON.parse(body) as unknown);
|
||||||
} else if (ctx.is(formUrlEncodedContentType)) {
|
} else if (ctx.is(formUrlEncodedContentType)) {
|
||||||
ctx.request.body = trySafe(() => querystring.parse(body));
|
ctx.request.body = querystring.parse(body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,4 +49,39 @@ describe('content-type: application/json compatibility', () => {
|
||||||
{ 'content-type': 'application/json1' }
|
{ 'content-type': 'application/json1' }
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should be ok when `content-type` is json but the body is malformed', async () => {
|
||||||
|
await trySafe(
|
||||||
|
api
|
||||||
|
.post('token', {
|
||||||
|
headers: {
|
||||||
|
'content-type': 'application/json',
|
||||||
|
},
|
||||||
|
body: 'this is not a json',
|
||||||
|
})
|
||||||
|
.json(),
|
||||||
|
async (error) => {
|
||||||
|
if (!(error instanceof HTTPError)) {
|
||||||
|
throw new TypeError('Error is not a HTTPError instance.');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 400 means the request has been processed, we just need to ensure no 500 error
|
||||||
|
expect(error.response.status).toBe(400);
|
||||||
|
expect(await error.response.json()).toHaveProperty(
|
||||||
|
'error_description',
|
||||||
|
'no client authentication mechanism provided'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be ok when `content-type` is json for GET requests', async () => {
|
||||||
|
await expect(
|
||||||
|
api.get('.well-known/openid-configuration', {
|
||||||
|
headers: {
|
||||||
|
'content-type': 'application/json',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
).resolves.toBeDefined();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue