Permalink
Browse files

add regexp support to field assertions

  • Loading branch information...
1 parent 88f53a1 commit 50c6277482d46d8bbeb281b0f57e8330bd7e48d8 @tj tj committed Jun 26, 2012
Showing with 22 additions and 2 deletions.
  1. +6 −2 lib/test.js
  2. +16 −0 test/supertest.js
View
@@ -61,7 +61,7 @@ Test.prototype.expect = function(val, fn){
// body
case 'string':
// header field
- if ('string' == typeof fn) {
+ if ('string' == typeof fn || fn instanceof RegExp) {
this._fields[val] = fn;
fn = arguments[2];
} else {
@@ -108,7 +108,8 @@ Test.prototype.assert = function(res, fn){
, fields = this._fields
, body = this._body
, expected
- , actual;
+ , actual
+ , re;
// status
if (status && res.status !== status) {
@@ -128,6 +129,9 @@ Test.prototype.assert = function(res, fn){
actual = res.header[field.toLowerCase()];
if (null == actual) return fn(new Error('expected "' + field + '" header field'));
if (expected == actual) continue;
+ if (expected instanceof RegExp) re = expected;
+ if (re && re.test(actual)) continue;
+ if (re) return fn(new Error('expected "' + field + '" matching ' + expected + ', got "' + actual + '"'));
return fn(new Error('expected "' + field + '" of "' + expected + '", got "' + actual + '"'));
}
View
@@ -121,5 +121,21 @@ describe('request(app)', function(){
.expect('Content-Length', '3')
.end(done);
})
+
+ it('should support regular expressions', function(done){
+ var app = express();
+
+ app.get('/', function(req, res){
+ res.send('hey');
+ });
+
+ request(app)
+ .get('/')
+ .expect('Content-Type', /^application/)
+ .end(function(err){
+ err.message.should.equal('expected "Content-Type" matching /^application/, got "text/html; charset=utf-8"');
+ done();
+ });
+ })
})
})

0 comments on commit 50c6277

Please sign in to comment.