Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Rework http error validation so it can be reused r=kgrandon
Browse files Browse the repository at this point in the history
  • Loading branch information
lightsofapollo committed May 11, 2013
1 parent 9b5dd29 commit 456913d
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 66 deletions.
62 changes: 33 additions & 29 deletions caldav.js
Original file line number Diff line number Diff line change
Expand Up @@ -2107,13 +2107,27 @@ function write (chunk) {
*/
(function(module, ns) {
var Native;
var Errors = ns.require('errors');

if (typeof(window) === 'undefined') {
Native = require('xmlhttprequest').XMLHttpRequest;
} else {
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
Expand Down Expand Up @@ -2155,6 +2169,7 @@ function write (chunk) {
password: null,
url: null,
streaming: true,
validateStatus: false,

headers: {},
data: null,
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -2581,7 +2607,8 @@ function write (chunk) {
}

BasicAuth.prototype = {
__proto__: XHR.prototype
__proto__: XHR.prototype,
validateStatus: true
};


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion lib/caldav/http/basic_auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
}

BasicAuth.prototype = {
__proto__: XHR.prototype
__proto__: XHR.prototype,
validateStatus: true
};


Expand Down
2 changes: 2 additions & 0 deletions lib/caldav/http/oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
Oauth2.prototype = {
__proto__: XHR.prototype,

validateStatus: true,

_sendXHR: function(xhr) {
xhr.setRequestHeader(
'Authorization', 'Bearer ' + this.connection.oauth.access_token
Expand Down
29 changes: 2 additions & 27 deletions lib/caldav/request/abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
28 changes: 27 additions & 1 deletion lib/caldav/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@
*/
(function(module, ns) {
var Native;
var Errors = ns.require('errors');

if (typeof(window) === 'undefined') {
Native = require('xmlhttprequest').XMLHttpRequest;
} else {
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
Expand Down Expand Up @@ -51,6 +65,7 @@
password: null,
url: null,
streaming: true,
validateStatus: false,

headers: {},
data: null,
Expand Down Expand Up @@ -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));

Expand Down
30 changes: 22 additions & 8 deletions test/caldav/request/asset_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ suite('caldav/request/asset.js', function() {
// classes
var Asset;
var Xhr;
var Errors;
var Connection;
var SAX;
var FakeXhr;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 456913d

Please sign in to comment.