forked from axios/axios
/
__helpers.js
125 lines (103 loc) · 3.25 KB
/
__helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Polyfill ES6 Promise
require('es6-promise').polyfill();
// Polyfill URLSearchParams
URLSearchParams = require('url-search-params');
// Import axios
axios = require('../../index');
// 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;
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();
}, 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();
console.log(request.requestHeaders['Authorization'], '\n\n\n');
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();
console.log(request.requestHeaders['Authorization'], '\n\n\n');
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();
});
});
};