diff options
author | James Lal <james@lightsofapollo.com> | 2012-07-23 22:37:40 -0700 |
---|---|---|
committer | James Lal <james@lightsofapollo.com> | 2012-07-23 22:37:40 -0700 |
commit | 585956e08bb7afaa3001684e7a65bf462d7acaaa (patch) | |
tree | f6b5607cf116acf437e2d5da6e8e80e8d0c2dd5c | |
parent | 2a138899fd6cb44256f7b31765bbe8e52508df1a (diff) | |
download | jsCalDAV-585956e08bb7afaa3001684e7a65bf462d7acaaa.tar.gz |
xhr fixes for firefox
-rw-r--r-- | lib/caldav/xhr.js | 27 | ||||
-rw-r--r-- | test/caldav/xhr_test.js | 43 |
2 files changed, 66 insertions, 4 deletions
diff --git a/lib/caldav/xhr.js b/lib/caldav/xhr.js index ac26865..ff4bdd3 100644 --- a/lib/caldav/xhr.js +++ b/lib/caldav/xhr.js @@ -67,6 +67,18 @@ }, /** + * @param {String} user basic auth user. + * @param {String} password basic auth pass. + * @return {String} basic auth token. + */ + _credentials: function(user, pass) { + // this code should never run in nodejs. + return 'Basic ' + window.btoa( + user + ':' + pass + ); + }, + + /** * Sends request to server. * * @param {Function} callback success/failure handler. @@ -86,10 +98,18 @@ this.xhr = xhr; - if (Xhr.authHack) { - xhr.open(this.method, this.url, this.async); - } else { + // This hack is in place due to some platform + // bug in gecko when using mozSystem xhr + // the credentials only seem to work as expected + // when constructing them manually. + if (!this.globalXhrOptions || !this.globalXhrOptions.mozSystem) { xhr.open(this.method, this.url, this.async, this.user, this.password); + } else { + xhr.open(this.method, this.url, this.async); + xhr.setRequestHeader('Authorization', this._credentials( + this.user, + this.password + )); } for (header in this.headers) { @@ -98,7 +118,6 @@ } } - xhr.onreadystatechange = function onReadyStateChange() { var data; if (xhr.readyState === 4) { diff --git a/test/caldav/xhr_test.js b/test/caldav/xhr_test.js index 1082958..c71dc12 100644 --- a/test/caldav/xhr_test.js +++ b/test/caldav/xhr_test.js @@ -49,6 +49,21 @@ suite('webacls/xhr', function() { }); }); + test('#_credentials', function() { + // don't run this in node + if (typeof(window) === 'undefined') { + return; + } + + var user = 'james'; + var password = 'lal'; + var expected = 'Basic ' + window.btoa(user + ':' + password); + + assert.equal( + subject._credentials(user, password), expected + ); + }); + suite('.abort', function() { suite('when there is an xhr object', function() { var aborted; @@ -121,6 +136,34 @@ suite('webacls/xhr', function() { responseXhr = null; }); + test('with mozSystem', function() { + var user = 'user'; + var password = 'pass'; + var url = '/foo'; + + request({ + globalXhrOptions: { mozSystem: true }, + user: user, + password: password, + method: 'GET', + url: url + }); + + + subject.send(function() {}); + var args = subject.xhr.openArgs; + + assert.deepEqual( + args, + ['GET', url, true] + ); + + assert.equal( + subject.xhr.headers['Authorization'], + subject._credentials(user, password) + ); + }); + suite('when xhr is a success and responds /w data', function() { var response = '<html></html>', cb; |