diff --git a/lib/test.js b/lib/test.js index f23ec8cd..8282f72f 100644 --- a/lib/test.js +++ b/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 { diff --git a/test/supertest.js b/test/supertest.js index cf071570..122bcaef 100644 --- a/test/supertest.js +++ b/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(){