Skip to content

Commit

Permalink
Fixes ladjs#258, handles undefined Response object rather than throwi…
Browse files Browse the repository at this point in the history
…ng an uncaught error
  • Loading branch information
mmerkes committed Jul 20, 2015
1 parent 95d0bf6 commit a431398
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,20 @@ Test.prototype.assert = function(resError, res, fn){

// status
if (status) {
if (resError && resError instanceof Error) {
// remove expected superagent error
if (resError.status === status) {
resError = null;
} else if (!res) {
return fn(resError);
}
}

if (res.status !== status) {
var a = http.STATUS_CODES[status];
var b = http.STATUS_CODES[res.status];
return fn(new Error('expected ' + status + ' "' + a + '", got ' + res.status + ' "' + b + '"'), res);
}

// remove expected superagent error
if (resError && resError instanceof Error && resError.status === status) {
resError = null;
}
}

// asserts
Expand Down
21 changes: 21 additions & 0 deletions test/supertest.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,27 @@ describe('request(app)', function(){
});
});
});

it('should handle an undefined Response', function (done) {
var app = express();

app.get('/', function(req, res){
setTimeout( function () {
res.end();
}, 20);
});

var s = app.listen(function(){
var url = 'http://localhost:' + s.address().port;
request(url)
.get('/')
.timeout(1)
.expect(200, function (err) {
err.should.be.an.instanceof(Error);
return done();
});
});
});
});

describe('.expect(status[, fn])', function(){
Expand Down

0 comments on commit a431398

Please sign in to comment.