Permalink
Browse files

add field assertions

  • Loading branch information...
1 parent 76df78a commit 24e6a5df03b1c102627f805bf4034bd244b9cbdd @tj tj committed Jun 26, 2012
Showing with 31 additions and 2 deletions.
  1. +13 −2 lib/test.js
  2. +18 −0 test/supertest.js
View
@@ -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) {
@@ -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);
};
View
@@ -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.