mirror of
https://github.com/tenrok/axios.git
synced 2026-06-11 18:02:32 +03:00
Polyfilling btoa where appropriate
Includes testing of the polyfills.
This commit is contained in:
+2
-1
@@ -8,6 +8,7 @@ var buildURL = require('./../helpers/buildURL');
|
||||
var parseHeaders = require('./../helpers/parseHeaders');
|
||||
var transformData = require('./../helpers/transformData');
|
||||
var isURLSameOrigin = require('./../helpers/isURLSameOrigin');
|
||||
var btoa = window.btoa || require('./../helpers/btoa.js')
|
||||
|
||||
module.exports = function xhrAdapter(resolve, reject, config) {
|
||||
// Transform request data
|
||||
@@ -41,9 +42,9 @@ module.exports = function xhrAdapter(resolve, reject, config) {
|
||||
|
||||
// HTTP basic authentication
|
||||
if (config.auth) {
|
||||
requestHeaders['Authorization'] = 'Basic: ' + window.btoa(username + ':' + password);
|
||||
var username = config.auth.username || '';
|
||||
var password = config.auth.password || '';
|
||||
requestHeaders['Authorization'] = 'Basic: ' + btoa(username + ':' + password);
|
||||
}
|
||||
|
||||
// Create the request
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
var axios = require('../../index');
|
||||
|
||||
describe('options', function () {
|
||||
describe('basicAuth without btoa polyfill', function () {
|
||||
beforeEach(function () {
|
||||
jasmine.Ajax.install();
|
||||
});
|
||||
@@ -9,66 +9,7 @@ describe('options', function () {
|
||||
jasmine.Ajax.uninstall();
|
||||
});
|
||||
|
||||
it('should accept HTTP Basic auth with user/pass', function (done) {
|
||||
var request;
|
||||
|
||||
axios({
|
||||
url: '/foo',
|
||||
auth: {
|
||||
user: 'Aladdin',
|
||||
pass: 'open sesame'
|
||||
}
|
||||
});
|
||||
|
||||
setTimeout(function () {
|
||||
request = jasmine.Ajax.requests.mostRecent();
|
||||
|
||||
expect(request.requestHeaders['Authorization']).toEqual('Basic: QWxhZGRpbjpvcGVuIHNlc2FtZQ==');
|
||||
done();
|
||||
}, 0);
|
||||
});
|
||||
|
||||
it('should accept HTTP Basic auth with username/pass', function (done) {
|
||||
var request;
|
||||
|
||||
axios({
|
||||
url: '/foo',
|
||||
auth: {
|
||||
username: 'Aladdin',
|
||||
pass: 'open sesame'
|
||||
}
|
||||
});
|
||||
|
||||
setTimeout(function () {
|
||||
request = jasmine.Ajax.requests.mostRecent();
|
||||
|
||||
expect(request.requestHeaders['Authorization']).toEqual('Basic: QWxhZGRpbjpvcGVuIHNlc2FtZQ==');
|
||||
done();
|
||||
}, 0);
|
||||
});
|
||||
|
||||
it('should accept HTTP Basic auth with user/password', function (done) {
|
||||
var request;
|
||||
|
||||
axios({
|
||||
url: '/foo',
|
||||
auth: {
|
||||
user: 'Aladdin',
|
||||
password: 'open sesame'
|
||||
}
|
||||
});
|
||||
|
||||
setTimeout(function () {
|
||||
request = jasmine.Ajax.requests.mostRecent();
|
||||
|
||||
expect(request.requestHeaders['Authorization']).toEqual('Basic: QWxhZGRpbjpvcGVuIHNlc2FtZQ==');
|
||||
done();
|
||||
}, 0);
|
||||
});
|
||||
|
||||
it('should accept HTTP Basic auth with username/password', function (done) {
|
||||
var request;
|
||||
|
||||
axios({
|
||||
url: '/foo',
|
||||
auth: {
|
||||
@@ -78,10 +19,25 @@ describe('options', function () {
|
||||
});
|
||||
|
||||
setTimeout(function () {
|
||||
request = jasmine.Ajax.requests.mostRecent();
|
||||
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({
|
||||
url: '/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) {
|
||||
expect(error.message).toEqual('INVALID_CHARACTER_ERR: DOM Exception 5')
|
||||
done()
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
var axios = require('../../index');
|
||||
|
||||
describe('basicAuth with btoa polyfill', function () {
|
||||
beforeAll(function() {
|
||||
this.original_btoa = window.btoa;
|
||||
window.btoa = undefined;
|
||||
})
|
||||
|
||||
afterAll(function() {
|
||||
window.btoa = this.original_btoa;
|
||||
})
|
||||
|
||||
beforeEach(function () {
|
||||
jasmine.Ajax.install();
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
jasmine.Ajax.uninstall();
|
||||
});
|
||||
|
||||
it('should accept HTTP Basic auth with username/password', function (done) {
|
||||
axios({
|
||||
url: '/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({
|
||||
url: '/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) {
|
||||
expect(error.message).toEqual('\'btoa\' failed: The string to be encoded contains characters outside of the Latin1 range.')
|
||||
done()
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user