From 456913dc8b057cc44df753223e31800da62d8e98 Mon Sep 17 00:00:00 2001 From: James Lal Date: Fri, 10 May 2013 17:25:19 -0700 Subject: Rework http error validation so it can be reused r=kgrandon --- caldav.js | 62 +++++++++++++++++++++------------------ lib/caldav/http/basic_auth.js | 3 +- lib/caldav/http/oauth2.js | 2 ++ lib/caldav/request/abstract.js | 29 ++---------------- lib/caldav/xhr.js | 28 +++++++++++++++++- test/caldav/request/asset_test.js | 30 ++++++++++++++----- 6 files changed, 88 insertions(+), 66 deletions(-) diff --git a/caldav.js b/caldav.js index 6947e17..6f43d34 100644 --- a/caldav.js +++ b/caldav.js @@ -2107,6 +2107,7 @@ function write (chunk) { */ (function(module, ns) { var Native; + var Errors = ns.require('errors'); if (typeof(window) === 'undefined') { Native = require('xmlhttprequest').XMLHttpRequest; @@ -2114,6 +2115,19 @@ function write (chunk) { Native = window.XMLHttpRequest; } + function determineHttpStatusError(status) { + var message = 'Cannot handle request due to server response'; + var err = 'Unknown'; + + if (status === 500) + err = 'ServerFailure'; + + if (status === 401) + err = 'Authentication'; + + return new Errors[err](message); + } + /** * Creates a XHR wrapper. * Depending on the platform this is loaded @@ -2155,6 +2169,7 @@ function write (chunk) { password: null, url: null, streaming: true, + validateStatus: false, headers: {}, data: null, @@ -2272,7 +2287,18 @@ function write (chunk) { } this.waiting = false; - callback(null, this.xhr); + + if ( + !this.validateStatus || + ( + this.xhr.status > 199 && + this.xhr.status < 300 + ) + ) { + return callback(null, this.xhr); + } + + callback(determineHttpStatusError(this.xhr.status), this.xhr); } }.bind(this)); @@ -2581,7 +2607,8 @@ function write (chunk) { } BasicAuth.prototype = { - __proto__: XHR.prototype + __proto__: XHR.prototype, + validateStatus: true }; @@ -2640,6 +2667,8 @@ function write (chunk) { Oauth2.prototype = { __proto__: XHR.prototype, + validateStatus: true, + _sendXHR: function(xhr) { xhr.setRequestHeader( 'Authorization', 'Bearer ' + this.connection.oauth.access_token @@ -3166,20 +3195,6 @@ function write (chunk) { var SAX = ns.require('sax'); var XHR = ns.require('xhr'); - var Errors = ns.require('errors'); - - function determineHttpStatusError(status) { - var message = 'Cannot handle request due to server response'; - var err = 'Unknown'; - - if (status === 500) - err = 'ServerFailure'; - - if (status === 401) - err = 'Authentication'; - - return new Errors[err](message); - } /** * Creates an (Web/Cal)Dav request. @@ -3249,19 +3264,8 @@ function write (chunk) { return callback(err); } - // handle the success case - if (xhr.status > 199 && xhr.status < 300) { - self.sax.close(); - return self._processResult(req, callback); - } - - // probable error cases - callback( - determineHttpStatusError(xhr.status), - xhr - ); - - + self.sax.close(); + return self._processResult(req, callback); }); return req; diff --git a/lib/caldav/http/basic_auth.js b/lib/caldav/http/basic_auth.js index 07f083d..5b9f666 100644 --- a/lib/caldav/http/basic_auth.js +++ b/lib/caldav/http/basic_auth.js @@ -19,7 +19,8 @@ } BasicAuth.prototype = { - __proto__: XHR.prototype + __proto__: XHR.prototype, + validateStatus: true }; diff --git a/lib/caldav/http/oauth2.js b/lib/caldav/http/oauth2.js index 75e6334..8b3b9e5 100644 --- a/lib/caldav/http/oauth2.js +++ b/lib/caldav/http/oauth2.js @@ -44,6 +44,8 @@ Oauth2.prototype = { __proto__: XHR.prototype, + validateStatus: true, + _sendXHR: function(xhr) { xhr.setRequestHeader( 'Authorization', 'Bearer ' + this.connection.oauth.access_token diff --git a/lib/caldav/request/abstract.js b/lib/caldav/request/abstract.js index 6f33af4..acc91cc 100644 --- a/lib/caldav/request/abstract.js +++ b/lib/caldav/request/abstract.js @@ -2,20 +2,6 @@ var SAX = ns.require('sax'); var XHR = ns.require('xhr'); - var Errors = ns.require('errors'); - - function determineHttpStatusError(status) { - var message = 'Cannot handle request due to server response'; - var err = 'Unknown'; - - if (status === 500) - err = 'ServerFailure'; - - if (status === 401) - err = 'Authentication'; - - return new Errors[err](message); - } /** * Creates an (Web/Cal)Dav request. @@ -85,19 +71,8 @@ return callback(err); } - // handle the success case - if (xhr.status > 199 && xhr.status < 300) { - self.sax.close(); - return self._processResult(req, callback); - } - - // probable error cases - callback( - determineHttpStatusError(xhr.status), - xhr - ); - - + self.sax.close(); + return self._processResult(req, callback); }); return req; diff --git a/lib/caldav/xhr.js b/lib/caldav/xhr.js index 6c8351f..42e0c32 100644 --- a/lib/caldav/xhr.js +++ b/lib/caldav/xhr.js @@ -3,6 +3,7 @@ */ (function(module, ns) { var Native; + var Errors = ns.require('errors'); if (typeof(window) === 'undefined') { Native = require('xmlhttprequest').XMLHttpRequest; @@ -10,6 +11,19 @@ Native = window.XMLHttpRequest; } + function determineHttpStatusError(status) { + var message = 'Cannot handle request due to server response'; + var err = 'Unknown'; + + if (status === 500) + err = 'ServerFailure'; + + if (status === 401) + err = 'Authentication'; + + return new Errors[err](message); + } + /** * Creates a XHR wrapper. * Depending on the platform this is loaded @@ -51,6 +65,7 @@ password: null, url: null, streaming: true, + validateStatus: false, headers: {}, data: null, @@ -168,7 +183,18 @@ } this.waiting = false; - callback(null, this.xhr); + + if ( + !this.validateStatus || + ( + this.xhr.status > 199 && + this.xhr.status < 300 + ) + ) { + return callback(null, this.xhr); + } + + callback(determineHttpStatusError(this.xhr.status), this.xhr); } }.bind(this)); diff --git a/test/caldav/request/asset_test.js b/test/caldav/request/asset_test.js index 3f9ebcd..a125782 100644 --- a/test/caldav/request/asset_test.js +++ b/test/caldav/request/asset_test.js @@ -6,6 +6,7 @@ suite('caldav/request/asset.js', function() { // classes var Asset; var Xhr; + var Errors; var Connection; var SAX; var FakeXhr; @@ -29,6 +30,7 @@ suite('caldav/request/asset.js', function() { FakeXhr = Caldav.require('support/fake_xhr'); Xhr = Caldav.require('xhr'); Connection = Caldav.require('connection'); + Errors = Caldav.require('errors'); oldXhrClass = Xhr.prototype.xhrClass; Xhr.prototype.xhrClass = FakeXhr; @@ -82,17 +84,29 @@ suite('caldav/request/asset.js', function() { }); - test('#put', function(done) { - var content = 'foo'; + suite('#put', function() { + test('with error', function() { + subject.put({}, '', function(err) { + assert.ok(err, 'returns error'); + console.log(err); + assert.instanceOf(err, Errors.Authentication, 'assets validate http'); + }); - subject.put({ etag: 'x' }, content, function(err, data, xhr) { - assert.equal(xhr.openArgs[0], 'PUT'); - assert.equal(xhr.sendArgs[0], content); - done(); + var xhr = lastXHR(); + xhr.respond('', 401); }); - var xhr = lastXHR(); - xhr.respond('', 201); + test('success', function(done) { + var content = 'foo'; + subject.put({ etag: 'x' }, content, function(err, data, xhr) { + assert.equal(xhr.openArgs[0], 'PUT'); + assert.equal(xhr.sendArgs[0], content); + done(); + }); + + var xhr = lastXHR(); + xhr.respond('', 201); + }); }); test('#delete', function(done) { -- cgit From 5f35b858c31d455894f3c700eade1d1ca0ca4d3a Mon Sep 17 00:00:00 2001 From: James Lal Date: Fri, 10 May 2013 17:29:36 -0700 Subject: remove console.log --- test/caldav/request/asset_test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/caldav/request/asset_test.js b/test/caldav/request/asset_test.js index a125782..f1878ba 100644 --- a/test/caldav/request/asset_test.js +++ b/test/caldav/request/asset_test.js @@ -88,7 +88,6 @@ suite('caldav/request/asset.js', function() { test('with error', function() { subject.put({}, '', function(err) { assert.ok(err, 'returns error'); - console.log(err); assert.instanceOf(err, Errors.Authentication, 'assets validate http'); }); -- cgit