mirror of
https://github.com/axios/axios.git
synced 2026-04-15 03:26:10 +08:00
* Added AxiosHeaders class; * Fixed README.md href; * Fixed a potential bug with headers normalization; * Fixed a potential bug with headers normalization; Refactored accessor building routine; Refactored default transforms; Removed `normalizeHeaderName` helper; * Added `Content-Length` accessor; Added missed `has` accessor to TS types; * Added `AxiosTransformStream` class; Added progress capturing ability for node.js environment; Added `maxRate` option to limit the data rate in node.js environment; Refactored event handled by `onUploadProgress` && `onDownloadProgress` listeners in browser environment; Added progress & data rate tests for the http adapter; Added response stream aborting test; Added a manual progress capture test for the browser; Updated TS types; Added TS tests; Refactored request abort logic for the http adapter; Added ability to abort the response stream; * Remove `stream/promises` & `timers/promises` modules usage in tests; * Use `abortcontroller-polyfill`; * Fixed AxiosTransformStream dead-lock in legacy node versions; Fixed CancelError emitting in streams; * Reworked AxiosTransformStream internal logic to optimize memory consumption; Added throwing an error if the request stream was silently destroying (without error) Refers to #3966; * Treat the destruction of the request stream as a cancellation of the request; Fixed tests; * Emit `progress` event in the next tick; * Initial refactoring; * Refactored Mocha tests to use ESM; * Refactored Karma tests to use rollup preprocessor & ESM; Replaced grunt with gulp; Improved dev scripts; Added Babel for rollup build; * Added default commonjs package export for Node build; Added automatic contributors list generator for package.json; Co-authored-by: Jay <jasonsaayman@gmail.com>
201 lines
4.7 KiB
JavaScript
201 lines
4.7 KiB
JavaScript
import AxiosError from "../../lib/core/AxiosError";
|
|
|
|
describe('transform', function () {
|
|
beforeEach(function () {
|
|
jasmine.Ajax.install();
|
|
});
|
|
|
|
afterEach(function () {
|
|
jasmine.Ajax.uninstall();
|
|
});
|
|
|
|
it('should transform JSON to string', function (done) {
|
|
const data = {
|
|
foo: 'bar'
|
|
};
|
|
|
|
axios.post('/foo', data);
|
|
|
|
getAjaxRequest().then(function (request) {
|
|
expect(request.params).toEqual('{"foo":"bar"}');
|
|
done();
|
|
});
|
|
});
|
|
|
|
it('should transform string to JSON', function (done) {
|
|
let response;
|
|
|
|
axios('/foo').then(function (data) {
|
|
response = data;
|
|
});
|
|
|
|
getAjaxRequest().then(function (request) {
|
|
request.respondWith({
|
|
status: 200,
|
|
responseText: '{"foo": "bar"}'
|
|
});
|
|
|
|
setTimeout(function () {
|
|
expect(typeof response.data).toEqual('object');
|
|
expect(response.data.foo).toEqual('bar');
|
|
done();
|
|
}, 100);
|
|
});
|
|
});
|
|
|
|
it('should throw a SyntaxError if JSON parsing failed and responseType is "json" if silentJSONParsing is false',
|
|
function (done) {
|
|
let thrown;
|
|
|
|
axios({
|
|
url: '/foo',
|
|
responseType: 'json',
|
|
transitional: {silentJSONParsing: false}
|
|
}).then(function () {
|
|
done(new Error('should fail'));
|
|
}, function (err) {
|
|
thrown = err;
|
|
});
|
|
|
|
getAjaxRequest().then(function (request) {
|
|
request.respondWith({
|
|
status: 200,
|
|
responseText: '{foo": "bar"}' // JSON SyntaxError
|
|
});
|
|
|
|
setTimeout(function () {
|
|
expect(thrown).toBeTruthy();
|
|
expect(thrown.name).toContain('SyntaxError');
|
|
expect(thrown.code).toEqual(AxiosError.ERR_BAD_RESPONSE);
|
|
done();
|
|
}, 100);
|
|
});
|
|
}
|
|
);
|
|
|
|
it('should send data as JSON if request content-type is application/json', function (done) {
|
|
let response;
|
|
|
|
axios.post('/foo', 123, {headers: {'Content-Type': 'application/json'}}).then(function (_response) {
|
|
response = _response;
|
|
}, function (err) {
|
|
done(err);
|
|
});
|
|
|
|
getAjaxRequest().then(function (request) {
|
|
request.respondWith({
|
|
status: 200,
|
|
responseText: ''
|
|
});
|
|
|
|
setTimeout(function () {
|
|
expect(response).toBeTruthy();
|
|
expect(request.requestHeaders['Content-Type']).toBe('application/json');
|
|
expect(JSON.parse(request.params)).toBe(123);
|
|
done();
|
|
}, 100);
|
|
});
|
|
});
|
|
|
|
it('should not assume JSON if responseType is not `json`', function (done) {
|
|
let response;
|
|
|
|
axios.get('/foo', {
|
|
responseType: 'text',
|
|
transitional: {
|
|
forcedJSONParsing: false
|
|
}
|
|
}).then(function (_response) {
|
|
response = _response;
|
|
}, function (err) {
|
|
done(err);
|
|
});
|
|
|
|
const rawData = '{"x":1}';
|
|
|
|
getAjaxRequest().then(function (request) {
|
|
request.respondWith({
|
|
status: 200,
|
|
responseText: rawData
|
|
});
|
|
|
|
setTimeout(function () {
|
|
expect(response).toBeTruthy();
|
|
expect(response.data).toBe(rawData);
|
|
done();
|
|
}, 100);
|
|
});
|
|
});
|
|
|
|
it('should override default transform', function (done) {
|
|
const data = {
|
|
foo: 'bar'
|
|
};
|
|
|
|
axios.post('/foo', data, {
|
|
transformRequest: function (data) {
|
|
return data;
|
|
}
|
|
});
|
|
|
|
getAjaxRequest().then(function (request) {
|
|
expect(typeof request.params).toEqual('object');
|
|
done();
|
|
});
|
|
});
|
|
|
|
it('should allow an Array of transformers', function (done) {
|
|
const data = {
|
|
foo: 'bar'
|
|
};
|
|
|
|
axios.post('/foo', data, {
|
|
transformRequest: axios.defaults.transformRequest.concat(
|
|
function (data) {
|
|
return data.replace('bar', 'baz');
|
|
}
|
|
)
|
|
});
|
|
|
|
getAjaxRequest().then(function (request) {
|
|
expect(request.params).toEqual('{"foo":"baz"}');
|
|
done();
|
|
});
|
|
});
|
|
|
|
it('should allowing mutating headers', function (done) {
|
|
const token = Math.floor(Math.random() * Math.pow(2, 64)).toString(36);
|
|
|
|
axios('/foo', {
|
|
transformRequest: function (data, headers) {
|
|
headers['X-Authorization'] = token;
|
|
}
|
|
});
|
|
|
|
getAjaxRequest().then(function (request) {
|
|
expect(request.requestHeaders['X-Authorization']).toEqual(token);
|
|
done();
|
|
});
|
|
});
|
|
|
|
it('should normalize \'content-type\' header when using a custom transformRequest', function (done) {
|
|
const data = {
|
|
foo: 'bar'
|
|
};
|
|
|
|
axios.post('/foo', data, {
|
|
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
transformRequest: [
|
|
function () {
|
|
return 'aa=44'
|
|
}
|
|
]
|
|
});
|
|
|
|
getAjaxRequest().then(function (request) {
|
|
expect(request.requestHeaders['Content-Type']).toEqual('application/x-www-form-urlencoded');
|
|
done();
|
|
});
|
|
});
|
|
});
|