Skip to content
Merged
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
12 changes: 7 additions & 5 deletions src/http/serializers/response/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ export const shouldDownloadAsText = (contentType = '') =>
/(json|xml|yaml|text)\b/.test(contentType);

function parseBody(body, contentType) {
if (
contentType &&
(contentType.indexOf('application/json') === 0 || contentType.indexOf('+json') > 0)
) {
return JSON.parse(body);
if (contentType) {
if (contentType.indexOf('application/json') === 0 || contentType.indexOf('+json') > 0) {
return JSON.parse(body);
}
if (contentType.indexOf('application/xml') === 0 || contentType.indexOf('+xml') > 0) {
return body;
}
}
return jsYaml.load(body);
}
Expand Down
17 changes: 17 additions & 0 deletions test/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,23 @@ describe('http', () => {
expect(resSerialize.data).toBe(body);
});
});

test('should not parse xml response', () => {
const headers = { 'Content-Type': 'application/xml' };
const body = '<Pet><name>cat: with: colon: and: spaces</name></Pet>';
const mockPool = mockAgent.get('http://swagger.io');
mockPool.intercept({ path: '/' }).reply(200, body, { headers });

return fetch('http://swagger.io')
.then((_res) =>
// eslint-disable-line no-undef
serializeResponse(_res, 'https://swagger.io')
)
.then((resSerialize) => {
expect(resSerialize.body).toBe(body);
expect(resSerialize.parseError).toBeUndefined();
});
});
});

describe('shouldDownloadAsText', () => {
Expand Down