Skip to content

Commit

Permalink
add .expect(status)
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jun 26, 2012
1 parent 959062c commit 4c31163
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
29 changes: 28 additions & 1 deletion lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Test.prototype.__proto__ = Request.prototype;
*/

Test.prototype.expect = function(val, fn){
var self = this;

switch (typeof val) {
// status
case 'number':
Expand All @@ -67,7 +69,12 @@ Test.prototype.expect = function(val, fn){
}

// callback
if ('function' == typeof fn) this.end(fn);
if ('function' == typeof fn) {
this.end(function(res){
self.assert(res, fn);
});
}

return this;
};

Expand All @@ -88,3 +95,23 @@ Test.prototype.end = function(fn){
this.url = 'http://' + addr.address + ':' + addr.port + this.url;
end.call(this, fn);
};

/**
* Perform assertions and invoke `fn(err)`.
*
* @param {Response} res
* @param {Function} fn
* @api private
*/

Test.prototype.assert = function(res, fn){
var status = this._status;

// status
if (status && res.status !== status) {
return fn(new Error('expected ' + status + ' response, got ' + res.status));
}

fn();
};

15 changes: 13 additions & 2 deletions test/supertest.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,19 @@ describe('request(app)', function(){
})

describe('.expect(status[, fn])', function(){
it('should assert the response status', function(){

it('should assert the response status', function(done){
var app = express();

app.get('/', function(req, res){
res.send('hey');
});

request(app)
.get('/')
.expect(404, function(err){
err.message.should.equal('expected 404 response, got 200');
done();
});
})
})
})

0 comments on commit 4c31163

Please sign in to comment.