Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing and cleanup of transformResponse #3377

Merged
merged 5 commits into from Mar 24, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/defaults.js
Expand Up @@ -55,13 +55,13 @@ var defaults = {
}],

transformResponse: [function transformResponse(data) {
/*eslint no-param-reassign:0*/
if (typeof data === 'string') {
var result = data;
if (utils.isString(result) && result.length) {
try {
data = JSON.parse(data);
result = JSON.parse(result);
} catch (e) { /* Ignore */ }
}
return data;
return result;
}],

/**
Expand Down
30 changes: 30 additions & 0 deletions test/unit/defaults/transformReponse.js
@@ -0,0 +1,30 @@
var defaults = require('../../../lib/defaults');
var transformData = require('../../../lib/core/transformData');
var assert = require('assert');

describe('transformResponse', function () {
describe('200 request', function () {
it('parses json', function () {
var data = '{"message": "hello, world"}';
var result = transformData(data, {'content-type': 'application/json'}, defaults.transformResponse);
assert.strictEqual(result.message, 'hello, world');
});
it('ignores XML', function () {
var data = '<message>hello, world</message>';
var result = transformData(data, {'content-type': 'text/xml'}, defaults.transformResponse);
assert.strictEqual(result, data);
});
});
describe('204 request', function () {
it('does not parse the empty string', function () {
var data = '';
var result = transformData(data, {'content-type': undefined}, defaults.transformResponse);
assert.strictEqual(result, '');
});
it('does not parse undefined', function () {
var data = undefined;
var result = transformData(data, {'content-type': undefined}, defaults.transformResponse);
assert.strictEqual(result, data);
});
});
});