diff --git a/lib/adapters/xhr.js b/lib/adapters/xhr.js index 2615c10..21badcf 100644 --- a/lib/adapters/xhr.js +++ b/lib/adapters/xhr.js @@ -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 diff --git a/test/specs/basicAuth.spec.js b/test/specs/basicAuth.spec.js index 4816e43..0aae6b5 100644 --- a/test/specs/basicAuth.spec.js +++ b/test/specs/basicAuth.spec.js @@ -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() + }); + }); }); diff --git a/test/specs/basicAuthWithPolyfill.spec.js b/test/specs/basicAuthWithPolyfill.spec.js new file mode 100644 index 0000000..83aaf9b --- /dev/null +++ b/test/specs/basicAuthWithPolyfill.spec.js @@ -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() + }); + }); +});