diff --git a/test/js-mocha-specs.js b/test/js-mocha-specs.js index 47efd98..10bb23c 100644 --- a/test/js-mocha-specs.js +++ b/test/js-mocha-specs.js @@ -15,6 +15,9 @@ describe('Advanced HTTP www interface', function() { this.timeout(900000); beforeEach(() => { + // mocked btoa function (base 64 encoding strings) + global.btoa = decoded => new Buffer(decoded).toString('base64'); + mock('cordova/exec', noop); mock(`${PLUGIN_ID}.angular-integration`, { registerService: noop }); mock(`${PLUGIN_ID}.cookie-handler`, {}); @@ -91,4 +94,9 @@ describe('Advanced HTTP www interface', function() { http.get('https://www.google.de/?gws_rd=ssl', {}, { myKey: 'myValue' }, noop, noop); }); + + it('sets basic authentication header correctly', () => { + http.useBasicAuth('name', 'pass'); + http.headers['*'].Authorization.should.equal('Basic bmFtZTpwYXNz'); + }); }); diff --git a/www/advanced-http.js b/www/advanced-http.js index 67e5b1d..37dfd84 100644 --- a/www/advanced-http.js +++ b/www/advanced-http.js @@ -151,8 +151,7 @@ var http = { return {'Authorization': 'Basic ' + b64EncodeUnicode(username + ':' + password)}; }, useBasicAuth: function (username, password) { - this.headers['*'] = this.headers['*'] || {}; - this.headers['*'].Authorization = 'Basic ' + b64EncodeUnicode(username + ':' + password); + this.setHeader('*', 'Authorization', 'Basic ' + b64EncodeUnicode(username + ':' + password)); }, setHeader: function () { // this one is for being backward compatible