mirror of
https://github.com/tenrok/axios.git
synced 2026-06-23 20:40:40 +03:00
Moving test helpers and auto-loading them
This commit is contained in:
+3
-1
@@ -111,7 +111,8 @@ module.exports = function(config) {
|
|||||||
|
|
||||||
// list of files / patterns to load in the browser
|
// list of files / patterns to load in the browser
|
||||||
files: [
|
files: [
|
||||||
'test/specs/**/*.spec.js'
|
'test/specs/__helpers.js',
|
||||||
|
'test/specs/**/*.spec.js',
|
||||||
],
|
],
|
||||||
|
|
||||||
|
|
||||||
@@ -124,6 +125,7 @@ module.exports = function(config) {
|
|||||||
// preprocess matching files before serving them to the browser
|
// preprocess matching files before serving them to the browser
|
||||||
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
|
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
|
||||||
preprocessors: {
|
preprocessors: {
|
||||||
|
'test/specs/__helpers.js': ['webpack', 'sourcemap'],
|
||||||
'test/specs/**/*.spec.js': ['webpack', 'sourcemap']
|
'test/specs/**/*.spec.js': ['webpack', 'sourcemap']
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
var attempts = 0;
|
|
||||||
var MAX_ATTEMPTS = 5;
|
|
||||||
var ATTEMPT_DELAY_FACTOR = 5;
|
|
||||||
|
|
||||||
module.exports = 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
setTimeout(function () {
|
|
||||||
var request = jasmine.Ajax.requests.mostRecent();
|
|
||||||
if (request) {
|
|
||||||
resolve(request);
|
|
||||||
} else {
|
|
||||||
attemptGettingAjaxRequest(resolve, reject);
|
|
||||||
}
|
|
||||||
}, delay);
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
// Polyfill ES6 Promise
|
||||||
|
require('es6-promise').polyfill();
|
||||||
|
|
||||||
|
// Import axios
|
||||||
|
axios = require('../../index');
|
||||||
|
|
||||||
|
// Is this an old version of IE that lacks standard objects like DataView, ArrayBuffer, FormData, etc.
|
||||||
|
isOldIE = /MSIE (8|9)\.0/.test(navigator.userAgent);
|
||||||
|
|
||||||
|
// Get Ajax request using an increasing timeout to retry
|
||||||
|
getAjaxRequest = (function () {
|
||||||
|
var attempts = 0;
|
||||||
|
var MAX_ATTEMPTS = 5;
|
||||||
|
var 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
setTimeout(function () {
|
||||||
|
var 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();
|
||||||
|
}, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should fail to encode HTTP Basic auth credentials with non-Latin1 characters', 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,42 +0,0 @@
|
|||||||
var axios = require('../../index');
|
|
||||||
var validateInvalidCharacterError = require('./__validateInvalidCharacterError');
|
|
||||||
|
|
||||||
module.exports = 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();
|
|
||||||
}, 0);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should fail to encode HTTP Basic auth credentials with non-Latin1 characters', 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,3 +0,0 @@
|
|||||||
module.exports = function validateInvalidCharacterError(error) {
|
|
||||||
expect(/character/i.test(error.message)).toEqual(true);
|
|
||||||
};
|
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
var axios = require('../../index');
|
|
||||||
|
|
||||||
describe('static api', function () {
|
describe('static api', function () {
|
||||||
it('should have request method helpers', function () {
|
it('should have request method helpers', function () {
|
||||||
expect(typeof axios.get).toEqual('function');
|
expect(typeof axios.get).toEqual('function');
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
var setupBasicAuthTest = require('./__setupBasicAuthTest');
|
|
||||||
|
|
||||||
describe('basicAuth without btoa polyfill', function () {
|
describe('basicAuth without btoa polyfill', function () {
|
||||||
setupBasicAuthTest();
|
setupBasicAuthTest();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
var setupBasicAuthTest = require('./__setupBasicAuthTest');
|
|
||||||
var window_btoa;
|
var window_btoa;
|
||||||
|
|
||||||
describe('basicAuth with btoa polyfill', function () {
|
describe('basicAuth with btoa polyfill', function () {
|
||||||
|
|||||||
@@ -1,20 +1,16 @@
|
|||||||
var axios = require('../../index');
|
|
||||||
var defaults = require('../../lib/defaults');
|
var defaults = require('../../lib/defaults');
|
||||||
var utils = require('../../lib/utils');
|
var utils = require('../../lib/utils');
|
||||||
var getAjaxRequest = require('./__getAjaxRequest');
|
|
||||||
|
|
||||||
describe('defaults', function () {
|
describe('defaults', function () {
|
||||||
var __defaults;
|
|
||||||
var XSRF_COOKIE_NAME = 'CUSTOM-XSRF-TOKEN';
|
var XSRF_COOKIE_NAME = 'CUSTOM-XSRF-TOKEN';
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
jasmine.Ajax.install();
|
jasmine.Ajax.install();
|
||||||
__defaults = axios.defaults;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
jasmine.Ajax.uninstall();
|
jasmine.Ajax.uninstall();
|
||||||
axios.defaults = __defaults;
|
delete axios.defaults.baseURL;
|
||||||
document.cookie = XSRF_COOKIE_NAME + '=;expires=' + new Date(Date.now() - 86400000).toGMTString();
|
document.cookie = XSRF_COOKIE_NAME + '=;expires=' + new Date(Date.now() - 86400000).toGMTString();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
var axios = require('../../index');
|
|
||||||
var getAjaxRequest = require('./__getAjaxRequest');
|
|
||||||
|
|
||||||
function testHeaderValue(headers, key, val) {
|
function testHeaderValue(headers, key, val) {
|
||||||
var found = false;
|
var found = false;
|
||||||
|
|
||||||
|
|||||||
@@ -1,29 +1,26 @@
|
|||||||
var __btoa = require('../../../lib/helpers/btoa');
|
var __btoa = require('../../../lib/helpers/btoa');
|
||||||
var validateInvalidCharacterError = require('../__validateInvalidCharacterError');
|
|
||||||
|
|
||||||
describe('btoa polyfill', function () {
|
describe('btoa polyfill', function () {
|
||||||
it('should behave the same as native window.btoa', function () {
|
it('should behave the same as native window.btoa', function () {
|
||||||
|
// btoa doesn't exist in IE8/9
|
||||||
|
if (isOldIE && typeof Int8Array === 'undefined') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var data = 'Hello, world';
|
var data = 'Hello, world';
|
||||||
expect(__btoa(data)).toEqual(window.btoa(data));
|
expect(__btoa(data)).toEqual(window.btoa(data));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw an error if char is out of range 0xFF', function () {
|
it('should throw an error if char is out of range 0xFF', function () {
|
||||||
var err1, err2;
|
var err;
|
||||||
var data = 'I ♡ Unicode!';
|
var data = 'I ♡ Unicode!';
|
||||||
|
|
||||||
try {
|
|
||||||
window.btoa(data);
|
|
||||||
} catch (e) {
|
|
||||||
err1 = e;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
__btoa(data);
|
__btoa(data);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
err2 = e;
|
err = e;
|
||||||
}
|
}
|
||||||
|
|
||||||
validateInvalidCharacterError(err1);
|
validateInvalidCharacterError(err);
|
||||||
validateInvalidCharacterError(err2);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
var axios = require('../../index');
|
|
||||||
var getAjaxRequest = require('./__getAjaxRequest');
|
|
||||||
|
|
||||||
describe('instance', function () {
|
describe('instance', function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
jasmine.Ajax.install();
|
jasmine.Ajax.install();
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
var axios = require('../../index');
|
|
||||||
var getAjaxRequest = require('./__getAjaxRequest');
|
|
||||||
|
|
||||||
describe('interceptors', function () {
|
describe('interceptors', function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
jasmine.Ajax.install();
|
jasmine.Ajax.install();
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
var axios = require('../../index');
|
|
||||||
var getAjaxRequest = require('./__getAjaxRequest');
|
|
||||||
|
|
||||||
describe('options', function () {
|
describe('options', function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
jasmine.Ajax.install();
|
jasmine.Ajax.install();
|
||||||
|
|||||||
@@ -1,7 +1,3 @@
|
|||||||
require('es6-promise').polyfill();
|
|
||||||
var axios = require('../../index');
|
|
||||||
var getAjaxRequest = require('./__getAjaxRequest');
|
|
||||||
|
|
||||||
describe('promise', function () {
|
describe('promise', function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
jasmine.Ajax.install();
|
jasmine.Ajax.install();
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
var axios = require('../../index');
|
|
||||||
var getAjaxRequest = require('./__getAjaxRequest');
|
|
||||||
|
|
||||||
describe('requests', function () {
|
describe('requests', function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
jasmine.Ajax.install();
|
jasmine.Ajax.install();
|
||||||
@@ -154,6 +151,12 @@ describe('requests', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should support binary data as array buffer', function (done) {
|
it('should support binary data as array buffer', function (done) {
|
||||||
|
// Int8Array doesn't exist in IE8/9
|
||||||
|
if (isOldIE && typeof Int8Array === 'undefined') {
|
||||||
|
done();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var input = new Int8Array(2);
|
var input = new Int8Array(2);
|
||||||
input[0] = 1;
|
input[0] = 1;
|
||||||
input[1] = 2;
|
input[1] = 2;
|
||||||
@@ -170,6 +173,12 @@ describe('requests', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should support binary data as array buffer view', function (done) {
|
it('should support binary data as array buffer view', function (done) {
|
||||||
|
// Int8Array doesn't exist in IE8/9
|
||||||
|
if (isOldIE && typeof Int8Array === 'undefined') {
|
||||||
|
done();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var input = new Int8Array(2);
|
var input = new Int8Array(2);
|
||||||
input[0] = 1;
|
input[0] = 1;
|
||||||
input[1] = 2;
|
input[1] = 2;
|
||||||
@@ -186,6 +195,12 @@ describe('requests', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should support array buffer response', function (done) {
|
it('should support array buffer response', function (done) {
|
||||||
|
// ArrayBuffer doesn't exist in IE8/9
|
||||||
|
if (isOldIE && typeof ArrayBuffer === 'undefined') {
|
||||||
|
done();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var response;
|
var response;
|
||||||
|
|
||||||
function str2ab(str) {
|
function str2ab(str) {
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
var axios = require('../../index');
|
|
||||||
var getAjaxRequest = require('./__getAjaxRequest');
|
|
||||||
|
|
||||||
describe('transform', function () {
|
describe('transform', function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
jasmine.Ajax.install();
|
jasmine.Ajax.install();
|
||||||
|
|||||||
@@ -7,22 +7,41 @@ describe('utils::isX', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should validate ArrayBuffer', function () {
|
it('should validate ArrayBuffer', function () {
|
||||||
|
// ArrayBuffer doesn't exist in IE8/9
|
||||||
|
if (isOldIE && typeof ArrayBuffer === 'undefined') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
expect(utils.isArrayBuffer(new ArrayBuffer(2))).toEqual(true);
|
expect(utils.isArrayBuffer(new ArrayBuffer(2))).toEqual(true);
|
||||||
expect(utils.isArrayBuffer({})).toEqual(false);
|
expect(utils.isArrayBuffer({})).toEqual(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should validate ArrayBufferView', function () {
|
it('should validate ArrayBufferView', function () {
|
||||||
|
// ArrayBuffer doesn't exist in IE8/9
|
||||||
|
if (isOldIE && typeof ArrayBuffer === 'undefined') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
expect(utils.isArrayBufferView(new DataView(new ArrayBuffer(2)))).toEqual(true);
|
expect(utils.isArrayBufferView(new DataView(new ArrayBuffer(2)))).toEqual(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should validate FormData', function () {
|
it('should validate FormData', function () {
|
||||||
|
// FormData doesn't exist in IE8/9
|
||||||
|
if (isOldIE && typeof FormData === 'undefined') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
expect(utils.isFormData(new FormData())).toEqual(true);
|
expect(utils.isFormData(new FormData())).toEqual(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO Blob is not a constructor in PhantomJS
|
it('should validate Blob', function () {
|
||||||
// it('should validate Blob', function () {
|
// Blob doesn't exist in IE8/9
|
||||||
// expect(utils.isBlob(new Blob())).toEqual(true);
|
if (isOldIE && typeof Blob === 'undefined') {
|
||||||
// });
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(utils.isBlob(new Blob())).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
it('should validate String', function () {
|
it('should validate String', function () {
|
||||||
expect(utils.isString('')).toEqual(true);
|
expect(utils.isString('')).toEqual(true);
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
var axios = require('../../index');
|
|
||||||
var getAjaxRequest = require('./__getAjaxRequest');
|
|
||||||
|
|
||||||
describe('xsrf', function () {
|
describe('xsrf', function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
jasmine.Ajax.install();
|
jasmine.Ajax.install();
|
||||||
|
|||||||
Reference in New Issue
Block a user