Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nodegate",
"description": "API gateway made simple, fast and easy to configure.",
"version": "2.0.0",
"version": "2.0.1",
"author": "Julien Martin <[email protected]>",
"license": "MIT",
"scripts": {
Expand Down
37 changes: 37 additions & 0 deletions test/workers/aggregate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,5 +458,42 @@ describe('workers/aggregate', () => {
await aggregate('post', 'https://wiki.federation.com/armaments')(container);
expect(container.body.phasers).toBe(16);
});
it('forces text parsing as json when the container headers is application/json', async () => {
const container = {
...getEmpty(),
headers: {
'content-type': 'application/json',
},
};
fetchMock.mockGlobal().postOnce('https://wiki.federation.com/armaments', {
status: 200,
body: '{ "phasers": 16 }',
headers: {
'Content-Type': 'text/plain;charset=UTF-8',
},
});
await aggregate('post', 'https://wiki.federation.com/armaments')(container);
expect(container.body.phasers).toBe(16);
});
it('should throw an error when the parsing failed', async () => {
const container = {
...getEmpty(),
headers: {
'content-type': 'application/json',
},
};
fetchMock.mockGlobal().postOnce('https://wiki.federation.com/armaments', {
status: 200,
body: '{ "badJSON: true }',
headers: {
'Content-Type': 'text/plain;charset=UTF-8',
},
});
try {
await aggregate('post', 'https://wiki.federation.com/armaments')(container);
} catch (err) {
expect(err.message).toEqual('SyntaxError: Unterminated string in JSON at position 18');
}
});
});
});
6 changes: 5 additions & 1 deletion workers/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ module.exports = (method, url, options = {}) => {
container.statusCode = response.status;
if (response.headers.get('content-type') && !isJsonContentType(response.headers.get('content-type'))) {
const text = await response.text();
setBodyToContainer(text, container, options);
setBodyToContainer(
container.headers['content-type'] === 'application/json' ? JSON.parse(text) : text,
container,
options,
);
if (!response.ok) {
throw new WorkflowError('Fetch error', { text, statusCode: response.status });
}
Expand Down