From 9ede644b98b03a29c66dfd5d90ab3d6a6f64ef5c Mon Sep 17 00:00:00 2001 From: Matt Zabriskie Date: Mon, 14 Dec 2015 12:16:57 -0700 Subject: [PATCH] Better tests for btoa --- test/specs/__setupBasicAuthTest.js | 43 ++++++++++++++++++++ test/specs/basicAuth.spec.js | 42 +------------------ test/specs/basicAuthWithPolyfill.spec.js | 51 +++++------------------- test/specs/helpers/btoa.spec.js | 27 +++++++++++++ 4 files changed, 82 insertions(+), 81 deletions(-) create mode 100644 test/specs/__setupBasicAuthTest.js create mode 100644 test/specs/helpers/btoa.spec.js diff --git a/test/specs/__setupBasicAuthTest.js b/test/specs/__setupBasicAuthTest.js new file mode 100644 index 0000000..8436026 --- /dev/null +++ b/test/specs/__setupBasicAuthTest.js @@ -0,0 +1,43 @@ +var axios = require('../../index'); + +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({ + 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('INVALID_CHARACTER_ERR: DOM Exception 5'); + done(); + }); + }); +}; diff --git a/test/specs/basicAuth.spec.js b/test/specs/basicAuth.spec.js index 0aae6b5..742fc25 100644 --- a/test/specs/basicAuth.spec.js +++ b/test/specs/basicAuth.spec.js @@ -1,43 +1,5 @@ -var axios = require('../../index'); +var setupBasicAuthTest = require('./__setupBasicAuthTest'); describe('basicAuth without btoa polyfill', function () { - 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('INVALID_CHARACTER_ERR: DOM Exception 5') - done() - }); - }); + setupBasicAuthTest(); }); diff --git a/test/specs/basicAuthWithPolyfill.spec.js b/test/specs/basicAuthWithPolyfill.spec.js index 83aaf9b..13de4c8 100644 --- a/test/specs/basicAuthWithPolyfill.spec.js +++ b/test/specs/basicAuthWithPolyfill.spec.js @@ -1,52 +1,21 @@ -var axios = require('../../index'); +var setupBasicAuthTest = require('./__setupBasicAuthTest'); +var window_btoa; describe('basicAuth with btoa polyfill', function () { beforeAll(function() { - this.original_btoa = window.btoa; + window_btoa = window.btoa; window.btoa = undefined; - }) + }); afterAll(function() { - window.btoa = this.original_btoa; - }) - - beforeEach(function () { - jasmine.Ajax.install(); + window.btoa = window_btoa; + window_btoa = undefined; }); - afterEach(function () { - jasmine.Ajax.uninstall(); + it('should not have native window.btoa', function () { + expect(window.btoa).toEqual(undefined); }); - 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() - }); - }); + setupBasicAuthTest(); }); + diff --git a/test/specs/helpers/btoa.spec.js b/test/specs/helpers/btoa.spec.js new file mode 100644 index 0000000..89a4403 --- /dev/null +++ b/test/specs/helpers/btoa.spec.js @@ -0,0 +1,27 @@ +var __btoa = require('../../../lib/helpers/btoa'); + +describe('btoa polyfill', function () { + it('should behave the same as native window.btoa', function () { + 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 data = 'I ♡ Unicode!'; + + try { + window.btoa(data); + } catch (e) { + err1 = e; + } + + try { + __btoa(data); + } catch (e) { + err2 = e; + } + + expect(err1.message).toEqual(err2.message); + }); +});