Skip to content

Commit

Permalink
Merge pull request #37 from fengmk2/support-expect-many
Browse files Browse the repository at this point in the history
assert response body multiple times
  • Loading branch information
tj committed Nov 27, 2012
2 parents 6d1e7b6 + 9e2d3cb commit bd84172
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand All @@ -103,7 +104,7 @@ Test.prototype.expect = function(a, b, c){
}

// body
this._body = a;
this._bodies.push(a);

return this;
};
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions test/supertest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down

0 comments on commit bd84172

Please sign in to comment.