Skip to content

Commit

Permalink
add field assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jun 26, 2012
1 parent 76df78a commit 24e6a5d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ Test.prototype.end = function(fn){
*/

Test.prototype.assert = function(res, fn){
var status = this._status;
var body = this._body;
var status = this._status
, fields = this._fields
, body = this._body
, expected
, actual;

// status
if (status && res.status !== status) {
Expand All @@ -119,6 +122,14 @@ Test.prototype.assert = function(res, fn){
return fn(new Error('expected ' + a + ' response body, got ' + b));
}

// fields
for (var field in fields) {
expected = fields[field];
actual = res.header[field.toLowerCase()];
if (expected == actual) continue;
return fn(new Error('expected "' + field + '" of "' + expected + '", got "' + actual + '"'));
}

fn(null, res);
};

18 changes: 18 additions & 0 deletions test/supertest.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,22 @@ describe('request(app)', function(){
});
})
})

describe('.expect(field, value[, fn])', function(){
it('should assert the header field value', function(done){
var app = express();

app.get('/', function(req, res){
res.send({ foo: 'bar' });
});

request(app)
.get('/')
.expect('Content-Type', 'text/html')
.end(function(err, res){
err.message.should.equal('expected "Content-Type" of "text/html", got "application/json; charset=utf-8"');
done();
});
})
})
})

0 comments on commit 24e6a5d

Please sign in to comment.