axios-axios/test/unit/defaults/transformReponse.js
Dmitriy Mozgovoy 5ad6994da3
JSON improvements: throw if JSON parsing failed; number, boolean can be passed directly as payload for encoding to JSON #2613, #61, #907 (#3688)
* Draft

* Added support for primitive types to be converted to JSON if the request Content-Type is 'application/json';
Added throwing SyntaxError if JSON parsing failed and responseType is json;
Added transitional option object;
Added options validator to assert transitional options;
Added transitional option `silentJSONParsing= true` for backward compatibility;
Updated README.md;
Updated typings;

* Fixed isOlderVersion helper;
Fixed typo;
Added validator.spec.js;

* Added forcedJSONParsing transitional option #2791

* `transformData` is now called in the default configuration context if the function context is not specified (for tests compatibility);

* Added `transitional.clarifyTimeoutError` to throw ETIMEDOUT error instead of generic ECONNABORTED on request timeouts;
Added support of onloadend handler if available instead of onreadystatechange;
Added xhr timeout test;
Fixed potential bug of xhr adapter with proper handling timeouts&errors (FakeXMLHTTPRequest failed to handle timeouts);
2021-04-19 18:55:34 +02:00

31 lines
1.3 KiB
JavaScript

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);
});
});
});