mirror of
https://github.com/tenrok/axios.git
synced 2026-05-15 11:59:42 +03:00
Axios ES2017 (#4787)
* 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>
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
</head>
|
||||
<body>
|
||||
See your console
|
||||
<script src="../../dist/axios.js"></script>
|
||||
<script>
|
||||
const data = new Int8Array(10 * 1024 * 1024);
|
||||
|
||||
data.fill(123);
|
||||
|
||||
axios.post('http://httpbin.org/post', data, {
|
||||
onUploadProgress: console.log,
|
||||
onDownloadProgress: console.log,
|
||||
}).then(data=> {
|
||||
console.log(`Done: `, data);
|
||||
}).catch(console.warn);
|
||||
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
+28
-110
@@ -1,123 +1,41 @@
|
||||
// Polyfill ES6 Promise
|
||||
require('es6-promise').polyfill();
|
||||
import _axios from '../../index.js';
|
||||
|
||||
// Polyfill URLSearchParams
|
||||
URLSearchParams = require('url-search-params');
|
||||
|
||||
// Import axios
|
||||
axios = require('../../index');
|
||||
window.axios = _axios;
|
||||
|
||||
// Jasmine config
|
||||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000;
|
||||
jasmine.getEnv().defaultTimeoutInterval = 20000;
|
||||
|
||||
// Get Ajax request using an increasing timeout to retry
|
||||
getAjaxRequest = (function () {
|
||||
var attempts = 0;
|
||||
var MAX_ATTEMPTS = 5;
|
||||
var ATTEMPT_DELAY_FACTOR = 5;
|
||||
window.getAjaxRequest = (function () {
|
||||
let attempts = 0;
|
||||
const MAX_ATTEMPTS = 5;
|
||||
const ATTEMPT_DELAY_FACTOR = 5;
|
||||
|
||||
function getAjaxRequest() {
|
||||
return new Promise(function (resolve, reject) {
|
||||
attempts = 0;
|
||||
attemptGettingAjaxRequest(resolve, reject);
|
||||
});
|
||||
}
|
||||
|
||||
function attemptGettingAjaxRequest(resolve, reject) {
|
||||
var delay = attempts * attempts * ATTEMPT_DELAY_FACTOR;
|
||||
|
||||
if (attempts++ > MAX_ATTEMPTS) {
|
||||
reject(new Error('No request was found'));
|
||||
return;
|
||||
function getAjaxRequest() {
|
||||
return new Promise(function (resolve, reject) {
|
||||
attempts = 0;
|
||||
attemptGettingAjaxRequest(resolve, reject);
|
||||
});
|
||||
}
|
||||
|
||||
setTimeout(function () {
|
||||
var request = jasmine.Ajax.requests.mostRecent();
|
||||
if (request) {
|
||||
resolve(request);
|
||||
} else {
|
||||
attemptGettingAjaxRequest(resolve, reject);
|
||||
function attemptGettingAjaxRequest(resolve, reject) {
|
||||
const delay = attempts * attempts * ATTEMPT_DELAY_FACTOR;
|
||||
|
||||
if (attempts++ > MAX_ATTEMPTS) {
|
||||
reject(new Error('No request was found'));
|
||||
return;
|
||||
}
|
||||
}, delay);
|
||||
}
|
||||
|
||||
return getAjaxRequest;
|
||||
setTimeout(function () {
|
||||
const request = jasmine.Ajax.requests.mostRecent();
|
||||
if (request) {
|
||||
resolve(request);
|
||||
} else {
|
||||
attemptGettingAjaxRequest(resolve, reject);
|
||||
}
|
||||
}, delay);
|
||||
}
|
||||
|
||||
return getAjaxRequest;
|
||||
})();
|
||||
|
||||
// Validate an invalid character error
|
||||
validateInvalidCharacterError = function validateInvalidCharacterError(error) {
|
||||
expect(/character/i.test(error.message)).toEqual(true);
|
||||
};
|
||||
|
||||
// Setup basic auth tests
|
||||
setupBasicAuthTest = function setupBasicAuthTest() {
|
||||
beforeEach(function () {
|
||||
jasmine.Ajax.install();
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
jasmine.Ajax.uninstall();
|
||||
});
|
||||
|
||||
it('should accept HTTP Basic auth with username/password', function (done) {
|
||||
axios('/foo', {
|
||||
auth: {
|
||||
username: 'Aladdin',
|
||||
password: 'open sesame'
|
||||
}
|
||||
});
|
||||
|
||||
setTimeout(function () {
|
||||
var request = jasmine.Ajax.requests.mostRecent();
|
||||
|
||||
expect(request.requestHeaders['Authorization']).toEqual('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==');
|
||||
done();
|
||||
}, 100);
|
||||
});
|
||||
|
||||
it('should accept HTTP Basic auth credentials without the password parameter', function (done) {
|
||||
axios('/foo', {
|
||||
auth: {
|
||||
username: 'Aladdin'
|
||||
}
|
||||
});
|
||||
|
||||
setTimeout(function () {
|
||||
var request = jasmine.Ajax.requests.mostRecent();
|
||||
|
||||
expect(request.requestHeaders['Authorization']).toEqual('Basic QWxhZGRpbjo=');
|
||||
done();
|
||||
}, 100);
|
||||
});
|
||||
|
||||
it('should accept HTTP Basic auth credentials with non-Latin1 characters in password', function (done) {
|
||||
axios('/foo', {
|
||||
auth: {
|
||||
username: 'Aladdin',
|
||||
password: 'open ßç£☃sesame'
|
||||
}
|
||||
});
|
||||
|
||||
setTimeout(function () {
|
||||
var request = jasmine.Ajax.requests.mostRecent();
|
||||
|
||||
expect(request.requestHeaders['Authorization']).toEqual('Basic QWxhZGRpbjpvcGVuIMOfw6fCo+KYg3Nlc2FtZQ==');
|
||||
done();
|
||||
}, 100);
|
||||
});
|
||||
|
||||
it('should fail to encode HTTP Basic auth credentials with non-Latin1 characters in username', function (done) {
|
||||
axios('/foo', {
|
||||
auth: {
|
||||
username: 'Aladßç£☃din',
|
||||
password: 'open sesame'
|
||||
}
|
||||
}).then(function(response) {
|
||||
done(new Error('Should not succeed to make a HTTP Basic auth request with non-latin1 chars in credentials.'));
|
||||
}).catch(function(error) {
|
||||
validateInvalidCharacterError(error);
|
||||
done();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
var axios = require('../../index');
|
||||
|
||||
describe('adapter', function () {
|
||||
beforeEach(function () {
|
||||
@@ -13,7 +12,7 @@ describe('adapter', function () {
|
||||
axios('/foo', {
|
||||
adapter: function barAdapter(config) {
|
||||
return new Promise(function dispatchXhrRequest(resolve) {
|
||||
var request = new XMLHttpRequest();
|
||||
const request = new XMLHttpRequest();
|
||||
request.open('GET', '/bar');
|
||||
|
||||
request.onreadystatechange = function () {
|
||||
@@ -26,7 +25,7 @@ describe('adapter', function () {
|
||||
request.send(null);
|
||||
});
|
||||
}
|
||||
}).catch(console.log);
|
||||
}).catch(done);
|
||||
|
||||
getAjaxRequest().then(function(request) {
|
||||
expect(request.url).toBe('/bar');
|
||||
@@ -35,11 +34,11 @@ describe('adapter', function () {
|
||||
});
|
||||
|
||||
it('should execute adapter code synchronously', function (done) {
|
||||
var asyncFlag = false;
|
||||
let asyncFlag = false;
|
||||
axios('/foo', {
|
||||
adapter: function barAdapter(config) {
|
||||
return new Promise(function dispatchXhrRequest(resolve) {
|
||||
var request = new XMLHttpRequest();
|
||||
const request = new XMLHttpRequest();
|
||||
request.open('GET', '/bar');
|
||||
|
||||
request.onreadystatechange = function () {
|
||||
@@ -53,7 +52,7 @@ describe('adapter', function () {
|
||||
request.send(null);
|
||||
});
|
||||
}
|
||||
}).catch(console.log);
|
||||
}).catch(done);
|
||||
|
||||
asyncFlag = true;
|
||||
|
||||
@@ -63,7 +62,7 @@ describe('adapter', function () {
|
||||
});
|
||||
|
||||
it('should execute adapter code asynchronously when interceptor is present', function (done) {
|
||||
var asyncFlag = false;
|
||||
let asyncFlag = false;
|
||||
|
||||
axios.interceptors.request.use(function (config) {
|
||||
config.headers.async = 'async it!';
|
||||
@@ -73,7 +72,7 @@ describe('adapter', function () {
|
||||
axios('/foo', {
|
||||
adapter: function barAdapter(config) {
|
||||
return new Promise(function dispatchXhrRequest(resolve) {
|
||||
var request = new XMLHttpRequest();
|
||||
const request = new XMLHttpRequest();
|
||||
request.open('GET', '/bar');
|
||||
|
||||
request.onreadystatechange = function () {
|
||||
@@ -87,7 +86,7 @@ describe('adapter', function () {
|
||||
request.send(null);
|
||||
});
|
||||
}
|
||||
}).catch(console.log);
|
||||
}).catch(done);
|
||||
|
||||
asyncFlag = true;
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
|
||||
describe('static api', function () {
|
||||
it('should have request method helpers', function () {
|
||||
expect(typeof axios.request).toEqual('function');
|
||||
@@ -11,7 +12,7 @@ describe('static api', function () {
|
||||
});
|
||||
|
||||
it('should have promise method helpers', function () {
|
||||
var promise = axios('/test');
|
||||
const promise = axios('/test');
|
||||
|
||||
expect(typeof promise.then).toEqual('function');
|
||||
expect(typeof promise.catch).toEqual('function');
|
||||
@@ -52,7 +53,7 @@ describe('static api', function () {
|
||||
});
|
||||
|
||||
describe('instance api', function () {
|
||||
var instance = axios.create();
|
||||
const instance = axios.create();
|
||||
|
||||
it('should have request methods', function () {
|
||||
expect(typeof instance.request).toEqual('function');
|
||||
|
||||
@@ -1,3 +1,77 @@
|
||||
import axios from "../../index";
|
||||
|
||||
function validateInvalidCharacterError(error) {
|
||||
expect(/character/i.test(error.message)).toEqual(true);
|
||||
};
|
||||
|
||||
describe('basicAuth', function () {
|
||||
setupBasicAuthTest();
|
||||
// Validate an invalid character error
|
||||
beforeEach(function () {
|
||||
jasmine.Ajax.install();
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
jasmine.Ajax.uninstall();
|
||||
});
|
||||
|
||||
it('should accept HTTP Basic auth with username/password', function (done) {
|
||||
axios('/foo', {
|
||||
auth: {
|
||||
username: 'Aladdin',
|
||||
password: 'open sesame'
|
||||
}
|
||||
});
|
||||
|
||||
setTimeout(function () {
|
||||
const request = jasmine.Ajax.requests.mostRecent();
|
||||
|
||||
expect(request.requestHeaders['Authorization']).toEqual('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==');
|
||||
done();
|
||||
}, 100);
|
||||
});
|
||||
|
||||
it('should accept HTTP Basic auth credentials without the password parameter', function (done) {
|
||||
axios('/foo', {
|
||||
auth: {
|
||||
username: 'Aladdin'
|
||||
}
|
||||
});
|
||||
|
||||
setTimeout(function () {
|
||||
const request = jasmine.Ajax.requests.mostRecent();
|
||||
|
||||
expect(request.requestHeaders['Authorization']).toEqual('Basic QWxhZGRpbjo=');
|
||||
done();
|
||||
}, 100);
|
||||
});
|
||||
|
||||
it('should accept HTTP Basic auth credentials with non-Latin1 characters in password', function (done) {
|
||||
axios('/foo', {
|
||||
auth: {
|
||||
username: 'Aladdin',
|
||||
password: 'open ßç£☃sesame'
|
||||
}
|
||||
});
|
||||
|
||||
setTimeout(function () {
|
||||
const request = jasmine.Ajax.requests.mostRecent();
|
||||
|
||||
expect(request.requestHeaders['Authorization']).toEqual('Basic QWxhZGRpbjpvcGVuIMOfw6fCo+KYg3Nlc2FtZQ==');
|
||||
done();
|
||||
}, 100);
|
||||
});
|
||||
|
||||
it('should fail to encode HTTP Basic auth credentials with non-Latin1 characters in username', function (done) {
|
||||
axios('/foo', {
|
||||
auth: {
|
||||
username: 'Aladßç£☃din',
|
||||
password: 'open sesame'
|
||||
}
|
||||
}).then(function (response) {
|
||||
done(new Error('Should not succeed to make a HTTP Basic auth request with non-latin1 chars in credentials.'));
|
||||
}).catch(function (error) {
|
||||
validateInvalidCharacterError(error);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
var Cancel = axios.Cancel;
|
||||
var CancelToken = axios.CancelToken;
|
||||
var _AbortController = require('abortcontroller-polyfill/dist/cjs-ponyfill.js').AbortController;
|
||||
const Cancel = axios.Cancel;
|
||||
const CancelToken = axios.CancelToken;
|
||||
import {AbortController as _AbortController} from 'abortcontroller-polyfill/dist/cjs-ponyfill.js';
|
||||
|
||||
var AbortController = typeof AbortController === 'function' ? AbortController : _AbortController;
|
||||
const envAbortController = typeof AbortController === 'function' ? AbortController : _AbortController;
|
||||
|
||||
describe('cancel', function() {
|
||||
beforeEach(function() {
|
||||
@@ -15,7 +15,7 @@ describe('cancel', function() {
|
||||
|
||||
describe('when called before sending request', function() {
|
||||
it('rejects Promise with a CanceledError object', function(done) {
|
||||
var source = CancelToken.source();
|
||||
const source = CancelToken.source();
|
||||
source.cancel('Operation has been canceled.');
|
||||
axios.get('/foo', {
|
||||
cancelToken: source.token
|
||||
@@ -29,7 +29,7 @@ describe('cancel', function() {
|
||||
|
||||
describe('when called after request has been sent', function() {
|
||||
it('rejects Promise with a CanceledError object', function(done) {
|
||||
var source = CancelToken.source();
|
||||
const source = CancelToken.source();
|
||||
axios.get('/foo/bar', {
|
||||
cancelToken: source.token
|
||||
}).catch(function(thrown) {
|
||||
@@ -49,8 +49,8 @@ describe('cancel', function() {
|
||||
});
|
||||
|
||||
it('calls abort on request object', function(done) {
|
||||
var source = CancelToken.source();
|
||||
var request;
|
||||
const source = CancelToken.source();
|
||||
let request;
|
||||
axios.get('/foo/bar', {
|
||||
cancelToken: source.token
|
||||
}).catch(function() {
|
||||
@@ -91,7 +91,7 @@ describe('cancel', function() {
|
||||
// });
|
||||
|
||||
it('it should support cancellation using AbortController signal', function(done) {
|
||||
var controller = new AbortController();
|
||||
const controller = new envAbortController();
|
||||
|
||||
axios.get('/foo/bar', {
|
||||
signal: controller.signal
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
var CancelToken = require('../../../lib/cancel/CancelToken');
|
||||
var CanceledError = require('../../../lib/cancel/CanceledError');
|
||||
import CancelToken from '../../../lib/cancel/CancelToken';
|
||||
import CanceledError from '../../../lib/cancel/CanceledError';
|
||||
|
||||
describe('CancelToken', function() {
|
||||
describe('constructor', function() {
|
||||
@@ -18,8 +18,8 @@ describe('CancelToken', function() {
|
||||
|
||||
describe('reason', function() {
|
||||
it('returns a CanceledError if cancellation has been requested', function() {
|
||||
var cancel;
|
||||
var token = new CancelToken(function(c) {
|
||||
let cancel;
|
||||
const token = new CancelToken(function(c) {
|
||||
cancel = c;
|
||||
});
|
||||
cancel('Operation has been canceled.');
|
||||
@@ -28,15 +28,15 @@ describe('CancelToken', function() {
|
||||
});
|
||||
|
||||
it('returns undefined if cancellation has not been requested', function() {
|
||||
var token = new CancelToken(function() {});
|
||||
const token = new CancelToken(function() {});
|
||||
expect(token.reason).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('promise', function() {
|
||||
it('returns a Promise that resolves when cancellation is requested', function(done) {
|
||||
var cancel;
|
||||
var token = new CancelToken(function(c) {
|
||||
let cancel;
|
||||
const token = new CancelToken(function(c) {
|
||||
cancel = c;
|
||||
});
|
||||
token.promise.then(function onFulfilled(value) {
|
||||
@@ -51,8 +51,8 @@ describe('CancelToken', function() {
|
||||
describe('throwIfRequested', function() {
|
||||
it('throws if cancellation has been requested', function() {
|
||||
// Note: we cannot use expect.toThrowError here as CanceledError does not inherit from Error
|
||||
var cancel;
|
||||
var token = new CancelToken(function(c) {
|
||||
let cancel;
|
||||
const token = new CancelToken(function(c) {
|
||||
cancel = c;
|
||||
});
|
||||
cancel('Operation has been canceled.');
|
||||
@@ -68,14 +68,14 @@ describe('CancelToken', function() {
|
||||
});
|
||||
|
||||
it('does not throw if cancellation has not been requested', function() {
|
||||
var token = new CancelToken(function() {});
|
||||
const token = new CancelToken(function() {});
|
||||
token.throwIfRequested();
|
||||
});
|
||||
});
|
||||
|
||||
describe('source', function() {
|
||||
it('returns an object containing token and cancel function', function() {
|
||||
var source = CancelToken.source();
|
||||
const source = CancelToken.source();
|
||||
expect(source.token).toEqual(jasmine.any(CancelToken));
|
||||
expect(source.cancel).toEqual(jasmine.any(Function));
|
||||
expect(source.token.reason).toBeUndefined();
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
var CanceledError = require('../../../lib/cancel/CanceledError');
|
||||
import CanceledError from '../../../lib/cancel/CanceledError';
|
||||
|
||||
describe('Cancel', function() {
|
||||
describe('toString', function() {
|
||||
it('returns correct result when message is not specified', function() {
|
||||
var cancel = new CanceledError();
|
||||
const cancel = new CanceledError();
|
||||
expect(cancel.toString()).toBe('CanceledError: canceled');
|
||||
});
|
||||
|
||||
it('returns correct result when message is specified', function() {
|
||||
var cancel = new CanceledError('Operation has been canceled.');
|
||||
const cancel = new CanceledError('Operation has been canceled.');
|
||||
expect(cancel.toString()).toBe('CanceledError: Operation has been canceled.');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
var isCancel = require('../../../lib/cancel/isCancel');
|
||||
var CanceledError = require('../../../lib/cancel/CanceledError');
|
||||
import isCancel from '../../../lib/cancel/isCancel';
|
||||
import CanceledError from '../../../lib/cancel/CanceledError';
|
||||
|
||||
describe('isCancel', function() {
|
||||
it('returns true if value is a CanceledError', function() {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
var AxiosError = require('../../../lib/core/AxiosError');
|
||||
import AxiosError from '../../../lib/core/AxiosError';
|
||||
|
||||
describe('core::AxiosError', function() {
|
||||
it('should create an Error with message, config, code, request, response, stack and isAxiosError', function() {
|
||||
var request = { path: '/foo' };
|
||||
var response = { status: 200, data: { foo: 'bar' } };
|
||||
var error = new AxiosError('Boom!', 'ESOMETHING', { foo: 'bar' }, request, response);
|
||||
const request = { path: '/foo' };
|
||||
const response = { status: 200, data: { foo: 'bar' } };
|
||||
const error = new AxiosError('Boom!', 'ESOMETHING', { foo: 'bar' }, request, response);
|
||||
expect(error instanceof Error).toBe(true);
|
||||
expect(error.message).toBe('Boom!');
|
||||
expect(error.config).toEqual({ foo: 'bar' });
|
||||
@@ -17,10 +17,10 @@ describe('core::AxiosError', function() {
|
||||
it('should create an Error that can be serialized to JSON', function() {
|
||||
// Attempting to serialize request and response results in
|
||||
// TypeError: Converting circular structure to JSON
|
||||
var request = { path: '/foo' };
|
||||
var response = { status: 200, data: { foo: 'bar' } };
|
||||
var error = new AxiosError('Boom!', 'ESOMETHING', { foo: 'bar' }, request, response);
|
||||
var json = error.toJSON();
|
||||
const request = { path: '/foo' };
|
||||
const response = { status: 200, data: { foo: 'bar' } };
|
||||
const error = new AxiosError('Boom!', 'ESOMETHING', { foo: 'bar' }, request, response);
|
||||
const json = error.toJSON();
|
||||
expect(json.message).toBe('Boom!');
|
||||
expect(json.config).toEqual({ foo: 'bar' });
|
||||
expect(json.code).toBe('ESOMETHING');
|
||||
@@ -31,11 +31,11 @@ describe('core::AxiosError', function() {
|
||||
|
||||
describe('core::createError.from', function() {
|
||||
it('should add config, config, request and response to error', function() {
|
||||
var error = new Error('Boom!');
|
||||
var request = { path: '/foo' };
|
||||
var response = { status: 200, data: { foo: 'bar' } };
|
||||
const error = new Error('Boom!');
|
||||
const request = { path: '/foo' };
|
||||
const response = { status: 200, data: { foo: 'bar' } };
|
||||
|
||||
var axiosError = AxiosError.from(error, 'ESOMETHING', { foo: 'bar' }, request, response);
|
||||
const axiosError = AxiosError.from(error, 'ESOMETHING', { foo: 'bar' }, request, response);
|
||||
expect(axiosError.config).toEqual({ foo: 'bar' });
|
||||
expect(axiosError.code).toBe('ESOMETHING');
|
||||
expect(axiosError.request).toBe(request);
|
||||
@@ -44,7 +44,7 @@ describe('core::AxiosError', function() {
|
||||
});
|
||||
|
||||
it('should return error', function() {
|
||||
var error = new Error('Boom!');
|
||||
const error = new Error('Boom!');
|
||||
expect(AxiosError.from(error, 'ESOMETHING', { foo: 'bar' }) instanceof AxiosError).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var buildFullPath = require('../../../lib/core/buildFullPath');
|
||||
import buildFullPath from '../../../lib/core/buildFullPath';
|
||||
|
||||
describe('helpers::buildFullPath', function () {
|
||||
it('should combine URLs when the requestedURL is relative', function () {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
var defaults = require('../../../lib/defaults');
|
||||
var mergeConfig = require('../../../lib/core/mergeConfig');
|
||||
import defaults from '../../../lib/defaults';
|
||||
import mergeConfig from '../../../lib/core/mergeConfig';
|
||||
|
||||
describe('core::mergeConfig', function() {
|
||||
it('should accept undefined for second argument', function() {
|
||||
@@ -11,19 +11,19 @@ describe('core::mergeConfig', function() {
|
||||
});
|
||||
|
||||
it('should not leave references', function() {
|
||||
var merged = mergeConfig(defaults, {});
|
||||
const merged = mergeConfig(defaults, {});
|
||||
expect(merged).not.toBe(defaults);
|
||||
expect(merged.headers).not.toBe(defaults.headers);
|
||||
});
|
||||
|
||||
it('should allow setting request options', function() {
|
||||
var config = {
|
||||
const config = {
|
||||
url: '__sample url__',
|
||||
method: '__sample method__',
|
||||
params: '__sample params__',
|
||||
data: { foo: true }
|
||||
};
|
||||
var merged = mergeConfig(defaults, config);
|
||||
const merged = mergeConfig(defaults, config);
|
||||
expect(merged.url).toEqual(config.url);
|
||||
expect(merged.method).toEqual(config.method);
|
||||
expect(merged.params).toEqual(config.params);
|
||||
@@ -31,18 +31,18 @@ describe('core::mergeConfig', function() {
|
||||
});
|
||||
|
||||
it('should not inherit request options', function() {
|
||||
var localDefaults = {
|
||||
const localDefaults = {
|
||||
method: '__sample method__',
|
||||
data: { foo: true }
|
||||
};
|
||||
var merged = mergeConfig(localDefaults, {});
|
||||
const merged = mergeConfig(localDefaults, {});
|
||||
expect(merged.method).toEqual(undefined);
|
||||
expect(merged.data).toEqual(undefined);
|
||||
});
|
||||
|
||||
['auth', 'headers', 'params', 'proxy'].forEach(function(key) {
|
||||
it('should set new config for' + key + ' without default', function() {
|
||||
var a = {}, b = {}, c = {}
|
||||
const a = {}, b = {}, c = {};
|
||||
a[key] = undefined
|
||||
b[key] = { user: 'foo', pass: 'test' }
|
||||
c[key] = { user: 'foo', pass: 'test' }
|
||||
@@ -51,7 +51,7 @@ describe('core::mergeConfig', function() {
|
||||
});
|
||||
|
||||
it('should merge ' + key + ' with defaults', function() {
|
||||
var a = {}, b = {}, c = {};
|
||||
const a = {}, b = {}, c = {};
|
||||
a[key] = { user: 'foo', pass: 'bar' };
|
||||
b[key] = { pass: 'test' };
|
||||
c[key] = { user: 'foo', pass: 'test' };
|
||||
@@ -61,7 +61,7 @@ describe('core::mergeConfig', function() {
|
||||
|
||||
it('should overwrite default ' + key + ' with a non-object value', function() {
|
||||
[false, null, 123].forEach(function(value) {
|
||||
var a = {}, b = {}, c = {};
|
||||
const a = {}, b = {}, c = {};
|
||||
a[key] = { user: 'foo', pass: 'test' };
|
||||
b[key] = value;
|
||||
c[key] = value;
|
||||
@@ -72,22 +72,22 @@ describe('core::mergeConfig', function() {
|
||||
});
|
||||
|
||||
it('should allow setting other options', function() {
|
||||
var merged = mergeConfig(defaults, { timeout: 123 });
|
||||
const merged = mergeConfig(defaults, { timeout: 123 });
|
||||
expect(merged.timeout).toEqual(123);
|
||||
});
|
||||
|
||||
it('should allow setting custom options', function() {
|
||||
var merged = mergeConfig(defaults, { foo: 'bar' });
|
||||
const merged = mergeConfig(defaults, { foo: 'bar' });
|
||||
expect(merged.foo).toEqual('bar');
|
||||
});
|
||||
|
||||
it('should allow setting custom default options', function() {
|
||||
var merged = mergeConfig({ foo: 'bar' }, {});
|
||||
const merged = mergeConfig({ foo: 'bar' }, {});
|
||||
expect(merged.foo).toEqual('bar');
|
||||
});
|
||||
|
||||
it('should allow merging custom objects in the config', function() {
|
||||
var merged = mergeConfig({
|
||||
const merged = mergeConfig({
|
||||
nestedConfig: {
|
||||
propertyOnDefaultConfig: true
|
||||
}
|
||||
@@ -101,28 +101,28 @@ describe('core::mergeConfig', function() {
|
||||
});
|
||||
|
||||
describe('valueFromConfig2Keys', function() {
|
||||
var config1 = {url: '/foo', method: 'post', data: {a: 3}};
|
||||
const config1 = {url: '/foo', method: 'post', data: {a: 3}};
|
||||
|
||||
it('should skip if config2 is undefined', function() {
|
||||
expect(mergeConfig(config1, {})).toEqual({});
|
||||
});
|
||||
|
||||
it('should clone config2 if is plain object', function() {
|
||||
var data = {a: 1, b: 2};
|
||||
var merged = mergeConfig(config1, {data: data});
|
||||
const data = {a: 1, b: 2};
|
||||
const merged = mergeConfig(config1, {data: data});
|
||||
expect(merged.data).toEqual(data);
|
||||
expect(merged.data).not.toBe(data);
|
||||
});
|
||||
|
||||
it('should clone config2 if is array', function() {
|
||||
var data = [1, 2, 3];
|
||||
var merged = mergeConfig(config1, {data: data});
|
||||
const data = [1, 2, 3];
|
||||
const merged = mergeConfig(config1, {data: data});
|
||||
expect(merged.data).toEqual(data);
|
||||
expect(merged.data).not.toBe(data);
|
||||
});
|
||||
|
||||
it('should set as config2 in other cases', function() {
|
||||
var obj = Object.create({});
|
||||
const obj = Object.create({});
|
||||
expect(mergeConfig(config1, {data: 1}).data).toBe(1);
|
||||
expect(mergeConfig(config1, {data: 'str'}).data).toBe('str');
|
||||
expect(mergeConfig(config1, {data: obj}).data).toBe(obj);
|
||||
@@ -141,24 +141,24 @@ describe('core::mergeConfig', function() {
|
||||
});
|
||||
|
||||
it('should clone config2 if is plain object', function() {
|
||||
var config1 = {headers: [1, 2, 3]};
|
||||
var config2 = {headers: {a: 1, b: 2}};
|
||||
var merged = mergeConfig(config1, config2);
|
||||
const config1 = {headers: [1, 2, 3]};
|
||||
const config2 = {headers: {a: 1, b: 2}};
|
||||
const merged = mergeConfig(config1, config2);
|
||||
expect(merged.headers).toEqual(config2.headers);
|
||||
expect(merged.headers).not.toBe(config2.headers);
|
||||
});
|
||||
|
||||
it('should clone config2 if is array', function() {
|
||||
var config1 = {headers: {a: 1, b: 1}};
|
||||
var config2 = {headers: [1, 2, 3]};
|
||||
var merged = mergeConfig(config1, config2);
|
||||
const config1 = {headers: {a: 1, b: 1}};
|
||||
const config2 = {headers: [1, 2, 3]};
|
||||
const merged = mergeConfig(config1, config2);
|
||||
expect(merged.headers).toEqual(config2.headers);
|
||||
expect(merged.headers).not.toBe(config2.headers);
|
||||
});
|
||||
|
||||
it('should set as config2 in other cases', function() {
|
||||
var config1 = {headers: {a: 1, b: 1}};
|
||||
var obj = Object.create({});
|
||||
const config1 = {headers: {a: 1, b: 1}};
|
||||
const obj = Object.create({});
|
||||
expect(mergeConfig(config1, {headers: 1}).headers).toBe(1);
|
||||
expect(mergeConfig(config1, {headers: 'str'}).headers).toBe('str');
|
||||
expect(mergeConfig(config1, {headers: obj}).headers).toBe(obj);
|
||||
@@ -166,24 +166,24 @@ describe('core::mergeConfig', function() {
|
||||
});
|
||||
|
||||
it('should clone config1 if is plain object', function() {
|
||||
var config1 = {headers: {a: 1, b: 2}};
|
||||
var config2 = {};
|
||||
var merged = mergeConfig(config1, config2);
|
||||
const config1 = {headers: {a: 1, b: 2}};
|
||||
const config2 = {};
|
||||
const merged = mergeConfig(config1, config2);
|
||||
expect(merged.headers).toEqual(config1.headers);
|
||||
expect(merged.headers).not.toBe(config1.headers);
|
||||
});
|
||||
|
||||
it('should clone config1 if is array', function() {
|
||||
var config1 = {headers: [1, 2, 3]};
|
||||
var config2 = {};
|
||||
var merged = mergeConfig(config1, config2);
|
||||
const config1 = {headers: [1, 2, 3]};
|
||||
const config2 = {};
|
||||
const merged = mergeConfig(config1, config2);
|
||||
expect(merged.headers).toEqual(config1.headers);
|
||||
expect(merged.headers).not.toBe(config1.headers);
|
||||
});
|
||||
|
||||
it('should set as config1 in other cases', function() {
|
||||
var config2 = {};
|
||||
var obj = Object.create({});
|
||||
const config2 = {};
|
||||
const obj = Object.create({});
|
||||
expect(mergeConfig({headers: 1}, config2).headers).toBe(1);
|
||||
expect(mergeConfig({headers: 'str'}, config2).headers).toBe('str');
|
||||
expect(mergeConfig({headers: obj}, config2).headers).toBe(obj);
|
||||
@@ -197,24 +197,24 @@ describe('core::mergeConfig', function() {
|
||||
});
|
||||
|
||||
it('should clone config2 if both config1 and config2 are plain object', function() {
|
||||
var config1 = {transformRequest: {a: 1, b: 1}};
|
||||
var config2 = {transformRequest: {b: 2, c: 2}};
|
||||
var merged = mergeConfig(config1, config2);
|
||||
const config1 = {transformRequest: {a: 1, b: 1}};
|
||||
const config2 = {transformRequest: {b: 2, c: 2}};
|
||||
const merged = mergeConfig(config1, config2);
|
||||
expect(merged.transformRequest).toEqual(config2.transformRequest);
|
||||
expect(merged.transformRequest).not.toBe(config2.transformRequest);
|
||||
});
|
||||
|
||||
it('should clone config2 if is array', function() {
|
||||
var config1 = {transformRequest: {a: 1, b: 1}};
|
||||
var config2 = {transformRequest: [1, 2, 3]};
|
||||
var merged = mergeConfig(config1, config2);
|
||||
const config1 = {transformRequest: {a: 1, b: 1}};
|
||||
const config2 = {transformRequest: [1, 2, 3]};
|
||||
const merged = mergeConfig(config1, config2);
|
||||
expect(merged.transformRequest).toEqual(config2.transformRequest);
|
||||
expect(merged.transformRequest).not.toBe(config2.transformRequest);
|
||||
});
|
||||
|
||||
it('should set as config2 in other cases', function() {
|
||||
var config1 = {transformRequest: {a: 1, b: 1}};
|
||||
var obj = Object.create({});
|
||||
const config1 = {transformRequest: {a: 1, b: 1}};
|
||||
const obj = Object.create({});
|
||||
expect(mergeConfig(config1, {transformRequest: 1}).transformRequest).toBe(1);
|
||||
expect(mergeConfig(config1, {transformRequest: 'str'}).transformRequest).toBe('str');
|
||||
expect(mergeConfig(config1, {transformRequest: obj}).transformRequest).toBe(obj);
|
||||
@@ -222,24 +222,24 @@ describe('core::mergeConfig', function() {
|
||||
});
|
||||
|
||||
it('should clone config1 if is plain object', function() {
|
||||
var config1 = {transformRequest: {a: 1, b: 2}};
|
||||
var config2 = {};
|
||||
var merged = mergeConfig(config1, config2);
|
||||
const config1 = {transformRequest: {a: 1, b: 2}};
|
||||
const config2 = {};
|
||||
const merged = mergeConfig(config1, config2);
|
||||
expect(merged.transformRequest).toEqual(config1.transformRequest);
|
||||
expect(merged.transformRequest).not.toBe(config1.transformRequest);
|
||||
});
|
||||
|
||||
it('should clone config1 if is array', function() {
|
||||
var config1 = {transformRequest: [1, 2, 3]};
|
||||
var config2 = {};
|
||||
var merged = mergeConfig(config1, config2);
|
||||
const config1 = {transformRequest: [1, 2, 3]};
|
||||
const config2 = {};
|
||||
const merged = mergeConfig(config1, config2);
|
||||
expect(merged.transformRequest).toEqual(config1.transformRequest);
|
||||
expect(merged.transformRequest).not.toBe(config1.transformRequest);
|
||||
});
|
||||
|
||||
it('should set as config1 in other cases', function() {
|
||||
var config2 = {};
|
||||
var obj = Object.create({});
|
||||
const config2 = {};
|
||||
const obj = Object.create({});
|
||||
expect(mergeConfig({transformRequest: 1}, config2).transformRequest).toBe(1);
|
||||
expect(mergeConfig({transformRequest: 'str'}, config2).transformRequest).toBe('str');
|
||||
expect(mergeConfig({transformRequest: obj}, config2).transformRequest).toBe(obj);
|
||||
@@ -258,24 +258,24 @@ describe('core::mergeConfig', function() {
|
||||
});
|
||||
|
||||
it('should clone config2 if is plain object', function() {
|
||||
var config1 = {validateStatus: [1, 2, 3]};
|
||||
var config2 = {validateStatus: {a: 1, b: 2}};
|
||||
var merged = mergeConfig(config1, config2);
|
||||
const config1 = {validateStatus: [1, 2, 3]};
|
||||
const config2 = {validateStatus: {a: 1, b: 2}};
|
||||
const merged = mergeConfig(config1, config2);
|
||||
expect(merged.validateStatus).toEqual(config2.validateStatus);
|
||||
expect(merged.validateStatus).not.toBe(config2.validateStatus);
|
||||
});
|
||||
|
||||
it('should clone config2 if is array', function() {
|
||||
var config1 = {validateStatus: {a: 1, b: 2}};
|
||||
var config2 = {validateStatus: [1, 2, 3]};
|
||||
var merged = mergeConfig(config1, config2);
|
||||
const config1 = {validateStatus: {a: 1, b: 2}};
|
||||
const config2 = {validateStatus: [1, 2, 3]};
|
||||
const merged = mergeConfig(config1, config2);
|
||||
expect(merged.validateStatus).toEqual(config2.validateStatus);
|
||||
expect(merged.validateStatus).not.toBe(config2.validateStatus);
|
||||
});
|
||||
|
||||
it('should set as config2 in other cases', function() {
|
||||
var config1 = {validateStatus: {a: 1, b: 2}};
|
||||
var obj = Object.create({});
|
||||
const config1 = {validateStatus: {a: 1, b: 2}};
|
||||
const obj = Object.create({});
|
||||
expect(mergeConfig(config1, {validateStatus: 1}).validateStatus).toBe(1);
|
||||
expect(mergeConfig(config1, {validateStatus: 'str'}).validateStatus).toBe('str');
|
||||
expect(mergeConfig(config1, {validateStatus: obj}).validateStatus).toBe(obj);
|
||||
@@ -283,24 +283,24 @@ describe('core::mergeConfig', function() {
|
||||
});
|
||||
|
||||
it('should clone config1 if is plain object', function() {
|
||||
var config1 = {validateStatus: {a: 1, b: 2}};
|
||||
var config2 = {};
|
||||
var merged = mergeConfig(config1, config2);
|
||||
const config1 = {validateStatus: {a: 1, b: 2}};
|
||||
const config2 = {};
|
||||
const merged = mergeConfig(config1, config2);
|
||||
expect(merged.validateStatus).toEqual(config1.validateStatus);
|
||||
expect(merged.validateStatus).not.toBe(config1.validateStatus);
|
||||
});
|
||||
|
||||
it('should clone config1 if is array', function() {
|
||||
var config1 = {validateStatus: [1, 2, 3]};
|
||||
var config2 = {};
|
||||
var merged = mergeConfig(config1, config2);
|
||||
const config1 = {validateStatus: [1, 2, 3]};
|
||||
const config2 = {};
|
||||
const merged = mergeConfig(config1, config2);
|
||||
expect(merged.validateStatus).toEqual(config1.validateStatus);
|
||||
expect(merged.validateStatus).not.toBe(config1.validateStatus);
|
||||
});
|
||||
|
||||
it('should set as config1 in other cases', function() {
|
||||
var config2 = {};
|
||||
var obj = Object.create({});
|
||||
const config2 = {};
|
||||
const obj = Object.create({});
|
||||
expect(mergeConfig({validateStatus: 1}, config2).validateStatus).toBe(1);
|
||||
expect(mergeConfig({validateStatus: 'str'}, config2).validateStatus).toBe('str');
|
||||
expect(mergeConfig({validateStatus: obj}, config2).validateStatus).toBe(obj);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
var settle = require('../../../lib/core/settle');
|
||||
import settle from '../../../lib/core/settle';
|
||||
|
||||
describe('core::settle', function() {
|
||||
var resolve;
|
||||
var reject;
|
||||
let resolve;
|
||||
let reject;
|
||||
|
||||
beforeEach(function() {
|
||||
resolve = jasmine.createSpy('resolve');
|
||||
@@ -10,7 +10,7 @@ describe('core::settle', function() {
|
||||
});
|
||||
|
||||
it('should resolve promise if status is not set', function() {
|
||||
var response = {
|
||||
const response = {
|
||||
config: {
|
||||
validateStatus: function() {
|
||||
return true;
|
||||
@@ -23,7 +23,7 @@ describe('core::settle', function() {
|
||||
});
|
||||
|
||||
it('should resolve promise if validateStatus is not set', function() {
|
||||
var response = {
|
||||
const response = {
|
||||
status: 500,
|
||||
config: {
|
||||
}
|
||||
@@ -34,7 +34,7 @@ describe('core::settle', function() {
|
||||
});
|
||||
|
||||
it('should resolve promise if validateStatus returns true', function() {
|
||||
var response = {
|
||||
const response = {
|
||||
status: 500,
|
||||
config: {
|
||||
validateStatus: function() {
|
||||
@@ -48,10 +48,10 @@ describe('core::settle', function() {
|
||||
});
|
||||
|
||||
it('should reject promise if validateStatus returns false', function() {
|
||||
var req = {
|
||||
const req = {
|
||||
path: '/foo'
|
||||
};
|
||||
var response = {
|
||||
const response = {
|
||||
status: 500,
|
||||
config: {
|
||||
validateStatus: function() {
|
||||
@@ -63,7 +63,7 @@ describe('core::settle', function() {
|
||||
settle(resolve, reject, response);
|
||||
expect(resolve).not.toHaveBeenCalled();
|
||||
expect(reject).toHaveBeenCalled();
|
||||
var reason = reject.calls.first().args[0];
|
||||
const reason = reject.calls.first().args[0];
|
||||
expect(reason instanceof Error).toBe(true);
|
||||
expect(reason.message).toBe('Request failed with status code 500');
|
||||
expect(reason.config).toBe(response.config);
|
||||
@@ -72,8 +72,8 @@ describe('core::settle', function() {
|
||||
});
|
||||
|
||||
it('should pass status to validateStatus', function() {
|
||||
var validateStatus = jasmine.createSpy('validateStatus');
|
||||
var response = {
|
||||
const validateStatus = jasmine.createSpy('validateStatus');
|
||||
const response = {
|
||||
status: 500,
|
||||
config: {
|
||||
validateStatus: validateStatus
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
var transformData = require('../../../lib/core/transformData');
|
||||
import transformData from '../../../lib/core/transformData';
|
||||
|
||||
describe('core::transformData', function () {
|
||||
it('should support a single transformer', function () {
|
||||
var data;
|
||||
data = transformData(data, null, null, function (data) {
|
||||
let data;
|
||||
|
||||
data = transformData.call({
|
||||
|
||||
}, function (data) {
|
||||
data = 'foo';
|
||||
return data;
|
||||
});
|
||||
})
|
||||
|
||||
expect(data).toEqual('foo');
|
||||
});
|
||||
|
||||
it('should support an array of transformers', function () {
|
||||
var data = '';
|
||||
data = transformData(data, null, null, [function (data) {
|
||||
let data = '';
|
||||
data = transformData.call({data}, [function (data) {
|
||||
data += 'f';
|
||||
return data;
|
||||
}, function (data) {
|
||||
@@ -28,11 +31,11 @@ describe('core::transformData', function () {
|
||||
});
|
||||
|
||||
it('should support reference headers in transformData', function () {
|
||||
var headers = {
|
||||
const headers = {
|
||||
'content-type': 'foo/bar',
|
||||
};
|
||||
var data = '';
|
||||
data = transformData(data, headers, null, [function (data, headers) {
|
||||
let data = '';
|
||||
data = transformData.call({data, headers}, [function (data, headers) {
|
||||
data += headers['content-type'];
|
||||
return data;
|
||||
}]);
|
||||
@@ -41,11 +44,11 @@ describe('core::transformData', function () {
|
||||
});
|
||||
|
||||
it('should support reference status code in transformData', function () {
|
||||
var data = '';
|
||||
data = transformData(data, null, 200, [function (data, headers, status) {
|
||||
let data = '';
|
||||
data = transformData.call({}, [function (data, headers, status) {
|
||||
data += status;
|
||||
return data;
|
||||
}]);
|
||||
}], {data, status: 200});
|
||||
|
||||
expect(data).toEqual('200');
|
||||
});
|
||||
|
||||
+17
-16
@@ -1,8 +1,9 @@
|
||||
var defaults = require('../../lib/defaults');
|
||||
var utils = require('../../lib/utils');
|
||||
import defaults from '../../lib/defaults';
|
||||
import utils from '../../lib/utils';
|
||||
import AxiosHeaders from '../../lib/core/AxiosHeaders';
|
||||
|
||||
describe('defaults', function () {
|
||||
var XSRF_COOKIE_NAME = 'CUSTOM-XSRF-TOKEN';
|
||||
const XSRF_COOKIE_NAME = 'CUSTOM-XSRF-TOKEN';
|
||||
|
||||
beforeEach(function () {
|
||||
jasmine.Ajax.install();
|
||||
@@ -17,13 +18,13 @@ describe('defaults', function () {
|
||||
});
|
||||
|
||||
it('should transform request json', function () {
|
||||
expect(defaults.transformRequest[0]({foo: 'bar'})).toEqual('{"foo":"bar"}');
|
||||
expect(defaults.transformRequest[0]({foo: 'bar'}, new AxiosHeaders())).toEqual('{"foo":"bar"}');
|
||||
});
|
||||
|
||||
it("should also transform request json when 'Content-Type' is 'application/json'", function () {
|
||||
var headers = {
|
||||
const headers = new AxiosHeaders({
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
});
|
||||
expect(defaults.transformRequest[0](JSON.stringify({ foo: 'bar' }), headers)).toEqual('{"foo":"bar"}');
|
||||
expect(defaults.transformRequest[0]([42, 43], headers)).toEqual('[42,43]');
|
||||
expect(defaults.transformRequest[0]('foo', headers)).toEqual('"foo"');
|
||||
@@ -34,23 +35,23 @@ describe('defaults', function () {
|
||||
});
|
||||
|
||||
it("should transform the plain data object to a FormData instance 'Content-Type' if header is 'multipart/form-data'", function() {
|
||||
var headers = {
|
||||
const headers = new AxiosHeaders({
|
||||
'Content-Type': 'multipart/form-data'
|
||||
};
|
||||
});
|
||||
|
||||
var payload = {x: 1};
|
||||
const payload = {x: 1};
|
||||
|
||||
var transformed = defaults.transformRequest[0](payload, headers);
|
||||
const transformed = defaults.transformRequest[0](payload, headers);
|
||||
|
||||
expect(transformed).toEqual(jasmine.any(FormData));
|
||||
});
|
||||
|
||||
it('should do nothing to request string', function () {
|
||||
expect(defaults.transformRequest[0]('foo=bar')).toEqual('foo=bar');
|
||||
expect(defaults.transformRequest[0]('foo=bar', new AxiosHeaders())).toEqual('foo=bar');
|
||||
});
|
||||
|
||||
it('should transform response json', function () {
|
||||
var data = defaults.transformResponse[0].call(defaults, '{"foo":"bar"}');
|
||||
const data = defaults.transformResponse[0].call(defaults, '{"foo":"bar"}');
|
||||
|
||||
expect(typeof data).toEqual('object');
|
||||
expect(data.foo).toEqual('bar');
|
||||
@@ -92,7 +93,7 @@ describe('defaults', function () {
|
||||
});
|
||||
|
||||
it('should use default config for custom instance', function (done) {
|
||||
var instance = axios.create({
|
||||
const instance = axios.create({
|
||||
xsrfCookieName: XSRF_COOKIE_NAME,
|
||||
xsrfHeaderName: 'X-CUSTOM-XSRF-TOKEN'
|
||||
});
|
||||
@@ -127,7 +128,7 @@ describe('defaults', function () {
|
||||
});
|
||||
|
||||
it('should use header config', function (done) {
|
||||
var instance = axios.create({
|
||||
const instance = axios.create({
|
||||
headers: {
|
||||
common: {
|
||||
'X-COMMON-HEADER': 'commonHeaderValue'
|
||||
@@ -163,7 +164,7 @@ describe('defaults', function () {
|
||||
|
||||
it('should be used by custom instance if set before instance created', function (done) {
|
||||
axios.defaults.baseURL = 'http://example.org/';
|
||||
var instance = axios.create();
|
||||
const instance = axios.create();
|
||||
|
||||
instance.get('/foo');
|
||||
|
||||
@@ -174,7 +175,7 @@ describe('defaults', function () {
|
||||
});
|
||||
|
||||
it('should not be used by custom instance if set after instance created', function (done) {
|
||||
var instance = axios.create();
|
||||
const instance = axios.create();
|
||||
axios.defaults.baseURL = 'http://example.org/';
|
||||
|
||||
instance.get('/foo');
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
function testHeaderValue(headers, key, val) {
|
||||
var found = false;
|
||||
let found = false;
|
||||
|
||||
for (var k in headers) {
|
||||
for (const k in headers) {
|
||||
if (k.toLowerCase() === key.toLowerCase()) {
|
||||
found = true;
|
||||
expect(headers[k]).toEqual(val);
|
||||
@@ -28,12 +28,12 @@ describe('headers', function () {
|
||||
});
|
||||
|
||||
it('should default common headers', function (done) {
|
||||
var headers = axios.defaults.headers.common;
|
||||
const headers = axios.defaults.headers.common;
|
||||
|
||||
axios('/foo');
|
||||
|
||||
getAjaxRequest().then(function (request) {
|
||||
for (var key in headers) {
|
||||
for (const key in headers) {
|
||||
if (headers.hasOwnProperty(key)) {
|
||||
expect(request.requestHeaders[key]).toEqual(headers[key]);
|
||||
}
|
||||
@@ -43,12 +43,12 @@ describe('headers', function () {
|
||||
});
|
||||
|
||||
it('should add extra headers for post', function (done) {
|
||||
var headers = axios.defaults.headers.common;
|
||||
const headers = axios.defaults.headers.common;
|
||||
|
||||
axios.post('/foo', 'fizz=buzz');
|
||||
|
||||
getAjaxRequest().then(function (request) {
|
||||
for (var key in headers) {
|
||||
for (const key in headers) {
|
||||
if (headers.hasOwnProperty(key)) {
|
||||
expect(request.requestHeaders[key]).toEqual(headers[key]);
|
||||
}
|
||||
@@ -72,15 +72,17 @@ describe('headers', function () {
|
||||
'x-header-a': null,
|
||||
'x-header-b': undefined
|
||||
}
|
||||
}).catch(function (err) {
|
||||
done(err);
|
||||
});
|
||||
|
||||
getAjaxRequest().then(function (request) {
|
||||
testHeaderValue(request.requestHeaders, 'Content-Type', null);
|
||||
testHeaderValue(request.requestHeaders, 'x-header-a', null);
|
||||
testHeaderValue(request.requestHeaders, 'Content-Type', undefined);
|
||||
testHeaderValue(request.requestHeaders, 'x-header-a', undefined);
|
||||
testHeaderValue(request.requestHeaders, 'x-header-b', undefined);
|
||||
testHeaderValue(request.requestHeaders, 'x-header-c', 'c');
|
||||
done();
|
||||
});
|
||||
}).catch(done);
|
||||
});
|
||||
|
||||
it('should use application/json when posting an object', function (done) {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
var bind = require('../../../lib/helpers/bind');
|
||||
import bind from '../../../lib/helpers/bind';
|
||||
|
||||
describe('bind', function () {
|
||||
it('should bind an object to a function', function () {
|
||||
var o = { val: 123 };
|
||||
var f = bind(function (num) {
|
||||
const o = { val: 123 };
|
||||
const f = bind(function (num) {
|
||||
return this.val * num;
|
||||
}, o);
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
var buildURL = require('../../../lib/helpers/buildURL');
|
||||
var URLSearchParams = require('url-search-params');
|
||||
import buildURL from '../../../lib/helpers/buildURL';
|
||||
import URLSearchParams from 'url-search-params';
|
||||
|
||||
describe('helpers::buildURL', function () {
|
||||
it('should support null params', function () {
|
||||
@@ -21,7 +21,7 @@ describe('helpers::buildURL', function () {
|
||||
});
|
||||
|
||||
it('should support date params', function () {
|
||||
var date = new Date();
|
||||
const date = new Date();
|
||||
|
||||
expect(buildURL('/foo', {
|
||||
date: date
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var combineURLs = require('../../../lib/helpers/combineURLs');
|
||||
import combineURLs from '../../../lib/helpers/combineURLs';
|
||||
|
||||
describe('helpers::combineURLs', function () {
|
||||
it('should combine URLs', function () {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
var cookies = require('../../../lib/helpers/cookies');
|
||||
import cookies from '../../../lib/helpers/cookies';
|
||||
|
||||
describe('helpers::cookies', function () {
|
||||
afterEach(function () {
|
||||
// Remove all the cookies
|
||||
var expires = Date.now() - (60 * 60 * 24 * 7);
|
||||
const expires = Date.now() - (60 * 60 * 24 * 7);
|
||||
document.cookie.split(';').map(function (cookie) {
|
||||
return cookie.split('=')[0];
|
||||
}).forEach(function (name) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var formDataToJSON = require('../../../lib/helpers/formDataToJSON');
|
||||
import formDataToJSON from '../../../lib/helpers/formDataToJSON';
|
||||
|
||||
describe('formDataToJSON', function () {
|
||||
it('should convert a FormData Object to JSON Object', function () {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var isAbsoluteURL = require('../../../lib/helpers/isAbsoluteURL');
|
||||
import isAbsoluteURL from '../../../lib/helpers/isAbsoluteURL';
|
||||
|
||||
describe('helpers::isAbsoluteURL', function () {
|
||||
it('should return true if URL begins with valid scheme name', function () {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
var AxiosError = require('../../../lib/core/AxiosError');
|
||||
var isAxiosError = require('../../../lib/helpers/isAxiosError');
|
||||
import AxiosError from '../../../lib/core/AxiosError';
|
||||
import isAxiosError from '../../../lib/helpers/isAxiosError';
|
||||
|
||||
describe('helpers::isAxiosError', function() {
|
||||
it('should return true if the error is created by core::createError', function() {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var isURLSameOrigin = require('../../../lib/helpers/isURLSameOrigin');
|
||||
import isURLSameOrigin from '../../../lib/helpers/isURLSameOrigin';
|
||||
|
||||
describe('helpers::isURLSameOrigin', function () {
|
||||
it('should detect same origin', function () {
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
var normalizeHeaderName = require('../../../lib/helpers/normalizeHeaderName');
|
||||
|
||||
describe('helpers::normalizeHeaderName', function () {
|
||||
it('should normalize matching header name', function () {
|
||||
var headers = {
|
||||
'conTenT-Type': 'foo/bar',
|
||||
};
|
||||
normalizeHeaderName(headers, 'Content-Type');
|
||||
expect(headers['Content-Type']).toBe('foo/bar');
|
||||
expect(headers['conTenT-Type']).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should not change non-matching header name', function () {
|
||||
var headers = {
|
||||
'content-type': 'foo/bar',
|
||||
};
|
||||
normalizeHeaderName(headers, 'Content-Length');
|
||||
expect(headers['content-type']).toBe('foo/bar');
|
||||
expect(headers['Content-Length']).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -1,9 +1,9 @@
|
||||
var parseHeaders = require('../../../lib/helpers/parseHeaders');
|
||||
import parseHeaders from '../../../lib/helpers/parseHeaders';
|
||||
|
||||
describe('helpers::parseHeaders', function () {
|
||||
it('should parse headers', function () {
|
||||
var date = new Date();
|
||||
var parsed = parseHeaders(
|
||||
const date = new Date();
|
||||
const parsed = parseHeaders(
|
||||
'Date: ' + date.toISOString() + '\n' +
|
||||
'Content-Type: application/json\n' +
|
||||
'Connection: keep-alive\n' +
|
||||
@@ -17,11 +17,11 @@ describe('helpers::parseHeaders', function () {
|
||||
});
|
||||
|
||||
it('should use array for set-cookie', function() {
|
||||
var parsedZero = parseHeaders('');
|
||||
var parsedSingle = parseHeaders(
|
||||
const parsedZero = parseHeaders('');
|
||||
const parsedSingle = parseHeaders(
|
||||
'Set-Cookie: key=val;'
|
||||
);
|
||||
var parsedMulti = parseHeaders(
|
||||
const parsedMulti = parseHeaders(
|
||||
'Set-Cookie: key=val;\n' +
|
||||
'Set-Cookie: key2=val2;\n'
|
||||
);
|
||||
@@ -32,7 +32,7 @@ describe('helpers::parseHeaders', function () {
|
||||
});
|
||||
|
||||
it('should handle duplicates', function() {
|
||||
var parsed = parseHeaders(
|
||||
const parsed = parseHeaders(
|
||||
'Age: age-a\n' + // age is in ignore duplicates blocklist
|
||||
'Age: age-b\n' +
|
||||
'Foo: foo-a\n' +
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
var spread = require('../../../lib/helpers/spread');
|
||||
import spread from '../../../lib/helpers/spread';
|
||||
|
||||
describe('helpers::spread', function () {
|
||||
it('should spread array to arguments', function () {
|
||||
var value = 0;
|
||||
let value = 0;
|
||||
spread(function (a, b) {
|
||||
value = a * b;
|
||||
})([5, 10]);
|
||||
@@ -11,7 +11,7 @@ describe('helpers::spread', function () {
|
||||
});
|
||||
|
||||
it('should return callback result', function () {
|
||||
var value = spread(function (a, b) {
|
||||
const value = spread(function (a, b) {
|
||||
return a * b;
|
||||
})([5, 10]);
|
||||
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
var toFormData = require('../../../lib/helpers/toFormData');
|
||||
import toFormData from '../../../lib/helpers/toFormData';
|
||||
|
||||
describe('toFormData', function () {
|
||||
it('should convert nested data object to FormData with dots option enabled', function () {
|
||||
var o = {
|
||||
const o = {
|
||||
val: 123,
|
||||
nested: {
|
||||
arr: ['hello', 'world']
|
||||
}
|
||||
};
|
||||
|
||||
var form = toFormData(o, null, {dots: true});
|
||||
const form = toFormData(o, null, {dots: true});
|
||||
expect(form instanceof FormData).toEqual(true);
|
||||
expect(Array.from(form.keys()).length).toEqual(3);
|
||||
expect(form.get('val')).toEqual('123');
|
||||
@@ -17,13 +17,13 @@ describe('toFormData', function () {
|
||||
});
|
||||
|
||||
it('should respect metaTokens option', function () {
|
||||
var data = {
|
||||
const data = {
|
||||
'obj{}': {x: 1, y: 2}
|
||||
};
|
||||
|
||||
var str = JSON.stringify(data['obj{}']);
|
||||
const str = JSON.stringify(data['obj{}']);
|
||||
|
||||
var form = toFormData(data, null, {metaTokens: false});
|
||||
const form = toFormData(data, null, {metaTokens: false});
|
||||
|
||||
expect(Array.from(form.keys()).length).toEqual(1);
|
||||
expect(form.getAll('obj')).toEqual([str]);
|
||||
@@ -31,12 +31,12 @@ describe('toFormData', function () {
|
||||
|
||||
describe('Flat arrays serialization', function () {
|
||||
it('should include full indexes when the `indexes` option is set to true', function () {
|
||||
var data = {
|
||||
const data = {
|
||||
arr: [1, 2, 3],
|
||||
arr2: [1, [2], 3]
|
||||
};
|
||||
|
||||
var form = toFormData(data, null, {indexes: true});
|
||||
const form = toFormData(data, null, {indexes: true});
|
||||
|
||||
expect(Array.from(form.keys()).length).toEqual(6);
|
||||
|
||||
@@ -50,12 +50,12 @@ describe('toFormData', function () {
|
||||
});
|
||||
|
||||
it('should include brackets only when the `indexes` option is set to false', function () {
|
||||
var data = {
|
||||
const data = {
|
||||
arr: [1, 2, 3],
|
||||
arr2: [1, [2], 3]
|
||||
};
|
||||
|
||||
var form = toFormData(data, null, {indexes: false});
|
||||
const form = toFormData(data, null, {indexes: false});
|
||||
|
||||
expect(Array.from(form.keys()).length).toEqual(6);
|
||||
|
||||
@@ -67,12 +67,12 @@ describe('toFormData', function () {
|
||||
});
|
||||
|
||||
it('should omit brackets when the `indexes` option is set to null', function () {
|
||||
var data = {
|
||||
const data = {
|
||||
arr: [1, 2, 3],
|
||||
arr2: [1, [2], 3]
|
||||
};
|
||||
|
||||
var form = toFormData(data, null, {indexes: null});
|
||||
const form = toFormData(data, null, {indexes: null});
|
||||
|
||||
expect(Array.from(form.keys()).length).toEqual(6);
|
||||
|
||||
@@ -85,14 +85,14 @@ describe('toFormData', function () {
|
||||
});
|
||||
|
||||
it('should convert nested data object to FormData', function () {
|
||||
var o = {
|
||||
const o = {
|
||||
val: 123,
|
||||
nested: {
|
||||
arr: ['hello', 'world']
|
||||
}
|
||||
};
|
||||
|
||||
var form = toFormData(o);
|
||||
const form = toFormData(o);
|
||||
expect(form instanceof FormData).toEqual(true);
|
||||
expect(Array.from(form.keys()).length).toEqual(3);
|
||||
expect(form.get('val')).toEqual('123');
|
||||
@@ -100,24 +100,24 @@ describe('toFormData', function () {
|
||||
});
|
||||
|
||||
it('should append value whose key ends with [] as separate values with the same key', function () {
|
||||
var data = {
|
||||
const data = {
|
||||
'arr[]': [1, 2, 3]
|
||||
};
|
||||
|
||||
var form = toFormData(data);
|
||||
const form = toFormData(data);
|
||||
|
||||
expect(Array.from(form.keys()).length).toEqual(3);
|
||||
expect(form.getAll('arr[]')).toEqual(['1', '2', '3']);
|
||||
});
|
||||
|
||||
it('should append value whose key ends with {} as a JSON string', function () {
|
||||
var data = {
|
||||
const data = {
|
||||
'obj{}': {x: 1, y: 2}
|
||||
};
|
||||
|
||||
var str = JSON.stringify(data['obj{}']);
|
||||
const str = JSON.stringify(data['obj{}']);
|
||||
|
||||
var form = toFormData(data);
|
||||
const form = toFormData(data);
|
||||
|
||||
expect(Array.from(form.keys()).length).toEqual(1);
|
||||
expect(form.getAll('obj{}')).toEqual([str]);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
var validator = require('../../../lib/helpers/validator');
|
||||
import validator from '../../../lib/helpers/validator';
|
||||
|
||||
describe('validator::assertOptions', function() {
|
||||
it('should throw only if unknown an option was passed', function() {
|
||||
|
||||
+15
-15
@@ -8,9 +8,9 @@ describe('instance', function () {
|
||||
});
|
||||
|
||||
it('should have the same methods as default instance', function () {
|
||||
var instance = axios.create();
|
||||
const instance = axios.create();
|
||||
|
||||
for (var prop in axios) {
|
||||
for (const prop in axios) {
|
||||
if ([
|
||||
'Axios',
|
||||
'AxiosError',
|
||||
@@ -35,7 +35,7 @@ describe('instance', function () {
|
||||
});
|
||||
|
||||
it('should make an http request without verb helper', function (done) {
|
||||
var instance = axios.create();
|
||||
const instance = axios.create();
|
||||
|
||||
instance('/foo');
|
||||
|
||||
@@ -46,7 +46,7 @@ describe('instance', function () {
|
||||
});
|
||||
|
||||
it('should make an http request with url instead of baseURL', function (done) {
|
||||
var instance = axios.create({
|
||||
const instance = axios.create({
|
||||
url: 'https://api.example.com'
|
||||
});
|
||||
|
||||
@@ -59,7 +59,7 @@ describe('instance', function () {
|
||||
});
|
||||
|
||||
it('should make an http request', function (done) {
|
||||
var instance = axios.create();
|
||||
const instance = axios.create();
|
||||
|
||||
instance.get('/foo');
|
||||
|
||||
@@ -70,7 +70,7 @@ describe('instance', function () {
|
||||
});
|
||||
|
||||
it('should use instance options', function (done) {
|
||||
var instance = axios.create({ timeout: 1000 });
|
||||
const instance = axios.create({ timeout: 1000 });
|
||||
|
||||
instance.get('/foo');
|
||||
|
||||
@@ -81,7 +81,7 @@ describe('instance', function () {
|
||||
});
|
||||
|
||||
it('should have defaults.headers', function () {
|
||||
var instance = axios.create({
|
||||
const instance = axios.create({
|
||||
baseURL: 'https://api.example.com'
|
||||
});
|
||||
|
||||
@@ -95,13 +95,13 @@ describe('instance', function () {
|
||||
return config;
|
||||
});
|
||||
|
||||
var instance = axios.create();
|
||||
const instance = axios.create();
|
||||
instance.interceptors.request.use(function (config) {
|
||||
config.bar = true;
|
||||
return config;
|
||||
});
|
||||
|
||||
var response;
|
||||
let response;
|
||||
instance.get('/foo').then(function (res) {
|
||||
response = res;
|
||||
});
|
||||
@@ -120,10 +120,10 @@ describe('instance', function () {
|
||||
});
|
||||
|
||||
it('should have getUri on the instance', function() {
|
||||
var instance = axios.create({
|
||||
const instance = axios.create({
|
||||
baseURL: 'https://api.example.com'
|
||||
});
|
||||
var options = {
|
||||
const options = {
|
||||
url: 'foo/bar',
|
||||
params: {
|
||||
name: 'axios'
|
||||
@@ -133,8 +133,8 @@ describe('instance', function () {
|
||||
});
|
||||
|
||||
it('should correctly build url without baseURL', function () {
|
||||
var instance = axios.create();
|
||||
var options = {
|
||||
const instance = axios.create();
|
||||
const options = {
|
||||
url: 'foo/bar?foo=bar',
|
||||
params: {
|
||||
name: 'axios'
|
||||
@@ -144,8 +144,8 @@ describe('instance', function () {
|
||||
});
|
||||
|
||||
it('should correctly discard url hash mark', function () {
|
||||
var instance = axios.create();
|
||||
var options = {
|
||||
const instance = axios.create();
|
||||
const options = {
|
||||
baseURL: 'https://api.example.com',
|
||||
url: 'foo/bar?foo=bar#hash',
|
||||
params: {
|
||||
|
||||
@@ -10,7 +10,7 @@ describe('interceptors', function () {
|
||||
});
|
||||
|
||||
it('should add a request interceptor (asynchronous by default)', function (done) {
|
||||
var asyncFlag = false;
|
||||
let asyncFlag = false;
|
||||
axios.interceptors.request.use(function (config) {
|
||||
config.headers.test = 'added by interceptor';
|
||||
expect(asyncFlag).toBe(true);
|
||||
@@ -27,7 +27,7 @@ describe('interceptors', function () {
|
||||
});
|
||||
|
||||
it('should add a request interceptor (explicitly flagged as asynchronous)', function (done) {
|
||||
var asyncFlag = false;
|
||||
let asyncFlag = false;
|
||||
axios.interceptors.request.use(function (config) {
|
||||
config.headers.test = 'added by interceptor';
|
||||
expect(asyncFlag).toBe(true);
|
||||
@@ -44,7 +44,7 @@ describe('interceptors', function () {
|
||||
});
|
||||
|
||||
it('should add a request interceptor that is executed synchronously when flag is provided', function (done) {
|
||||
var asyncFlag = false;
|
||||
let asyncFlag = false;
|
||||
axios.interceptors.request.use(function (config) {
|
||||
config.headers.test = 'added by synchronous interceptor';
|
||||
expect(asyncFlag).toBe(false);
|
||||
@@ -61,7 +61,7 @@ describe('interceptors', function () {
|
||||
});
|
||||
|
||||
it('should execute asynchronously when not all interceptors are explicitly flagged as synchronous', function (done) {
|
||||
var asyncFlag = false;
|
||||
let asyncFlag = false;
|
||||
axios.interceptors.request.use(function (config) {
|
||||
config.headers.foo = 'uh oh, async';
|
||||
expect(asyncFlag).toBe(true);
|
||||
@@ -126,7 +126,7 @@ describe('interceptors', function () {
|
||||
});
|
||||
|
||||
it('does not run async interceptor if runWhen function is provided and resolves to false (and run synchronously)', function (done) {
|
||||
var asyncFlag = false;
|
||||
let asyncFlag = false;
|
||||
|
||||
function onPostCall(config) {
|
||||
return config.method === 'post';
|
||||
@@ -153,13 +153,13 @@ describe('interceptors', function () {
|
||||
});
|
||||
|
||||
it('should add a request interceptor with an onRejected block that is called if interceptor code fails', function (done) {
|
||||
var rejectedSpy = jasmine.createSpy('rejectedSpy');
|
||||
var error = new Error('deadly error');
|
||||
const rejectedSpy = jasmine.createSpy('rejectedSpy');
|
||||
const error = new Error('deadly error');
|
||||
axios.interceptors.request.use(function () {
|
||||
throw error;
|
||||
}, rejectedSpy, { synchronous: true });
|
||||
|
||||
axios('/foo');
|
||||
axios('/foo').catch(done);
|
||||
|
||||
getAjaxRequest().then(function () {
|
||||
expect(rejectedSpy).toHaveBeenCalledWith(error);
|
||||
@@ -228,7 +228,7 @@ describe('interceptors', function () {
|
||||
});
|
||||
|
||||
it('should add a response interceptor', function (done) {
|
||||
var response;
|
||||
let response;
|
||||
|
||||
axios.interceptors.response.use(function (data) {
|
||||
data.data = data.data + ' - modified by interceptor';
|
||||
@@ -253,7 +253,7 @@ describe('interceptors', function () {
|
||||
});
|
||||
|
||||
it('should add a response interceptor when request interceptor is defined', function (done) {
|
||||
var response;
|
||||
let response;
|
||||
|
||||
axios.interceptors.request.use(function (data) {
|
||||
return data;
|
||||
@@ -282,7 +282,7 @@ describe('interceptors', function () {
|
||||
});
|
||||
|
||||
it('should add a response interceptor that returns a new data object', function (done) {
|
||||
var response;
|
||||
let response;
|
||||
|
||||
axios.interceptors.response.use(function () {
|
||||
return {
|
||||
@@ -308,7 +308,7 @@ describe('interceptors', function () {
|
||||
});
|
||||
|
||||
it('should add a response interceptor that returns a promise', function (done) {
|
||||
var response;
|
||||
let response;
|
||||
|
||||
axios.interceptors.response.use(function (data) {
|
||||
return new Promise(function (resolve) {
|
||||
@@ -342,7 +342,7 @@ describe('interceptors', function () {
|
||||
describe('and when the response was fulfilled', function () {
|
||||
|
||||
function fireRequestAndExpect(expectation) {
|
||||
var response;
|
||||
let response;
|
||||
axios('/foo').then(function(data) {
|
||||
response = data;
|
||||
});
|
||||
@@ -359,8 +359,8 @@ describe('interceptors', function () {
|
||||
}
|
||||
|
||||
it('then each interceptor is executed', function (done) {
|
||||
var interceptor1 = jasmine.createSpy('interceptor1');
|
||||
var interceptor2 = jasmine.createSpy('interceptor2');
|
||||
const interceptor1 = jasmine.createSpy('interceptor1');
|
||||
const interceptor2 = jasmine.createSpy('interceptor2');
|
||||
axios.interceptors.response.use(interceptor1);
|
||||
axios.interceptors.response.use(interceptor2);
|
||||
|
||||
@@ -372,8 +372,8 @@ describe('interceptors', function () {
|
||||
});
|
||||
|
||||
it('then they are executed in the order they were added', function (done) {
|
||||
var interceptor1 = jasmine.createSpy('interceptor1');
|
||||
var interceptor2 = jasmine.createSpy('interceptor2');
|
||||
const interceptor1 = jasmine.createSpy('interceptor1');
|
||||
const interceptor2 = jasmine.createSpy('interceptor2');
|
||||
axios.interceptors.response.use(interceptor1);
|
||||
axios.interceptors.response.use(interceptor2);
|
||||
|
||||
@@ -433,7 +433,7 @@ describe('interceptors', function () {
|
||||
axios.interceptors.response.use(function() {
|
||||
throw Error('throwing interceptor');
|
||||
});
|
||||
var interceptor2 = jasmine.createSpy('interceptor2');
|
||||
const interceptor2 = jasmine.createSpy('interceptor2');
|
||||
axios.interceptors.response.use(interceptor2);
|
||||
|
||||
fireRequestCatchAndExpect(function () {
|
||||
@@ -446,8 +446,8 @@ describe('interceptors', function () {
|
||||
axios.interceptors.response.use(function() {
|
||||
throw Error('throwing interceptor');
|
||||
});
|
||||
var unusedFulfillInterceptor = function() {};
|
||||
var rejectIntercept = jasmine.createSpy('rejectIntercept');
|
||||
const unusedFulfillInterceptor = function() {};
|
||||
const rejectIntercept = jasmine.createSpy('rejectIntercept');
|
||||
axios.interceptors.response.use(unusedFulfillInterceptor, rejectIntercept);
|
||||
|
||||
fireRequestCatchAndExpect(function () {
|
||||
@@ -455,17 +455,17 @@ describe('interceptors', function () {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('once caught, another following fulfill-interceptor is called again (just like in a promise chain)', function (done) {
|
||||
axios.interceptors.response.use(function() {
|
||||
throw Error('throwing interceptor');
|
||||
});
|
||||
|
||||
var unusedFulfillInterceptor = function() {};
|
||||
var catchingThrowingInterceptor = function() {};
|
||||
const unusedFulfillInterceptor = function() {};
|
||||
const catchingThrowingInterceptor = function() {};
|
||||
axios.interceptors.response.use(unusedFulfillInterceptor, catchingThrowingInterceptor);
|
||||
|
||||
var interceptor3 = jasmine.createSpy('interceptor3');
|
||||
const interceptor3 = jasmine.createSpy('interceptor3');
|
||||
axios.interceptors.response.use(interceptor3);
|
||||
|
||||
fireRequestCatchAndExpect(function () {
|
||||
@@ -478,7 +478,7 @@ describe('interceptors', function () {
|
||||
});
|
||||
|
||||
it('should allow removing interceptors', function (done) {
|
||||
var response, intercept;
|
||||
let response, intercept;
|
||||
|
||||
axios.interceptors.response.use(function (data) {
|
||||
data.data = data.data + '1';
|
||||
@@ -513,13 +513,13 @@ describe('interceptors', function () {
|
||||
});
|
||||
|
||||
it('should remove async interceptor before making request and execute synchronously', function (done) {
|
||||
var asyncFlag = false;
|
||||
var asyncIntercept = axios.interceptors.request.use(function (config) {
|
||||
let asyncFlag = false;
|
||||
const asyncIntercept = axios.interceptors.request.use(function (config) {
|
||||
config.headers.async = 'async it!';
|
||||
return config;
|
||||
}, null, { synchronous: false });
|
||||
|
||||
var syncIntercept = axios.interceptors.request.use(function (config) {
|
||||
const syncIntercept = axios.interceptors.request.use(function (config) {
|
||||
config.headers.sync = 'hello world';
|
||||
expect(asyncFlag).toBe(false);
|
||||
return config;
|
||||
@@ -555,7 +555,7 @@ describe('interceptors', function () {
|
||||
});
|
||||
|
||||
it('should modify base URL in request interceptor', function (done) {
|
||||
var instance = axios.create({
|
||||
const instance = axios.create({
|
||||
baseURL: 'http://test.com/'
|
||||
});
|
||||
|
||||
@@ -573,7 +573,7 @@ describe('interceptors', function () {
|
||||
});
|
||||
|
||||
it('should clear all request interceptors', function () {
|
||||
var instance = axios.create({
|
||||
const instance = axios.create({
|
||||
baseURL: 'http://test.com/'
|
||||
});
|
||||
|
||||
@@ -587,7 +587,7 @@ describe('interceptors', function () {
|
||||
});
|
||||
|
||||
it('should clear all response interceptors', function () {
|
||||
var instance = axios.create({
|
||||
const instance = axios.create({
|
||||
baseURL: 'http://test.com/'
|
||||
});
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ describe('options', function () {
|
||||
});
|
||||
|
||||
it('should accept base URL', function (done) {
|
||||
var instance = axios.create({
|
||||
const instance = axios.create({
|
||||
baseURL: 'http://test.com/'
|
||||
});
|
||||
|
||||
@@ -70,7 +70,7 @@ describe('options', function () {
|
||||
});
|
||||
|
||||
it('should ignore base URL if request URL is absolute', function (done) {
|
||||
var instance = axios.create({
|
||||
const instance = axios.create({
|
||||
baseURL: 'http://someurl.com/'
|
||||
});
|
||||
|
||||
@@ -83,8 +83,8 @@ describe('options', function () {
|
||||
});
|
||||
|
||||
it('should change only the baseURL of the specified instance', function() {
|
||||
var instance1 = axios.create();
|
||||
var instance2 = axios.create();
|
||||
const instance1 = axios.create();
|
||||
const instance2 = axios.create();
|
||||
|
||||
instance1.defaults.baseURL = 'http://instance1.example.com/';
|
||||
|
||||
@@ -92,8 +92,8 @@ describe('options', function () {
|
||||
});
|
||||
|
||||
it('should change only the headers of the specified instance', function() {
|
||||
var instance1 = axios.create();
|
||||
var instance2 = axios.create();
|
||||
const instance1 = axios.create();
|
||||
const instance2 = axios.create();
|
||||
|
||||
instance1.defaults.headers.common.Authorization = 'faketoken';
|
||||
instance2.defaults.headers.common.Authorization = 'differentfaketoken';
|
||||
|
||||
+11
-11
@@ -8,7 +8,7 @@ describe('progress events', function () {
|
||||
});
|
||||
|
||||
it('should add a download progress handler', function (done) {
|
||||
var progressSpy = jasmine.createSpy('progress');
|
||||
const progressSpy = jasmine.createSpy('progress');
|
||||
|
||||
axios('/foo', { onDownloadProgress: progressSpy } );
|
||||
|
||||
@@ -23,7 +23,7 @@ describe('progress events', function () {
|
||||
});
|
||||
|
||||
it('should add a upload progress handler', function (done) {
|
||||
var progressSpy = jasmine.createSpy('progress');
|
||||
const progressSpy = jasmine.createSpy('progress');
|
||||
|
||||
axios('/foo', { onUploadProgress: progressSpy } );
|
||||
|
||||
@@ -35,8 +35,8 @@ describe('progress events', function () {
|
||||
});
|
||||
|
||||
it('should add both upload and download progress handlers', function (done) {
|
||||
var downloadProgressSpy = jasmine.createSpy('downloadProgress');
|
||||
var uploadProgressSpy = jasmine.createSpy('uploadProgress');
|
||||
const downloadProgressSpy = jasmine.createSpy('downloadProgress');
|
||||
const uploadProgressSpy = jasmine.createSpy('uploadProgress');
|
||||
|
||||
axios('/foo', { onDownloadProgress: downloadProgressSpy, onUploadProgress: uploadProgressSpy });
|
||||
|
||||
@@ -53,9 +53,9 @@ describe('progress events', function () {
|
||||
});
|
||||
|
||||
it('should add a download progress handler from instance config', function (done) {
|
||||
var progressSpy = jasmine.createSpy('progress');
|
||||
const progressSpy = jasmine.createSpy('progress');
|
||||
|
||||
var instance = axios.create({
|
||||
const instance = axios.create({
|
||||
onDownloadProgress: progressSpy,
|
||||
});
|
||||
|
||||
@@ -72,9 +72,9 @@ describe('progress events', function () {
|
||||
});
|
||||
|
||||
it('should add a upload progress handler from instance config', function (done) {
|
||||
var progressSpy = jasmine.createSpy('progress');
|
||||
const progressSpy = jasmine.createSpy('progress');
|
||||
|
||||
var instance = axios.create({
|
||||
const instance = axios.create({
|
||||
onUploadProgress: progressSpy,
|
||||
});
|
||||
|
||||
@@ -87,10 +87,10 @@ describe('progress events', function () {
|
||||
});
|
||||
|
||||
it('should add upload and download progress handlers from instance config', function (done) {
|
||||
var downloadProgressSpy = jasmine.createSpy('downloadProgress');
|
||||
var uploadProgressSpy = jasmine.createSpy('uploadProgress');
|
||||
const downloadProgressSpy = jasmine.createSpy('downloadProgress');
|
||||
const uploadProgressSpy = jasmine.createSpy('uploadProgress');
|
||||
|
||||
var instance = axios.create({
|
||||
const instance = axios.create({
|
||||
onDownloadProgress: downloadProgressSpy,
|
||||
onUploadProgress: uploadProgressSpy,
|
||||
});
|
||||
|
||||
@@ -8,7 +8,7 @@ describe('promise', function () {
|
||||
});
|
||||
|
||||
it('should provide succinct object to then', function (done) {
|
||||
var response;
|
||||
let response;
|
||||
|
||||
axios('/foo').then(function (r) {
|
||||
response = r;
|
||||
@@ -32,7 +32,7 @@ describe('promise', function () {
|
||||
});
|
||||
|
||||
it('should support all', function (done) {
|
||||
var fulfilled = false;
|
||||
let fulfilled = false;
|
||||
|
||||
axios.all([true, 123]).then(function () {
|
||||
fulfilled = true;
|
||||
@@ -45,9 +45,9 @@ describe('promise', function () {
|
||||
});
|
||||
|
||||
it('should support spread', function (done) {
|
||||
var sum = 0;
|
||||
var fulfilled = false;
|
||||
var result;
|
||||
let sum = 0;
|
||||
let fulfilled = false;
|
||||
let result;
|
||||
|
||||
axios
|
||||
.all([123, 456])
|
||||
@@ -58,7 +58,7 @@ describe('promise', function () {
|
||||
}))
|
||||
.then(function (res) {
|
||||
result = res;
|
||||
});
|
||||
}).catch(done);
|
||||
|
||||
setTimeout(function () {
|
||||
expect(fulfilled).toEqual(true);
|
||||
|
||||
+37
-37
@@ -112,13 +112,13 @@ describe('requests', function () {
|
||||
// disable jasmine.Ajax since we're hitting a non-existent server anyway
|
||||
jasmine.Ajax.uninstall();
|
||||
|
||||
var resolveSpy = jasmine.createSpy('resolve');
|
||||
var rejectSpy = jasmine.createSpy('reject');
|
||||
const resolveSpy = jasmine.createSpy('resolve');
|
||||
const rejectSpy = jasmine.createSpy('reject');
|
||||
|
||||
var finish = function () {
|
||||
const finish = function () {
|
||||
expect(resolveSpy).not.toHaveBeenCalled();
|
||||
expect(rejectSpy).toHaveBeenCalled();
|
||||
var reason = rejectSpy.calls.first().args[0];
|
||||
const reason = rejectSpy.calls.first().args[0];
|
||||
expect(reason instanceof Error).toBe(true);
|
||||
expect(reason.config.method).toBe('get');
|
||||
expect(reason.config.url).toBe('http://thisisnotaserver/foo');
|
||||
@@ -136,13 +136,13 @@ describe('requests', function () {
|
||||
});
|
||||
|
||||
it('should reject on abort', function (done) {
|
||||
var resolveSpy = jasmine.createSpy('resolve');
|
||||
var rejectSpy = jasmine.createSpy('reject');
|
||||
const resolveSpy = jasmine.createSpy('resolve');
|
||||
const rejectSpy = jasmine.createSpy('reject');
|
||||
|
||||
var finish = function () {
|
||||
const finish = function () {
|
||||
expect(resolveSpy).not.toHaveBeenCalled();
|
||||
expect(rejectSpy).toHaveBeenCalled();
|
||||
var reason = rejectSpy.calls.first().args[0];
|
||||
const reason = rejectSpy.calls.first().args[0];
|
||||
expect(reason instanceof Error).toBe(true);
|
||||
expect(reason.config.method).toBe('get');
|
||||
expect(reason.config.url).toBe('/foo');
|
||||
@@ -161,8 +161,8 @@ describe('requests', function () {
|
||||
});
|
||||
|
||||
it('should reject when validateStatus returns false', function (done) {
|
||||
var resolveSpy = jasmine.createSpy('resolve');
|
||||
var rejectSpy = jasmine.createSpy('reject');
|
||||
const resolveSpy = jasmine.createSpy('resolve');
|
||||
const rejectSpy = jasmine.createSpy('reject');
|
||||
|
||||
axios('/foo', {
|
||||
validateStatus: function (status) {
|
||||
@@ -173,7 +173,7 @@ describe('requests', function () {
|
||||
.then(function () {
|
||||
expect(resolveSpy).not.toHaveBeenCalled();
|
||||
expect(rejectSpy).toHaveBeenCalled();
|
||||
var reason = rejectSpy.calls.first().args[0];
|
||||
const reason = rejectSpy.calls.first().args[0];
|
||||
expect(reason instanceof Error).toBe(true);
|
||||
expect(reason.message).toBe('Request failed with status code 500');
|
||||
expect(reason.config.method).toBe('get');
|
||||
@@ -191,8 +191,8 @@ describe('requests', function () {
|
||||
});
|
||||
|
||||
it('should resolve when validateStatus returns true', function (done) {
|
||||
var resolveSpy = jasmine.createSpy('resolve');
|
||||
var rejectSpy = jasmine.createSpy('reject');
|
||||
const resolveSpy = jasmine.createSpy('resolve');
|
||||
const rejectSpy = jasmine.createSpy('reject');
|
||||
|
||||
axios('/foo', {
|
||||
validateStatus: function (status) {
|
||||
@@ -214,8 +214,8 @@ describe('requests', function () {
|
||||
});
|
||||
|
||||
it('should resolve when the response status is 0 (i.e. requesting with file protocol)', function (done) {
|
||||
var resolveSpy = jasmine.createSpy('resolve');
|
||||
var rejectSpy = jasmine.createSpy('reject');
|
||||
const resolveSpy = jasmine.createSpy('resolve');
|
||||
const rejectSpy = jasmine.createSpy('reject');
|
||||
|
||||
axios('file:///xxx').then(resolveSpy)
|
||||
.catch(rejectSpy)
|
||||
@@ -234,8 +234,8 @@ describe('requests', function () {
|
||||
});
|
||||
|
||||
it('should resolve when validateStatus is null', function (done) {
|
||||
var resolveSpy = jasmine.createSpy('resolve');
|
||||
var rejectSpy = jasmine.createSpy('reject');
|
||||
const resolveSpy = jasmine.createSpy('resolve');
|
||||
const rejectSpy = jasmine.createSpy('reject');
|
||||
|
||||
axios('/foo', {
|
||||
validateStatus: null
|
||||
@@ -255,8 +255,8 @@ describe('requests', function () {
|
||||
});
|
||||
|
||||
it('should resolve when validateStatus is undefined', function (done) {
|
||||
var resolveSpy = jasmine.createSpy('resolve');
|
||||
var rejectSpy = jasmine.createSpy('reject');
|
||||
const resolveSpy = jasmine.createSpy('resolve');
|
||||
const rejectSpy = jasmine.createSpy('reject');
|
||||
|
||||
axios('/foo', {
|
||||
validateStatus: undefined
|
||||
@@ -277,7 +277,7 @@ describe('requests', function () {
|
||||
|
||||
// https://github.com/axios/axios/issues/378
|
||||
it('should return JSON when rejecting', function (done) {
|
||||
var response;
|
||||
let response;
|
||||
|
||||
axios('/api/account/signup', {
|
||||
username: null,
|
||||
@@ -309,7 +309,7 @@ describe('requests', function () {
|
||||
});
|
||||
|
||||
it('should make cross domain http request', function (done) {
|
||||
var response;
|
||||
let response;
|
||||
|
||||
axios.post('www.someurl.com/foo').then(function(res){
|
||||
response = res;
|
||||
@@ -337,7 +337,7 @@ describe('requests', function () {
|
||||
|
||||
|
||||
it('should supply correct response', function (done) {
|
||||
var response;
|
||||
let response;
|
||||
|
||||
axios.post('/foo').then(function (res) {
|
||||
response = res;
|
||||
@@ -364,7 +364,7 @@ describe('requests', function () {
|
||||
});
|
||||
|
||||
it('should not modify the config url with relative baseURL', function (done) {
|
||||
var config;
|
||||
let config;
|
||||
|
||||
axios.get('/foo', {
|
||||
baseURL: '/api'
|
||||
@@ -388,12 +388,12 @@ describe('requests', function () {
|
||||
});
|
||||
|
||||
it('should allow overriding Content-Type header case-insensitive', function (done) {
|
||||
var response;
|
||||
var contentType = 'application/vnd.myapp.type+json';
|
||||
let response;
|
||||
const contentType = 'application/vnd.myapp.type+json';
|
||||
|
||||
axios.post('/foo', { prop: 'value' }, {
|
||||
headers: {
|
||||
'content-type': contentType
|
||||
'Content-Type': contentType
|
||||
}
|
||||
}).then(function (res) {
|
||||
response = res;
|
||||
@@ -406,14 +406,14 @@ describe('requests', function () {
|
||||
});
|
||||
|
||||
it('should support binary data as array buffer', function (done) {
|
||||
var input = new Int8Array(2);
|
||||
const input = new Int8Array(2);
|
||||
input[0] = 1;
|
||||
input[1] = 2;
|
||||
|
||||
axios.post('/foo', input.buffer);
|
||||
|
||||
getAjaxRequest().then(function (request) {
|
||||
var output = new Int8Array(request.params);
|
||||
const output = new Int8Array(request.params);
|
||||
expect(output.length).toEqual(2);
|
||||
expect(output[0]).toEqual(1);
|
||||
expect(output[1]).toEqual(2);
|
||||
@@ -422,14 +422,14 @@ describe('requests', function () {
|
||||
});
|
||||
|
||||
it('should support binary data as array buffer view', function (done) {
|
||||
var input = new Int8Array(2);
|
||||
const input = new Int8Array(2);
|
||||
input[0] = 1;
|
||||
input[1] = 2;
|
||||
|
||||
axios.post('/foo', input);
|
||||
|
||||
getAjaxRequest().then(function (request) {
|
||||
var output = new Int8Array(request.params);
|
||||
const output = new Int8Array(request.params);
|
||||
expect(output.length).toEqual(2);
|
||||
expect(output[0]).toEqual(1);
|
||||
expect(output[1]).toEqual(2);
|
||||
@@ -438,12 +438,12 @@ describe('requests', function () {
|
||||
});
|
||||
|
||||
it('should support array buffer response', function (done) {
|
||||
var response;
|
||||
let response;
|
||||
|
||||
function str2ab(str) {
|
||||
var buff = new ArrayBuffer(str.length * 2);
|
||||
var view = new Uint16Array(buff);
|
||||
for ( var i=0, l=str.length; i<l; i++) {
|
||||
const buff = new ArrayBuffer(str.length * 2);
|
||||
const view = new Uint16Array(buff);
|
||||
for ( let i=0, l=str.length; i<l; i++) {
|
||||
view[i] = str.charCodeAt(i);
|
||||
}
|
||||
return buff;
|
||||
@@ -469,7 +469,7 @@ describe('requests', function () {
|
||||
});
|
||||
|
||||
it('should support URLSearchParams', function (done) {
|
||||
var params = new URLSearchParams();
|
||||
const params = new URLSearchParams();
|
||||
params.append('param1', 'value1');
|
||||
params.append('param2', 'value2');
|
||||
|
||||
@@ -483,7 +483,7 @@ describe('requests', function () {
|
||||
});
|
||||
|
||||
it('should support HTTP protocol', function (done) {
|
||||
var response;
|
||||
let response;
|
||||
|
||||
axios.get('/foo')
|
||||
.then(function (res) {
|
||||
@@ -500,7 +500,7 @@ describe('requests', function () {
|
||||
});
|
||||
|
||||
it('should support HTTPS protocol', function (done) {
|
||||
var response;
|
||||
let response;
|
||||
axios.get('https://www.google.com')
|
||||
.then(function (res) {
|
||||
response = res
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var AxiosError = require("../../lib/core/AxiosError");
|
||||
import AxiosError from "../../lib/core/AxiosError";
|
||||
|
||||
describe('transform', function () {
|
||||
beforeEach(function () {
|
||||
@@ -10,7 +10,7 @@ describe('transform', function () {
|
||||
});
|
||||
|
||||
it('should transform JSON to string', function (done) {
|
||||
var data = {
|
||||
const data = {
|
||||
foo: 'bar'
|
||||
};
|
||||
|
||||
@@ -23,7 +23,7 @@ describe('transform', function () {
|
||||
});
|
||||
|
||||
it('should transform string to JSON', function (done) {
|
||||
var response;
|
||||
let response;
|
||||
|
||||
axios('/foo').then(function (data) {
|
||||
response = data;
|
||||
@@ -45,7 +45,7 @@ describe('transform', function () {
|
||||
|
||||
it('should throw a SyntaxError if JSON parsing failed and responseType is "json" if silentJSONParsing is false',
|
||||
function (done) {
|
||||
var thrown;
|
||||
let thrown;
|
||||
|
||||
axios({
|
||||
url: '/foo',
|
||||
@@ -74,7 +74,7 @@ describe('transform', function () {
|
||||
);
|
||||
|
||||
it('should send data as JSON if request content-type is application/json', function (done) {
|
||||
var response;
|
||||
let response;
|
||||
|
||||
axios.post('/foo', 123, {headers: {'Content-Type': 'application/json'}}).then(function (_response) {
|
||||
response = _response;
|
||||
@@ -98,7 +98,7 @@ describe('transform', function () {
|
||||
});
|
||||
|
||||
it('should not assume JSON if responseType is not `json`', function (done) {
|
||||
var response;
|
||||
let response;
|
||||
|
||||
axios.get('/foo', {
|
||||
responseType: 'text',
|
||||
@@ -111,7 +111,7 @@ describe('transform', function () {
|
||||
done(err);
|
||||
});
|
||||
|
||||
var rawData = '{"x":1}';
|
||||
const rawData = '{"x":1}';
|
||||
|
||||
getAjaxRequest().then(function (request) {
|
||||
request.respondWith({
|
||||
@@ -128,7 +128,7 @@ describe('transform', function () {
|
||||
});
|
||||
|
||||
it('should override default transform', function (done) {
|
||||
var data = {
|
||||
const data = {
|
||||
foo: 'bar'
|
||||
};
|
||||
|
||||
@@ -145,7 +145,7 @@ describe('transform', function () {
|
||||
});
|
||||
|
||||
it('should allow an Array of transformers', function (done) {
|
||||
var data = {
|
||||
const data = {
|
||||
foo: 'bar'
|
||||
};
|
||||
|
||||
@@ -164,7 +164,7 @@ describe('transform', function () {
|
||||
});
|
||||
|
||||
it('should allowing mutating headers', function (done) {
|
||||
var token = Math.floor(Math.random() * Math.pow(2, 64)).toString(36);
|
||||
const token = Math.floor(Math.random() * Math.pow(2, 64)).toString(36);
|
||||
|
||||
axios('/foo', {
|
||||
transformRequest: function (data, headers) {
|
||||
@@ -179,7 +179,7 @@ describe('transform', function () {
|
||||
});
|
||||
|
||||
it('should normalize \'content-type\' header when using a custom transformRequest', function (done) {
|
||||
var data = {
|
||||
const data = {
|
||||
foo: 'bar'
|
||||
};
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
var kindOf = require('../../../lib/utils').kindOf;
|
||||
import utils from '../../../lib/utils';
|
||||
|
||||
const {kindOf} = utils;
|
||||
|
||||
describe('utils::kindOf', function () {
|
||||
it('should return object tag', function () {
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
var extend = require('../../../lib/utils').extend;
|
||||
import utils from '../../../lib/utils';
|
||||
|
||||
const {extend} = utils;
|
||||
|
||||
describe('utils::extend', function () {
|
||||
it('should be mutable', function () {
|
||||
var a = {};
|
||||
var b = {foo: 123};
|
||||
const a = {};
|
||||
const b = {foo: 123};
|
||||
|
||||
extend(a, b);
|
||||
|
||||
expect(a.foo).toEqual(b.foo);
|
||||
});
|
||||
|
||||
|
||||
it('should extend properties', function () {
|
||||
var a = {foo: 123, bar: 456};
|
||||
var b = {bar: 789};
|
||||
let a = {foo: 123, bar: 456};
|
||||
const b = {bar: 789};
|
||||
|
||||
a = extend(a, b);
|
||||
|
||||
@@ -21,9 +23,9 @@ describe('utils::extend', function () {
|
||||
});
|
||||
|
||||
it('should bind to thisArg', function () {
|
||||
var a = {};
|
||||
var b = {getFoo: function getFoo() { return this.foo; }};
|
||||
var thisArg = { foo: 'barbaz' };
|
||||
const a = {};
|
||||
const b = {getFoo: function getFoo() { return this.foo; }};
|
||||
const thisArg = { foo: 'barbaz' };
|
||||
|
||||
extend(a, b, thisArg);
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
var forEach = require('../../../lib/utils').forEach;
|
||||
import utils from '../../../lib/utils';
|
||||
|
||||
const {forEach} = utils;
|
||||
|
||||
describe('utils::forEach', function () {
|
||||
it('should loop over an array', function () {
|
||||
var sum = 0;
|
||||
let sum = 0;
|
||||
|
||||
forEach([1, 2, 3, 4, 5], function (val) {
|
||||
sum += val;
|
||||
@@ -12,9 +14,9 @@ describe('utils::forEach', function () {
|
||||
});
|
||||
|
||||
it('should loop over object keys', function () {
|
||||
var keys = '';
|
||||
var vals = 0;
|
||||
var obj = {
|
||||
let keys = '';
|
||||
let vals = 0;
|
||||
const obj = {
|
||||
b: 1,
|
||||
a: 2,
|
||||
r: 3
|
||||
@@ -30,7 +32,7 @@ describe('utils::forEach', function () {
|
||||
});
|
||||
|
||||
it('should handle undefined gracefully', function () {
|
||||
var count = 0;
|
||||
let count = 0;
|
||||
|
||||
forEach(undefined, function () {
|
||||
count++;
|
||||
@@ -40,7 +42,7 @@ describe('utils::forEach', function () {
|
||||
});
|
||||
|
||||
it('should make an array out of non-array argument', function () {
|
||||
var count = 0;
|
||||
let count = 0;
|
||||
|
||||
forEach(function () {}, function () {
|
||||
count++;
|
||||
@@ -50,8 +52,8 @@ describe('utils::forEach', function () {
|
||||
});
|
||||
|
||||
it('should handle non object prototype gracefully', function () {
|
||||
var count = 0;
|
||||
var data = Object.create(null);
|
||||
let count = 0;
|
||||
const data = Object.create(null);
|
||||
data.foo = 'bar'
|
||||
|
||||
forEach(data, function () {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
var utils = require('../../../lib/utils');
|
||||
var Stream = require('stream');
|
||||
import utils from '../../../lib/utils';
|
||||
|
||||
describe('utils::isX', function () {
|
||||
it('should validate Array', function () {
|
||||
@@ -7,12 +6,6 @@ describe('utils::isX', function () {
|
||||
expect(utils.isArray({length: 5})).toEqual(false);
|
||||
});
|
||||
|
||||
it('should validate Buffer', function () {
|
||||
expect(utils.isBuffer(Buffer.from('a'))).toEqual(true);
|
||||
expect(utils.isBuffer(null)).toEqual(false);
|
||||
expect(utils.isBuffer(undefined)).toEqual(false);
|
||||
});
|
||||
|
||||
it('should validate ArrayBuffer', function () {
|
||||
expect(utils.isArrayBuffer(new ArrayBuffer(2))).toEqual(true);
|
||||
expect(utils.isArrayBuffer({})).toEqual(false);
|
||||
@@ -68,11 +61,6 @@ describe('utils::isX', function () {
|
||||
expect(utils.isFunction('function')).toEqual(false);
|
||||
});
|
||||
|
||||
it('should validate Stream', function () {
|
||||
expect(utils.isStream(new Stream.Readable())).toEqual(true);
|
||||
expect(utils.isStream({ foo: 'bar' })).toEqual(false);
|
||||
});
|
||||
|
||||
it('should validate URLSearchParams', function () {
|
||||
expect(utils.isURLSearchParams(new URLSearchParams())).toEqual(true);
|
||||
expect(utils.isURLSearchParams('foo=1&bar=2')).toEqual(false);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
var kindOfTest = require('../../../lib/utils').kindOfTest;
|
||||
import {kindOfTest} from '../../../lib/utils';
|
||||
|
||||
describe('utils::endsWith', function () {
|
||||
it('should return true if the string ends with passed substring', function () {
|
||||
var test = kindOfTest('number');
|
||||
const test = kindOfTest('number');
|
||||
|
||||
expect(test(123)).toEqual(true);
|
||||
expect(test('123')).toEqual(false);
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
var kindOf = require('../../../lib/utils').kindOf;
|
||||
import utils from '../../../lib/utils';
|
||||
|
||||
const {kindOf} = utils;
|
||||
|
||||
describe('utils::kindOf', function () {
|
||||
it('should return object tag', function () {
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
var merge = require('../../../lib/utils').merge;
|
||||
import utils from '../../../lib/utils';
|
||||
|
||||
const {merge} = utils;
|
||||
|
||||
describe('utils::merge', function () {
|
||||
it('should be immutable', function () {
|
||||
var a = {};
|
||||
var b = {foo: 123};
|
||||
var c = {bar: 456};
|
||||
const a = {};
|
||||
const b = {foo: 123};
|
||||
const c = {bar: 456};
|
||||
|
||||
merge(a, b, c);
|
||||
|
||||
@@ -13,21 +15,21 @@ describe('utils::merge', function () {
|
||||
expect(typeof b.bar).toEqual('undefined');
|
||||
expect(typeof c.foo).toEqual('undefined');
|
||||
});
|
||||
|
||||
|
||||
it('should merge properties', function () {
|
||||
var a = {foo: 123};
|
||||
var b = {bar: 456};
|
||||
var c = {foo: 789};
|
||||
var d = merge(a, b, c);
|
||||
const a = {foo: 123};
|
||||
const b = {bar: 456};
|
||||
const c = {foo: 789};
|
||||
const d = merge(a, b, c);
|
||||
|
||||
expect(d.foo).toEqual(789);
|
||||
expect(d.bar).toEqual(456);
|
||||
});
|
||||
|
||||
it('should merge recursively', function () {
|
||||
var a = {foo: {bar: 123}};
|
||||
var b = {foo: {baz: 456}, bar: {qux: 789}};
|
||||
|
||||
const a = {foo: {bar: 123}};
|
||||
const b = {foo: {baz: 456}, bar: {qux: 789}};
|
||||
|
||||
expect(merge(a, b)).toEqual({
|
||||
foo: {
|
||||
bar: 123,
|
||||
@@ -40,9 +42,9 @@ describe('utils::merge', function () {
|
||||
});
|
||||
|
||||
it('should remove all references from nested objects', function () {
|
||||
var a = {foo: {bar: 123}};
|
||||
var b = {};
|
||||
var d = merge(a, b);
|
||||
const a = {foo: {bar: 123}};
|
||||
const b = {};
|
||||
const d = merge(a, b);
|
||||
|
||||
expect(d).toEqual({
|
||||
foo: {
|
||||
@@ -75,8 +77,8 @@ describe('utils::merge', function () {
|
||||
});
|
||||
|
||||
it('should replace properties with cloned arrays', function () {
|
||||
var a = [1, 2, 3];
|
||||
var d = merge({}, {a: a});
|
||||
const a = [1, 2, 3];
|
||||
const d = merge({}, {a: a});
|
||||
|
||||
expect(d).toEqual({a: [1, 2, 3]});
|
||||
expect(d.a).not.toBe(a);
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
var toArray = require('../../../lib/utils').toArray;
|
||||
import utils from '../../../lib/utils';
|
||||
|
||||
const {toArray} = utils;
|
||||
|
||||
describe('utils::kindOf', function () {
|
||||
it('should return object tag', function () {
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
var toFlatObject = require('../../../lib/utils').toFlatObject;
|
||||
import utils from '../../../lib/utils';
|
||||
|
||||
const {toFlatObject} = utils;
|
||||
|
||||
describe('utils::toFlatObject', function () {
|
||||
it('should resolve object proto chain to a flat object representation', function () {
|
||||
var a = {x: 1};
|
||||
var b = Object.create(a, {y: {value: 2}});
|
||||
var c = Object.create(b, {z: {value: 3}});
|
||||
const a = {x: 1};
|
||||
const b = Object.create(a, {y: {value: 2}});
|
||||
const c = Object.create(b, {z: {value: 3}});
|
||||
expect(toFlatObject(c)).toEqual({x: 1, y: 2, z: 3});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
var trim = require('../../../lib/utils').trim;
|
||||
import utils from '../../../lib/utils';
|
||||
|
||||
describe('utils::trim', function () {
|
||||
it('should trim spaces', function () {
|
||||
expect(trim(' foo ')).toEqual('foo');
|
||||
expect(utils.trim(' foo ')).toEqual('foo');
|
||||
});
|
||||
|
||||
it('should trim tabs', function () {
|
||||
expect(trim('\tfoo\t')).toEqual('foo');
|
||||
expect(utils.trim('\tfoo\t')).toEqual('foo');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var cookies = require('../../lib/helpers/cookies');
|
||||
import cookies from '../../lib/helpers/cookies';
|
||||
|
||||
describe('xsrf', function () {
|
||||
beforeEach(function () {
|
||||
|
||||
@@ -7,7 +7,7 @@ import axios, {
|
||||
Cancel,
|
||||
CancelToken,
|
||||
CancelTokenSource,
|
||||
Canceler
|
||||
Canceler, AxiosProgressEvent
|
||||
} from 'axios';
|
||||
|
||||
const config: AxiosRequestConfig = {
|
||||
@@ -34,8 +34,8 @@ const config: AxiosRequestConfig = {
|
||||
responseType: 'json',
|
||||
xsrfCookieName: 'XSRF-TOKEN',
|
||||
xsrfHeaderName: 'X-XSRF-TOKEN',
|
||||
onUploadProgress: (progressEvent: ProgressEvent) => {},
|
||||
onDownloadProgress: (progressEvent: ProgressEvent) => {},
|
||||
onUploadProgress: (progressEvent: AxiosProgressEvent) => {},
|
||||
onDownloadProgress: (progressEvent: AxiosProgressEvent) => {},
|
||||
maxContentLength: 2000,
|
||||
maxBodyLength: 2000,
|
||||
validateStatus: (status: number) => status >= 200 && status < 300,
|
||||
@@ -389,3 +389,37 @@ axios.toFormData({x: 1}, new FormData());
|
||||
// AbortSignal
|
||||
|
||||
axios.get('/user', {signal: new AbortController().signal});
|
||||
|
||||
// AxiosHeaders methods
|
||||
|
||||
axios.get('/user', {
|
||||
transformRequest: (data, headers) => {
|
||||
headers.setContentType('text/plain');
|
||||
headers['Foo'] = 'bar';
|
||||
},
|
||||
|
||||
transformResponse: (data, headers) => {
|
||||
headers.has('foo');
|
||||
}
|
||||
});
|
||||
|
||||
// Max Rate
|
||||
|
||||
axios.get('/user', {
|
||||
maxRate: 1000
|
||||
});
|
||||
|
||||
axios.get('/user', {
|
||||
maxRate: [1000, 1000],
|
||||
});
|
||||
|
||||
// Node progress
|
||||
|
||||
axios.get('/user', {
|
||||
onUploadProgress: (e) => {
|
||||
console.log(e.loaded);
|
||||
console.log(e.total);
|
||||
console.log(e.progress);
|
||||
console.log(e.rate);
|
||||
}
|
||||
});
|
||||
|
||||
+398
-38
@@ -1,24 +1,105 @@
|
||||
var axios = require('../../../index');
|
||||
var http = require('http');
|
||||
var https = require('https');
|
||||
var net = require('net');
|
||||
var url = require('url');
|
||||
var zlib = require('zlib');
|
||||
var assert = require('assert');
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var pkg = require('./../../../package.json');
|
||||
var server, proxy;
|
||||
var AxiosError = require('../../../lib/core/AxiosError');
|
||||
var FormData = require('form-data');
|
||||
var formidable = require('formidable');
|
||||
var express = require('express');
|
||||
var multer = require('multer');
|
||||
var bodyParser = require('body-parser');
|
||||
import axios from '../../../index.js';
|
||||
import http from 'http';
|
||||
import https from 'https';
|
||||
import net from 'net';
|
||||
import url from 'url';
|
||||
import zlib from 'zlib';
|
||||
import stream from 'stream';
|
||||
import util from 'util';
|
||||
import assert from 'assert';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import {EventEmitter} from 'events';
|
||||
let server, proxy;
|
||||
import AxiosError from '../../../lib/core/AxiosError.js';
|
||||
import FormData from 'form-data';
|
||||
import formidable from 'formidable';
|
||||
import express from 'express';
|
||||
import multer from 'multer';
|
||||
import bodyParser from 'body-parser';
|
||||
const isBlobSupported = typeof Blob !== 'undefined';
|
||||
import {Throttle} from 'stream-throttle';
|
||||
import devNull from 'dev-null';
|
||||
import {AbortController} from 'abortcontroller-polyfill/dist/cjs-ponyfill.js';
|
||||
|
||||
const __filename = url.fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
import getStream from 'get-stream';
|
||||
|
||||
function setTimeoutAsync(ms) {
|
||||
return new Promise(resolve=> setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
const pipelineAsync = util.promisify(stream.pipeline);
|
||||
const finishedAsync = util.promisify(stream.finished);
|
||||
|
||||
function toleranceRange(positive, negative) {
|
||||
const p = (1 + 1 / positive);
|
||||
const n = (1 / negative);
|
||||
|
||||
return (actualValue, value) => {
|
||||
return actualValue - value > 0 ? actualValue < value * p : actualValue > value * n;
|
||||
}
|
||||
}
|
||||
|
||||
var noop = ()=> {};
|
||||
|
||||
const LOCAL_SERVER_URL = 'http://localhost:4444';
|
||||
|
||||
function startHTTPServer({useBuffering= false, rate = undefined, port = 4444} = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
http.createServer(async function (req, res) {
|
||||
try {
|
||||
req.headers['content-length'] && res.setHeader('content-length', req.headers['content-length']);
|
||||
|
||||
var dataStream = req;
|
||||
|
||||
if (useBuffering) {
|
||||
dataStream = stream.Readable.from(await getStream(req));
|
||||
}
|
||||
|
||||
var streams = [dataStream];
|
||||
|
||||
if (rate) {
|
||||
streams.push(new Throttle({rate}))
|
||||
}
|
||||
|
||||
streams.push(res);
|
||||
|
||||
stream.pipeline(streams, (err) => {
|
||||
err && console.log('Server warning: ' + err.message)
|
||||
});
|
||||
} catch (err){
|
||||
console.warn('HTTP server error:', err);
|
||||
}
|
||||
|
||||
}).listen(port, function (err) {
|
||||
err ? reject(err) : resolve(this);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function generateReadableStream(length = 1024 * 1024, chunkSize = 10 * 1024, sleep = 50) {
|
||||
return stream.Readable.from(async function* (){
|
||||
let dataLength = 0;
|
||||
|
||||
while(dataLength < length) {
|
||||
const leftBytes = length - dataLength;
|
||||
|
||||
const chunk = Buffer.alloc(leftBytes > chunkSize? chunkSize : leftBytes);
|
||||
|
||||
dataLength += chunk.length;
|
||||
|
||||
yield chunk;
|
||||
|
||||
if (sleep) {
|
||||
await setTimeoutAsync(sleep);
|
||||
}
|
||||
}
|
||||
}());
|
||||
}
|
||||
|
||||
describe('supports http with nodejs', function () {
|
||||
|
||||
afterEach(function () {
|
||||
@@ -635,14 +716,15 @@ describe('supports http with nodejs', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('should support streams', function (done) {
|
||||
server = http.createServer(function (req, res) {
|
||||
req.pipe(res);
|
||||
}).listen(4444, function () {
|
||||
axios.post('http://localhost:4444/',
|
||||
fs.createReadStream(__filename), {
|
||||
responseType: 'stream'
|
||||
}).then(function (res) {
|
||||
describe('streams', function(){
|
||||
it('should support streams', function (done) {
|
||||
server = http.createServer(function (req, res) {
|
||||
req.pipe(res);
|
||||
}).listen(4444, function () {
|
||||
axios.post('http://localhost:4444/',
|
||||
fs.createReadStream(__filename), {
|
||||
responseType: 'stream'
|
||||
}).then(function (res) {
|
||||
var stream = res.data;
|
||||
var string = '';
|
||||
stream.on('data', function (chunk) {
|
||||
@@ -653,23 +735,49 @@ describe('supports http with nodejs', function () {
|
||||
done();
|
||||
});
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should pass errors for a failed stream', function (done) {
|
||||
var notExitPath = path.join(__dirname, 'does_not_exist');
|
||||
it('should pass errors for a failed stream', async function () {
|
||||
server = await startHTTPServer();
|
||||
|
||||
server = http.createServer(function (req, res) {
|
||||
req.pipe(res);
|
||||
}).listen(4444, function () {
|
||||
axios.post('http://localhost:4444/',
|
||||
fs.createReadStream(notExitPath)
|
||||
).then(function (res) {
|
||||
var notExitPath = path.join(__dirname, 'does_not_exist');
|
||||
|
||||
try {
|
||||
await axios.post(LOCAL_SERVER_URL,
|
||||
fs.createReadStream(notExitPath)
|
||||
);
|
||||
assert.fail('expected ENOENT error');
|
||||
}).catch(function (err) {
|
||||
} catch (err) {
|
||||
assert.equal(err.message, `ENOENT: no such file or directory, open \'${notExitPath}\'`);
|
||||
done();
|
||||
}).catch(done);
|
||||
}
|
||||
});
|
||||
|
||||
it('should destroy the response stream with an error on request stream destroying', async function () {
|
||||
server = await startHTTPServer();
|
||||
|
||||
let stream = generateReadableStream();
|
||||
|
||||
setTimeout(function () {
|
||||
stream.destroy();
|
||||
}, 1000);
|
||||
|
||||
const {data} = await axios.post(LOCAL_SERVER_URL, stream, {responseType: 'stream'});
|
||||
|
||||
let streamError;
|
||||
|
||||
data.on('error', function(err) {
|
||||
streamError = err;
|
||||
});
|
||||
|
||||
try {
|
||||
await pipelineAsync(data, devNull());
|
||||
assert.fail('stream was not aborted');
|
||||
} catch(e) {
|
||||
console.log(`pipeline error: ${e}`);
|
||||
} finally {
|
||||
assert.strictEqual(streamError && streamError.code, 'ERR_CANCELED');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1241,7 +1349,7 @@ describe('supports http with nodejs', function () {
|
||||
|
||||
it('should supply a user-agent if one is not specified', function (done) {
|
||||
server = http.createServer(function (req, res) {
|
||||
assert.equal(req.headers["user-agent"], 'axios/' + pkg.version);
|
||||
assert.equal(req.headers["user-agent"], 'axios/' + axios.VERSION);
|
||||
res.end();
|
||||
}).listen(4444, function () {
|
||||
axios.get('http://localhost:4444/'
|
||||
@@ -1532,4 +1640,256 @@ describe('supports http with nodejs', function () {
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
|
||||
describe('progress', function () {
|
||||
describe('upload', function () {
|
||||
it('should support upload progress capturing', async function () {
|
||||
server = await startHTTPServer({
|
||||
rate: 100 * 1024
|
||||
});
|
||||
|
||||
let content = '';
|
||||
const count = 10;
|
||||
const chunk = "test";
|
||||
const chunkLength = Buffer.byteLength(chunk);
|
||||
const contentLength = count * chunkLength;
|
||||
|
||||
var readable = stream.Readable.from(async function* () {
|
||||
let i = count;
|
||||
|
||||
while (i-- > 0) {
|
||||
await setTimeoutAsync(1100);
|
||||
content += chunk;
|
||||
yield chunk;
|
||||
}
|
||||
}());
|
||||
|
||||
const samples = [];
|
||||
|
||||
const {data} = await axios.post(LOCAL_SERVER_URL, readable, {
|
||||
onUploadProgress: ({loaded, total, progress, bytes, upload}) => {
|
||||
samples.push({
|
||||
loaded,
|
||||
total,
|
||||
progress,
|
||||
bytes,
|
||||
upload
|
||||
});
|
||||
},
|
||||
headers: {
|
||||
'Content-Length': contentLength
|
||||
},
|
||||
responseType: 'text'
|
||||
});
|
||||
|
||||
assert.strictEqual(data, content);
|
||||
|
||||
assert.deepStrictEqual(samples, Array.from(function* () {
|
||||
for (let i = 1; i <= 10; i++) {
|
||||
yield ({
|
||||
loaded: chunkLength * i,
|
||||
total: contentLength,
|
||||
progress: (chunkLength * i) / contentLength,
|
||||
bytes: 4,
|
||||
upload: true
|
||||
});
|
||||
}
|
||||
}()));
|
||||
});
|
||||
});
|
||||
|
||||
describe('download', function () {
|
||||
it('should support download progress capturing', async function () {
|
||||
server = await startHTTPServer({
|
||||
rate: 100 * 1024
|
||||
});
|
||||
|
||||
let content = '';
|
||||
const count = 10;
|
||||
const chunk = "test";
|
||||
const chunkLength = Buffer.byteLength(chunk);
|
||||
const contentLength = count * chunkLength;
|
||||
|
||||
var readable = stream.Readable.from(async function* () {
|
||||
let i = count;
|
||||
|
||||
while (i-- > 0) {
|
||||
await setTimeoutAsync(1100);
|
||||
content += chunk;
|
||||
yield chunk;
|
||||
}
|
||||
}());
|
||||
|
||||
const samples = [];
|
||||
|
||||
const {data} = await axios.post(LOCAL_SERVER_URL, readable, {
|
||||
onDownloadProgress: ({loaded, total, progress, bytes, download}) => {
|
||||
samples.push({
|
||||
loaded,
|
||||
total,
|
||||
progress,
|
||||
bytes,
|
||||
download
|
||||
});
|
||||
},
|
||||
headers: {
|
||||
'Content-Length': contentLength
|
||||
},
|
||||
responseType: 'text',
|
||||
maxRedirects: 0
|
||||
});
|
||||
|
||||
assert.strictEqual(data, content);
|
||||
|
||||
assert.deepStrictEqual(samples, Array.from(function* () {
|
||||
for (let i = 1; i <= 10; i++) {
|
||||
yield ({
|
||||
loaded: chunkLength * i,
|
||||
total: contentLength,
|
||||
progress: (chunkLength * i) / contentLength,
|
||||
bytes: 4,
|
||||
download: true
|
||||
});
|
||||
}
|
||||
}()));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Rate limit', function () {
|
||||
it('should support upload rate limit', async function () {
|
||||
const secs = 10;
|
||||
const configRate = 100_000;
|
||||
const chunkLength = configRate * secs;
|
||||
|
||||
server = await startHTTPServer();
|
||||
|
||||
const buf = Buffer.alloc(chunkLength).fill('s');
|
||||
const samples = [];
|
||||
const skip = 2;
|
||||
const compareValues = toleranceRange(10, 50);
|
||||
|
||||
const {data} = await axios.post(LOCAL_SERVER_URL, buf, {
|
||||
onUploadProgress: ({loaded, total, progress, bytes, rate}) => {
|
||||
samples.push({
|
||||
loaded,
|
||||
total,
|
||||
progress,
|
||||
bytes,
|
||||
rate
|
||||
});
|
||||
},
|
||||
maxRate: [configRate],
|
||||
responseType: 'text',
|
||||
maxRedirects: 0
|
||||
});
|
||||
|
||||
samples.slice(skip).forEach(({rate, progress}, i, _samples)=> {
|
||||
assert.ok(compareValues(rate, configRate),
|
||||
`Rate sample at index ${i} is out of the expected range (${rate} / ${configRate}) [${
|
||||
_samples.map(({rate}) => rate).join(', ')
|
||||
}]`
|
||||
);
|
||||
|
||||
const progressTicksRate = 2;
|
||||
const expectedProgress = ((i + skip) / secs) / progressTicksRate;
|
||||
|
||||
assert.ok(
|
||||
Math.abs(expectedProgress - progress) < 0.25,
|
||||
`Progress sample at index ${i} is out of the expected range (${progress} / ${expectedProgress}) [${
|
||||
_samples.map(({progress}) => progress).join(', ')
|
||||
}]`
|
||||
);
|
||||
});
|
||||
|
||||
assert.strictEqual(data, buf.toString(), 'content corrupted');
|
||||
});
|
||||
|
||||
it('should support download rate limit', async function () {
|
||||
const secs = 10;
|
||||
const configRate = 100_000;
|
||||
const chunkLength = configRate * secs;
|
||||
|
||||
server = await startHTTPServer();
|
||||
|
||||
const buf = Buffer.alloc(chunkLength).fill('s');
|
||||
const samples = [];
|
||||
const skip = 2;
|
||||
const compareValues = toleranceRange(10, 50);
|
||||
|
||||
const {data} = await axios.post(LOCAL_SERVER_URL, buf, {
|
||||
onDownloadProgress: ({loaded, total, progress, bytes, rate}) => {
|
||||
samples.push({
|
||||
loaded,
|
||||
total,
|
||||
progress,
|
||||
bytes,
|
||||
rate
|
||||
});
|
||||
},
|
||||
maxRate: [0, configRate],
|
||||
responseType: 'text',
|
||||
maxRedirects: 0
|
||||
});
|
||||
|
||||
samples.slice(skip).forEach(({rate, progress}, i, _samples)=> {
|
||||
assert.ok(compareValues(rate, configRate),
|
||||
`Rate sample at index ${i} is out of the expected range (${rate} / ${configRate}) [${
|
||||
_samples.map(({rate}) => rate).join(', ')
|
||||
}]`
|
||||
);
|
||||
|
||||
const progressTicksRate = 2;
|
||||
const expectedProgress = ((i + skip) / secs) / progressTicksRate;
|
||||
|
||||
assert.ok(
|
||||
Math.abs(expectedProgress - progress) < 0.25,
|
||||
`Progress sample at index ${i} is out of the expected range (${progress} / ${expectedProgress}) [${
|
||||
_samples.map(({progress}) => progress).join(', ')
|
||||
}]`
|
||||
);
|
||||
});
|
||||
|
||||
assert.strictEqual(data, buf.toString(), 'content corrupted');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('request aborting', function() {
|
||||
it('should be able to abort the response stream', async function () {
|
||||
server = await startHTTPServer({
|
||||
rate: 100_000,
|
||||
useBuffering: true
|
||||
});
|
||||
|
||||
const buf = Buffer.alloc(1024 * 1024);
|
||||
|
||||
const controller = new AbortController();
|
||||
|
||||
var {data} = await axios.post(LOCAL_SERVER_URL, buf, {
|
||||
responseType: 'stream',
|
||||
signal: controller.signal,
|
||||
maxRedirects: 0
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
controller.abort();
|
||||
}, 500);
|
||||
|
||||
let streamError;
|
||||
|
||||
data.on('error', function(err) {
|
||||
streamError = err;
|
||||
});
|
||||
|
||||
try {
|
||||
await pipelineAsync(data, devNull());
|
||||
assert.fail('stream was not aborted');
|
||||
} catch(e) {
|
||||
console.log(`pipeline error: ${e}`);
|
||||
} finally {
|
||||
assert.strictEqual(streamError && streamError.code, 'ERR_CANCELED');
|
||||
}
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
@@ -0,0 +1,331 @@
|
||||
import AxiosHeaders from '../../../lib/core/AxiosHeaders.js';
|
||||
import assert from 'assert';
|
||||
|
||||
|
||||
describe('AxiosHeaders', function () {
|
||||
it('should support headers argument', function () {
|
||||
const headers = new AxiosHeaders({
|
||||
x: 1,
|
||||
y: 2
|
||||
});
|
||||
|
||||
assert.strictEqual(headers.get('x'), '1');
|
||||
assert.strictEqual(headers.get('y'), '2');
|
||||
})
|
||||
|
||||
|
||||
describe('set', function () {
|
||||
it('should support adding a single header', function(){
|
||||
const headers = new AxiosHeaders();
|
||||
|
||||
headers.set('foo', 'bar');
|
||||
|
||||
assert.strictEqual(headers.get('foo'), 'bar');
|
||||
})
|
||||
|
||||
it('should support adding multiple headers', function(){
|
||||
const headers = new AxiosHeaders();
|
||||
|
||||
headers.set({
|
||||
foo: 'value1',
|
||||
bar: 'value2',
|
||||
});
|
||||
|
||||
assert.strictEqual(headers.get('foo'), 'value1');
|
||||
assert.strictEqual(headers.get('bar'), 'value2');
|
||||
})
|
||||
|
||||
it('should not rewrite header the header if the value is false', function(){
|
||||
const headers = new AxiosHeaders();
|
||||
|
||||
headers.set('foo', 'value1');
|
||||
|
||||
headers.set('foo', 'value2', false);
|
||||
|
||||
assert.strictEqual(headers.get('foo'), 'value1');
|
||||
|
||||
headers.set('foo', 'value2');
|
||||
|
||||
assert.strictEqual(headers.get('foo'), 'value2');
|
||||
|
||||
headers.set('foo', 'value3', true);
|
||||
|
||||
assert.strictEqual(headers.get('foo'), 'value3');
|
||||
});
|
||||
|
||||
it('should not rewrite the header if its value is false, unless rewrite options is set to true', function(){
|
||||
const headers = new AxiosHeaders();
|
||||
|
||||
headers.set('foo', false);
|
||||
headers.set('foo', 'value2');
|
||||
|
||||
assert.strictEqual(headers.get('foo'), false);
|
||||
|
||||
headers.set('foo', 'value2', true);
|
||||
|
||||
assert.strictEqual(headers.get('foo'), 'value2');
|
||||
})
|
||||
});
|
||||
|
||||
describe('get', function () {
|
||||
describe('filter', function() {
|
||||
it('should support RegExp', function () {
|
||||
const headers = new AxiosHeaders();
|
||||
|
||||
headers.set('foo', 'bar=value1');
|
||||
|
||||
assert.strictEqual(headers.get('foo', /^bar=(\w+)/)[1], 'value1');
|
||||
assert.strictEqual(headers.get('foo', /^foo=/), null);
|
||||
});
|
||||
|
||||
it('should support function', function () {
|
||||
const headers = new AxiosHeaders();
|
||||
|
||||
headers.set('foo', 'bar=value1');
|
||||
|
||||
assert.strictEqual(headers.get('foo', (value, header) => {
|
||||
assert.strictEqual(value, 'bar=value1');
|
||||
assert.strictEqual(header, 'foo');
|
||||
return value;
|
||||
}), 'bar=value1');
|
||||
assert.strictEqual(headers.get('foo', () => false), false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('has', function () {
|
||||
it('should return true if the header is defined, otherwise false', function () {
|
||||
const headers = new AxiosHeaders();
|
||||
|
||||
headers.set('foo', 'bar=value1');
|
||||
|
||||
assert.strictEqual(headers.has('foo'), true);
|
||||
assert.strictEqual(headers.has('bar'), false);
|
||||
});
|
||||
|
||||
describe('filter', function () {
|
||||
it('should support RegExp', function () {
|
||||
const headers = new AxiosHeaders();
|
||||
|
||||
headers.set('foo', 'bar=value1');
|
||||
|
||||
assert.strictEqual(headers.has('foo', /^bar=(\w+)/), true);
|
||||
assert.strictEqual(headers.has('foo', /^foo=/), false);
|
||||
});
|
||||
|
||||
it('should support function', function () {
|
||||
const headers = new AxiosHeaders();
|
||||
|
||||
headers.set('foo', 'bar=value1');
|
||||
|
||||
assert.strictEqual(headers.has('foo', (value, header, headers)=> {
|
||||
assert.strictEqual(value, 'bar=value1');
|
||||
assert.strictEqual(header, 'foo');
|
||||
return true;
|
||||
}), true);
|
||||
assert.strictEqual(headers.has('foo', ()=> false), false);
|
||||
});
|
||||
|
||||
it('should support string pattern', function () {
|
||||
const headers = new AxiosHeaders();
|
||||
|
||||
headers.set('foo', 'bar=value1');
|
||||
|
||||
assert.strictEqual(headers.has('foo', 'value1'), true);
|
||||
assert.strictEqual(headers.has('foo', 'value2'), false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('delete', function () {
|
||||
it('should delete the header', function () {
|
||||
const headers = new AxiosHeaders();
|
||||
|
||||
headers.set('foo', 'bar=value1');
|
||||
|
||||
assert.strictEqual(headers.has('foo'), true);
|
||||
|
||||
headers.delete('foo');
|
||||
|
||||
assert.strictEqual(headers.has('foo'), false);
|
||||
});
|
||||
|
||||
it('should return true if the header has been deleted, otherwise false', function () {
|
||||
const headers = new AxiosHeaders();
|
||||
|
||||
headers.set('foo', 'bar=value1');
|
||||
|
||||
assert.strictEqual(headers.delete('bar'), false);
|
||||
|
||||
assert.strictEqual(headers.delete('foo'), true);
|
||||
});
|
||||
|
||||
it('should support headers array', function () {
|
||||
const headers = new AxiosHeaders();
|
||||
|
||||
headers.set('foo', 'x');
|
||||
headers.set('bar', 'y');
|
||||
headers.set('baz', 'z');
|
||||
|
||||
assert.strictEqual(headers.delete(['foo', 'baz']), true);
|
||||
|
||||
assert.strictEqual(headers.has('foo'), false);
|
||||
assert.strictEqual(headers.has('bar'), true);
|
||||
assert.strictEqual(headers.has('baa'), false);
|
||||
});
|
||||
|
||||
describe('filter', function () {
|
||||
it('should support RegExp', function () {
|
||||
const headers = new AxiosHeaders();
|
||||
|
||||
headers.set('foo', 'bar=value1');
|
||||
|
||||
assert.strictEqual(headers.has('foo'), true);
|
||||
|
||||
headers.delete('foo', /baz=/);
|
||||
|
||||
assert.strictEqual(headers.has('foo'), true);
|
||||
|
||||
headers.delete('foo', /bar=/);
|
||||
|
||||
assert.strictEqual(headers.has('foo'), false);
|
||||
});
|
||||
|
||||
it('should support function', function () {
|
||||
const headers = new AxiosHeaders();
|
||||
|
||||
headers.set('foo', 'bar=value1');
|
||||
|
||||
headers.delete('foo', (value, header)=> {
|
||||
assert.strictEqual(value, 'bar=value1');
|
||||
assert.strictEqual(header, 'foo');
|
||||
return false;
|
||||
});
|
||||
|
||||
assert.strictEqual(headers.has('foo'), true);
|
||||
|
||||
assert.strictEqual(headers.delete('foo', ()=> true), true);
|
||||
|
||||
assert.strictEqual(headers.has('foo'), false);
|
||||
});
|
||||
|
||||
it('should support string pattern', function () {
|
||||
const headers = new AxiosHeaders();
|
||||
|
||||
headers.set('foo', 'bar=value1');
|
||||
|
||||
assert.strictEqual(headers.has('foo'), true);
|
||||
|
||||
headers.delete('foo', 'baz');
|
||||
|
||||
assert.strictEqual(headers.has('foo'), true);
|
||||
|
||||
headers.delete('foo', 'bar');
|
||||
|
||||
assert.strictEqual(headers.has('foo'), false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('toJSON', function () {
|
||||
it('should return headers object with original headers case', function () {
|
||||
const headers = new AxiosHeaders({
|
||||
Foo: 'x',
|
||||
bAr: 'y'
|
||||
});
|
||||
|
||||
assert.deepStrictEqual({...headers.toJSON()}, {
|
||||
Foo: 'x',
|
||||
bAr: 'y'
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('accessors', function () {
|
||||
it('should support get accessor', function () {
|
||||
const headers = new AxiosHeaders({
|
||||
foo: 1
|
||||
});
|
||||
|
||||
headers.constructor.accessor('foo');
|
||||
|
||||
assert.strictEqual(typeof headers.getFoo, 'function');
|
||||
assert.strictEqual(headers.getFoo(), '1');
|
||||
});
|
||||
|
||||
it('should support set accessor', function () {
|
||||
const headers = new AxiosHeaders({
|
||||
foo: 1
|
||||
});
|
||||
|
||||
headers.constructor.accessor('foo');
|
||||
|
||||
assert.strictEqual(typeof headers.setFoo, 'function');
|
||||
headers.setFoo(2);
|
||||
assert.strictEqual(headers.getFoo(), '2');
|
||||
});
|
||||
|
||||
it('should support has accessor', function () {
|
||||
const headers = new AxiosHeaders({
|
||||
foo: 1
|
||||
});
|
||||
|
||||
headers.constructor.accessor('foo');
|
||||
|
||||
assert.strictEqual(typeof headers.hasFoo, 'function');
|
||||
assert.strictEqual(headers.hasFoo(), true);
|
||||
});
|
||||
});
|
||||
|
||||
it('should be caseless', function () {
|
||||
const headers = new AxiosHeaders({
|
||||
fOo: 1
|
||||
});
|
||||
|
||||
assert.strictEqual(headers.get('Foo'), '1');
|
||||
assert.strictEqual(headers.get('foo'), '1');
|
||||
|
||||
headers.set('foo', 2);
|
||||
|
||||
assert.strictEqual(headers.get('foO'), '2');
|
||||
assert.strictEqual(headers.get('fOo'), '2');
|
||||
|
||||
assert.strictEqual(headers.has('fOo'), true);
|
||||
|
||||
headers.delete('FOO');
|
||||
|
||||
assert.strictEqual(headers.has('fOo'), false);
|
||||
|
||||
});
|
||||
|
||||
describe('normalize()', function () {
|
||||
it('should support auto-formatting', function () {
|
||||
const headers = new AxiosHeaders({
|
||||
fOo: 1,
|
||||
'x-foo': 2,
|
||||
'y-bar-bAz': 3
|
||||
});
|
||||
|
||||
assert.deepStrictEqual({...headers.normalize(true).toJSON()}, {
|
||||
Foo: '1',
|
||||
'X-Foo': '2',
|
||||
'Y-Bar-Baz': '3'
|
||||
});
|
||||
});
|
||||
|
||||
it('should support external defined values', function () {
|
||||
const headers = new AxiosHeaders({
|
||||
foo: '1'
|
||||
});
|
||||
|
||||
headers['Foo'] = 2;
|
||||
|
||||
headers['bar'] = 3;
|
||||
|
||||
assert.deepStrictEqual({...headers.normalize().toJSON()}, {
|
||||
foo: '2',
|
||||
bar: '3'
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,29 +1,53 @@
|
||||
var defaults = require('../../../lib/defaults');
|
||||
var transformData = require('../../../lib/core/transformData');
|
||||
var assert = require('assert');
|
||||
import defaults from '../../../lib/defaults/index.js';
|
||||
import transformData from '../../../lib/core/transformData.js';
|
||||
import assert from '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'}, 200, defaults.transformResponse);
|
||||
const data = '{"message": "hello, world"}';
|
||||
const result = transformData.call({
|
||||
data,
|
||||
response: {
|
||||
headers: {'content-type': 'application/json'},
|
||||
status: 200
|
||||
}
|
||||
}, 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'}, 200, defaults.transformResponse);
|
||||
const data = '<message>hello, world</message>';
|
||||
const result = transformData.call({
|
||||
data,
|
||||
response: {
|
||||
headers: {'content-type': 'text/xml'},
|
||||
status: 200
|
||||
}
|
||||
}, 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}, 204, defaults.transformResponse);
|
||||
const data = '';
|
||||
const result = transformData.call({
|
||||
data,
|
||||
response: {
|
||||
headers: {'content-type': undefined},
|
||||
status: 204
|
||||
}
|
||||
}, defaults.transformResponse);
|
||||
assert.strictEqual(result, '');
|
||||
});
|
||||
it('does not parse undefined', function () {
|
||||
var data = undefined;
|
||||
var result = transformData(data, {'content-type': undefined}, 200, defaults.transformResponse);
|
||||
const data = undefined;
|
||||
const result = transformData.call({
|
||||
data,
|
||||
response: {
|
||||
headers: {'content-type': undefined},
|
||||
status: 200
|
||||
}
|
||||
}, defaults.transformResponse);
|
||||
assert.strictEqual(result, data);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
var assert = require('assert');
|
||||
var fromDataURI = require('../../../lib/helpers/fromDataURI');
|
||||
import assert from 'assert';
|
||||
import fromDataURI from '../../../lib/helpers/fromDataURI.js';
|
||||
|
||||
describe('helpers::fromDataURI', function () {
|
||||
it('should return buffer from data uri', function () {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
var assert = require('assert');
|
||||
var utils = require('../../../lib/utils');
|
||||
var parseProtocol = require('../../../lib/helpers/parseProtocol');
|
||||
import assert from 'assert';
|
||||
import utils from '../../../lib/utils.js';
|
||||
import parseProtocol from '../../../lib/helpers/parseProtocol.js';
|
||||
|
||||
describe('helpers::parseProtocol', function () {
|
||||
it('should parse protocol part if it exists', function () {
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
// https://github.com/axios/axios/issues/3407
|
||||
// https://github.com/axios/axios/issues/3369
|
||||
|
||||
const axios = require('../../../index');
|
||||
const http = require('http');
|
||||
const assert = require('assert');
|
||||
import axios from '../../../index.js';
|
||||
import http from 'http';
|
||||
import assert from 'assert';
|
||||
|
||||
const PROXY_PORT = 4777;
|
||||
const EVIL_PORT = 4666;
|
||||
@@ -58,4 +58,4 @@ describe('Server-Side Request Forgery (SSRF)', () => {
|
||||
return response;
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
var assert = require('assert');
|
||||
var isFormData = require('../../../lib/utils').isFormData;
|
||||
var FormData = require('form-data');
|
||||
|
||||
describe('utils::isFormData', function () {
|
||||
it('should detect the FormData instance provided by the `form-data` package', function () {
|
||||
[1, 'str', {}, new RegExp()].forEach(function (thing) {
|
||||
assert.equal(isFormData(thing), false);
|
||||
});
|
||||
assert.equal(isFormData(new FormData()), true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
import assert from 'assert';
|
||||
import utils from '../../../lib/utils.js';
|
||||
import FormData from 'form-data';
|
||||
import stream from 'stream';
|
||||
|
||||
describe('utils', function (){
|
||||
it('should validate Stream', function () {
|
||||
assert.strictEqual(utils.isStream(new stream.Readable()),true);
|
||||
assert.strictEqual(utils.isStream({ foo: 'bar' }),false);
|
||||
});
|
||||
|
||||
it('should validate Buffer', function () {
|
||||
assert.strictEqual(utils.isBuffer(Buffer.from('a')),true);
|
||||
assert.strictEqual(utils.isBuffer(null),false);
|
||||
assert.strictEqual(utils.isBuffer(undefined),false);
|
||||
});
|
||||
|
||||
describe('utils::isFormData', function () {
|
||||
it('should detect the FormData instance provided by the `form-data` package', function () {
|
||||
[1, 'str', {}, new RegExp()].forEach(function (thing) {
|
||||
assert.equal(utils.isFormData(thing), false);
|
||||
});
|
||||
assert.equal(utils.isFormData(new FormData()), true);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user