mirror of
https://github.com/tenrok/axios.git
synced 2026-06-05 16:42:32 +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
|
||||
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
|
||||
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
|
||||
preprocessors: {
|
||||
'test/specs/__helpers.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 () {
|
||||
it('should have request method helpers', function () {
|
||||
expect(typeof axios.get).toEqual('function');
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
var setupBasicAuthTest = require('./__setupBasicAuthTest');
|
||||
|
||||
describe('basicAuth without btoa polyfill', function () {
|
||||
setupBasicAuthTest();
|
||||
});
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
var setupBasicAuthTest = require('./__setupBasicAuthTest');
|
||||
var window_btoa;
|
||||
|
||||
describe('basicAuth with btoa polyfill', function () {
|
||||
|
||||
@@ -1,20 +1,16 @@
|
||||
var axios = require('../../index');
|
||||
var defaults = require('../../lib/defaults');
|
||||
var utils = require('../../lib/utils');
|
||||
var getAjaxRequest = require('./__getAjaxRequest');
|
||||
|
||||
describe('defaults', function () {
|
||||
var __defaults;
|
||||
var XSRF_COOKIE_NAME = 'CUSTOM-XSRF-TOKEN';
|
||||
|
||||
beforeEach(function () {
|
||||
jasmine.Ajax.install();
|
||||
__defaults = axios.defaults;
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
jasmine.Ajax.uninstall();
|
||||
axios.defaults = __defaults;
|
||||
delete axios.defaults.baseURL;
|
||||
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) {
|
||||
var found = false;
|
||||
|
||||
|
||||
@@ -1,29 +1,26 @@
|
||||
var __btoa = require('../../../lib/helpers/btoa');
|
||||
var validateInvalidCharacterError = require('../__validateInvalidCharacterError');
|
||||
|
||||
describe('btoa polyfill', 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';
|
||||
expect(__btoa(data)).toEqual(window.btoa(data));
|
||||
});
|
||||
|
||||
it('should throw an error if char is out of range 0xFF', function () {
|
||||
var err1, err2;
|
||||
var err;
|
||||
var data = 'I ♡ Unicode!';
|
||||
|
||||
try {
|
||||
window.btoa(data);
|
||||
} catch (e) {
|
||||
err1 = e;
|
||||
}
|
||||
|
||||
try {
|
||||
__btoa(data);
|
||||
} catch (e) {
|
||||
err2 = e;
|
||||
err = e;
|
||||
}
|
||||
|
||||
validateInvalidCharacterError(err1);
|
||||
validateInvalidCharacterError(err2);
|
||||
validateInvalidCharacterError(err);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
var axios = require('../../index');
|
||||
var getAjaxRequest = require('./__getAjaxRequest');
|
||||
|
||||
describe('instance', function () {
|
||||
beforeEach(function () {
|
||||
jasmine.Ajax.install();
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
var axios = require('../../index');
|
||||
var getAjaxRequest = require('./__getAjaxRequest');
|
||||
|
||||
describe('interceptors', function () {
|
||||
beforeEach(function () {
|
||||
jasmine.Ajax.install();
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
var axios = require('../../index');
|
||||
var getAjaxRequest = require('./__getAjaxRequest');
|
||||
|
||||
describe('options', function () {
|
||||
beforeEach(function () {
|
||||
jasmine.Ajax.install();
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
require('es6-promise').polyfill();
|
||||
var axios = require('../../index');
|
||||
var getAjaxRequest = require('./__getAjaxRequest');
|
||||
|
||||
describe('promise', function () {
|
||||
beforeEach(function () {
|
||||
jasmine.Ajax.install();
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
var axios = require('../../index');
|
||||
var getAjaxRequest = require('./__getAjaxRequest');
|
||||
|
||||
describe('requests', function () {
|
||||
beforeEach(function () {
|
||||
jasmine.Ajax.install();
|
||||
@@ -154,6 +151,12 @@ describe('requests', function () {
|
||||
});
|
||||
|
||||
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);
|
||||
input[0] = 1;
|
||||
input[1] = 2;
|
||||
@@ -170,6 +173,12 @@ describe('requests', function () {
|
||||
});
|
||||
|
||||
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);
|
||||
input[0] = 1;
|
||||
input[1] = 2;
|
||||
@@ -186,6 +195,12 @@ describe('requests', function () {
|
||||
});
|
||||
|
||||
it('should support array buffer response', function (done) {
|
||||
// ArrayBuffer doesn't exist in IE8/9
|
||||
if (isOldIE && typeof ArrayBuffer === 'undefined') {
|
||||
done();
|
||||
return;
|
||||
}
|
||||
|
||||
var response;
|
||||
|
||||
function str2ab(str) {
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
var axios = require('../../index');
|
||||
var getAjaxRequest = require('./__getAjaxRequest');
|
||||
|
||||
describe('transform', function () {
|
||||
beforeEach(function () {
|
||||
jasmine.Ajax.install();
|
||||
|
||||
@@ -7,22 +7,41 @@ describe('utils::isX', 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({})).toEqual(false);
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
// TODO Blob is not a constructor in PhantomJS
|
||||
// it('should validate Blob', function () {
|
||||
// expect(utils.isBlob(new Blob())).toEqual(true);
|
||||
// });
|
||||
it('should validate Blob', function () {
|
||||
// Blob doesn't exist in IE8/9
|
||||
if (isOldIE && typeof Blob === 'undefined') {
|
||||
return;
|
||||
}
|
||||
|
||||
expect(utils.isBlob(new Blob())).toEqual(true);
|
||||
});
|
||||
|
||||
it('should validate String', function () {
|
||||
expect(utils.isString('')).toEqual(true);
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
var axios = require('../../index');
|
||||
var getAjaxRequest = require('./__getAjaxRequest');
|
||||
|
||||
describe('xsrf', function () {
|
||||
beforeEach(function () {
|
||||
jasmine.Ajax.install();
|
||||
|
||||
Reference in New Issue
Block a user