Skip to content

Commit b205dc3

Browse files
fix(http): avoid parsing XML response bodies as YAML (#3986)
* fix(http): avoid parsing XML response bodies as YAML Closes #3844 --------- Co-authored-by: Vladimír Gorej <[email protected]>
1 parent 948a547 commit b205dc3

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/http/serializers/response/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ export const shouldDownloadAsText = (contentType = '') =>
44
/(json|xml|yaml|text)\b/.test(contentType);
55

66
function parseBody(body, contentType) {
7-
if (
8-
contentType &&
9-
(contentType.indexOf('application/json') === 0 || contentType.indexOf('+json') > 0)
10-
) {
11-
return JSON.parse(body);
7+
if (contentType) {
8+
if (contentType.indexOf('application/json') === 0 || contentType.indexOf('+json') > 0) {
9+
return JSON.parse(body);
10+
}
11+
if (contentType.indexOf('application/xml') === 0 || contentType.indexOf('+xml') > 0) {
12+
return body;
13+
}
1214
}
1315
return jsYaml.load(body);
1416
}

test/http/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,23 @@ describe('http', () => {
469469
expect(resSerialize.data).toBe(body);
470470
});
471471
});
472+
473+
test('should not parse xml response', () => {
474+
const headers = { 'Content-Type': 'application/xml' };
475+
const body = '<Pet><name>cat: with: colon: and: spaces</name></Pet>';
476+
const mockPool = mockAgent.get('http://swagger.io');
477+
mockPool.intercept({ path: '/' }).reply(200, body, { headers });
478+
479+
return fetch('http://swagger.io')
480+
.then((_res) =>
481+
// eslint-disable-line no-undef
482+
serializeResponse(_res, 'https://swagger.io')
483+
)
484+
.then((resSerialize) => {
485+
expect(resSerialize.body).toBe(body);
486+
expect(resSerialize.parseError).toBeUndefined();
487+
});
488+
});
472489
});
473490

474491
describe('shouldDownloadAsText', () => {

0 commit comments

Comments
 (0)