Skip to content
Browse files

assert response body multiple times

  • Loading branch information...
1 parent 6d1e7b6 commit 9e2d3cb93416e10f344bf4b946854ce3f981552f @fengmk2 fengmk2 committed Nov 23, 2012
Showing with 26 additions and 6 deletions.
  1. +8 −6 lib/test.js
  2. +18 −0 test/supertest.js
View
14 lib/test.js
@@ -38,6 +38,7 @@ function Test(app, method, path) {
this.buffer();
this.app = app;
this._fields = {};
+ this._bodies = [];
this.url = 'string' == typeof app
? app + path
: this.serverAddress(app, path);
@@ -92,7 +93,7 @@ Test.prototype.expect = function(a, b, c){
if ('number' == typeof a) {
this._status = a;
// body
- if ('function' != typeof b) this._body = b;
+ if ('function' != typeof b) this._bodies.push(b);
return this;
}
@@ -103,7 +104,7 @@ Test.prototype.expect = function(a, b, c){
}
// body
- this._body = a;
+ this._bodies.push(a);
return this;
};
@@ -136,8 +137,7 @@ Test.prototype.end = function(fn){
Test.prototype.assert = function(res, fn){
var status = this._status
, fields = this._fields
- , body = this._body
- , isregexp = body instanceof RegExp
+ , bodies = this._bodies
, expected
, actual
, re;
@@ -148,9 +148,11 @@ Test.prototype.assert = function(res, fn){
var b = http.STATUS_CODES[res.status];
return fn(new Error('expected ' + status + ' "' + a + '", got ' + res.status + ' "' + b + '"'), res);
}
-
+
// body
- if (null != body) {
+ for (var i = 0; i < bodies.length; i++) {
+ var body = bodies[i];
+ var isregexp = body instanceof RegExp;
// parsed
if ('object' == typeof body && !isregexp) {
try {
View
18 test/supertest.js
@@ -239,6 +239,24 @@ describe('request(app)', function(){
done();
});
})
+
+ it('should assert response body multiple times', function(done){
+ var app = express();
+
+ app.get('/', function(req, res){
+ res.send('hey tj');
+ });
+
+ request(app)
+ .get('/')
+ .expect(/tj/)
+ .expect('hey')
+ .expect('hey tj')
+ .end(function (err, res) {
+ err.message.should.equal("expected 'hey' response body, got 'hey tj'");
+ done();
+ });
+ })
})
describe('.expect(field, value[, fn])', function(){

0 comments on commit 9e2d3cb

Please sign in to comment.
Something went wrong with that request. Please try again.