mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Testing and cleanup of transformResponse (#3377)
- Tests for transformResponse - Remove eslint error by renaming the var - Test that there data a length to avoid JSON.parse headache - Use `util.isString()` over `typeof` Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
+4
-4
@@ -55,13 +55,13 @@ var defaults = {
|
|||||||
}],
|
}],
|
||||||
|
|
||||||
transformResponse: [function transformResponse(data) {
|
transformResponse: [function transformResponse(data) {
|
||||||
/*eslint no-param-reassign:0*/
|
var result = data;
|
||||||
if (typeof data === 'string') {
|
if (utils.isString(result) && result.length) {
|
||||||
try {
|
try {
|
||||||
data = JSON.parse(data);
|
result = JSON.parse(result);
|
||||||
} catch (e) { /* Ignore */ }
|
} catch (e) { /* Ignore */ }
|
||||||
}
|
}
|
||||||
return data;
|
return result;
|
||||||
}],
|
}],
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user