Permalink
Browse files

add body assertion regexp support

  • Loading branch information...
1 parent 50c6277 commit 402d9bd474805b1937242716b2085987b6015725 @tj tj committed Jun 26, 2012
Showing with 40 additions and 15 deletions.
  1. +23 −15 lib/test.js
  2. +17 −0 test/supertest.js
View
@@ -53,20 +53,20 @@ Test.prototype.__proto__ = Request.prototype;
Test.prototype.expect = function(val, fn){
var self = this;
- switch (typeof val) {
- // status
- case 'number':
- this._status = val;
- break;
- // body
- case 'string':
- // header field
- if ('string' == typeof fn || fn instanceof RegExp) {
- this._fields[val] = fn;
- fn = arguments[2];
- } else {
- this._body = val;
- }
+ // status
+ if ('number' == typeof val) {
+ this._status = val;
+ }
+
+ // body
+ if ('string' == typeof val || val instanceof RegExp) {
+ // header field
+ if ('string' == typeof fn || fn instanceof RegExp) {
+ this._fields[val] = fn;
+ fn = arguments[2];
+ } else {
+ this._body = val;
+ }
}
// callback
@@ -120,7 +120,15 @@ Test.prototype.assert = function(res, fn){
if (null != body && body !== res.text) {
var a = util.inspect(body);
var b = util.inspect(res.text);
- return fn(new Error('expected ' + a + ' response body, got ' + b));
+
+ // regexp
+ if (body instanceof RegExp) {
+ if (!body.test(res.text)) {
+ return fn(new Error('expected body ' + b + ' to match ' + body));
+ }
+ } else {
+ return fn(new Error('expected ' + a + ' response body, got ' + b));
+ }
}
// fields
View
@@ -73,6 +73,23 @@ describe('request(app)', function(){
done();
});
})
+
+ it('should support regular expressions', function(done){
+ var app = express();
+
+ app.get('/', function(req, res){
+ res.send('foobar');
+ });
+
+ request(app)
+ .get('/')
+ .expect(/^bar/)
+ .end(function(err, res){
+ console.log(err.message);
+ err.message.should.equal('expected body \'foobar\' to match /^bar/');
+ done();
+ });
+ })
})
describe('.expect(field, value[, fn])', function(){

0 comments on commit 402d9bd

Please sign in to comment.