Skip to content

Commit

Permalink
Merge pull request #10 from oponder/master
Browse files Browse the repository at this point in the history
Also apply 'sugar' behavior on error responses
  • Loading branch information
Florent Dubost committed Sep 9, 2015
2 parents 5450145 + 800ac62 commit 430d4d9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ module.exports = [
* Returning error codes example:
* request.get('https://domain.example/404').end(function(err, res){
* console.log(err); // 404
* console.log(res.notFound); // true
* })
*/
if (match[1] === '/404') {
Expand Down
19 changes: 18 additions & 1 deletion lib/superagent-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = mock;
function mock (superagent, config) {
var Request = superagent.Request;
var parsers = Object.create(null);
var response = {};

/**
* Keep the default methods
Expand Down Expand Up @@ -95,7 +96,23 @@ function mock (superagent, config) {
var fixtures = parser.fixtures(match, this.params, this.headers);
fn(null, parsers[this.url].callback(match, fixtures));
} catch(err) {
fn(err, undefined);
response = new superagent.Response({
res: {
headers: {},
setEncoding: function (){},
on: function (){}
},
req: {
method: function (){}
},
xhr: {
responseType: '',
getAllResponseHeaders: function () {return 'a header';},
getResponseHeader: function () {return 'a header';}
}
});
response.setStatusProperties(err.message);
fn(err, response);
}
} else {
oldEnd.call(this, fn);
Expand Down
2 changes: 1 addition & 1 deletion tests/support/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = [
pattern: 'https://error.example/(\\w+)',
fixtures: function (match) {
var code = (match || [])[1] || 404;
var newErr = new Error(http.STATUS_CODES[code]);
var newErr = new Error(parseInt(code));
newErr.response = http.STATUS_CODES[code];
newErr.status = code;
throw newErr;
Expand Down
42 changes: 39 additions & 3 deletions tests/support/expectations.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,24 @@ module.exports = function (request, config) {
});
},

'catches error and response it': function (test) {
'catches not found error and response it': function (test) {
request.get('https://error.example/404')
.end(function (err, result) {
test.notEqual(err, null);
test.equal(err.status, 404);
test.equal(err.response, http.STATUS_CODES[404]);
test.ok(result.notFound)
test.done();
});
},

'catches unauthorized error and response it': function (test) {
request.get('https://error.example/401')
.end(function (err, result) {
test.notEqual(err, null);
test.equal(err.status, 401);
test.equal(err.response, http.STATUS_CODES[401]);
test.ok(result.unauthorized)
test.done();
});
},
Expand Down Expand Up @@ -232,12 +244,24 @@ module.exports = function (request, config) {
});
},

'catches error and response it': function (test) {
'catches not found error and response it': function (test) {
request.post('https://error.example/404')
.end(function (err, result) {
test.notEqual(err, null);
test.equal(err.status, 404);
test.equal(err.response, http.STATUS_CODES[404]);
test.ok(result.notFound)
test.done();
});
},

'catches unauthorized error and response it': function (test) {
request.post('https://error.example/401')
.end(function (err, result) {
test.notEqual(err, null);
test.equal(err.status, 401);
test.equal(err.response, http.STATUS_CODES[401]);
test.ok(result.unauthorized)
test.done();
});
},
Expand Down Expand Up @@ -344,12 +368,24 @@ module.exports = function (request, config) {
});
},

'catches error and response it': function (test) {
'catches not found error and response it': function (test) {
request.put('https://error.example/404')
.end(function (err, result) {
test.notEqual(err, null);
test.equal(err.status, 404);
test.equal(err.response, http.STATUS_CODES[404]);
test.ok(result.notFound)
test.done();
});
},

'catches unauthorized error and response it': function (test) {
request.put('https://error.example/401')
.end(function (err, result) {
test.notEqual(err, null);
test.equal(err.status, 401);
test.equal(err.response, http.STATUS_CODES[401]);
test.ok(result.unauthorized)
test.done();
});
},
Expand Down

0 comments on commit 430d4d9

Please sign in to comment.