Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
fix: Use accept header to determine return content type(#168)
Browse files Browse the repository at this point in the history
* fix: change content-type to accept header

* test: update test description

* fix app.test
  • Loading branch information
liquid36 authored Jun 6, 2022
1 parent b9446ab commit c543c02
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('generateServerlessRouter', () => {
app.post('/test', async (req: express.Request, res: express.Response) => {
res.send({ data: 'test' });
});
const res = await requestWithSupertest.post('/test', {}).set('Content-Type', 'application/json');
const res = await requestWithSupertest.post('/test', {}).set('Accept', 'application/json');
expect(res.headers['content-type']).toEqual('application/json; charset=utf-8');
});
});
4 changes: 2 additions & 2 deletions src/router/middlewares/setContentType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ describe('setContentTypeMiddleware', () => {
expect(contentType).toHaveBeenCalledWith('application/fhir+json');
});

test('request should return application/json if user sent application/json', async () => {
test('request should return application/json if user request application/json in accept header', async () => {
const nextMock = jest.fn();
const req = {
headers: {
'content-type': 'application/json',
accept: 'application/json',
},
} as unknown as express.Request;
const contentType = jest.fn();
Expand Down
4 changes: 1 addition & 3 deletions src/router/middlewares/setContentType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import express from 'express';
*/
export const setContentTypeMiddleware = (req: express.Request, res: express.Response, next: express.NextFunction) => {
try {
res.contentType(
req.headers['content-type'] === 'application/json' ? 'application/json' : 'application/fhir+json',
);
res.contentType(req.headers.accept === 'application/json' ? 'application/json' : 'application/fhir+json');
next();
} catch (e) {
next(e);
Expand Down

0 comments on commit c543c02

Please sign in to comment.