From 70417759433e0293f2ffe5ca1e0278945b104ae0 Mon Sep 17 00:00:00 2001 From: Matt Zabriskie Date: Thu, 3 Mar 2016 22:43:16 -0700 Subject: [PATCH] Changing btoa ponyfill to improve testing --- lib/helpers/btoa.js | 12 ++++++------ test/specs/__setupBasicAuthTest.js | 3 ++- test/specs/__validateInvalidCharacterError.js | 4 ++++ test/specs/helpers/btoa.spec.js | 4 +++- 4 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 test/specs/__validateInvalidCharacterError.js diff --git a/lib/helpers/btoa.js b/lib/helpers/btoa.js index 5d0b378..2fe5014 100644 --- a/lib/helpers/btoa.js +++ b/lib/helpers/btoa.js @@ -4,12 +4,12 @@ var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; -function InvalidCharacterError(message) { - this.message = message; +function E() { + this.message = 'String contains an invalid character'; } -InvalidCharacterError.prototype = new Error; -InvalidCharacterError.prototype.code = 5; -InvalidCharacterError.prototype.name = 'InvalidCharacterError'; +E.prototype = new Error; +E.prototype.code = 5; +E.prototype.name = 'InvalidCharacterError'; function btoa(input) { var str = String(input); @@ -26,7 +26,7 @@ function btoa(input) { ) { charCode = str.charCodeAt(idx += 3 / 4); if (charCode > 0xFF) { - throw new InvalidCharacterError('INVALID_CHARACTER_ERR: DOM Exception 5'); + throw new E(); } block = block << 8 | charCode; } diff --git a/test/specs/__setupBasicAuthTest.js b/test/specs/__setupBasicAuthTest.js index 3fccf1b..1148f33 100644 --- a/test/specs/__setupBasicAuthTest.js +++ b/test/specs/__setupBasicAuthTest.js @@ -1,4 +1,5 @@ var axios = require('../../index'); +var validateInvalidCharacterError = require('./__validateInvalidCharacterError'); module.exports = function setupBasicAuthTest() { beforeEach(function () { @@ -36,7 +37,7 @@ module.exports = function setupBasicAuthTest() { }).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'); + validateInvalidCharacterError(error); done(); }); }); diff --git a/test/specs/__validateInvalidCharacterError.js b/test/specs/__validateInvalidCharacterError.js new file mode 100644 index 0000000..072ac4c --- /dev/null +++ b/test/specs/__validateInvalidCharacterError.js @@ -0,0 +1,4 @@ +module.exports = function validateInvalidCharacterError(error) { + expect(error instanceof Error).toEqual(true); + expect(/character/i.test(error.message)).toEqual(true); +}; diff --git a/test/specs/helpers/btoa.spec.js b/test/specs/helpers/btoa.spec.js index 89a4403..79b76b2 100644 --- a/test/specs/helpers/btoa.spec.js +++ b/test/specs/helpers/btoa.spec.js @@ -1,4 +1,5 @@ var __btoa = require('../../../lib/helpers/btoa'); +var validateInvalidCharacterError = require('../__validateInvalidCharacterError'); describe('btoa polyfill', function () { it('should behave the same as native window.btoa', function () { @@ -22,6 +23,7 @@ describe('btoa polyfill', function () { err2 = e; } - expect(err1.message).toEqual(err2.message); + validateInvalidCharacterError(err1); + validateInvalidCharacterError(err2); }); });