Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert response body multiple times #37

Merged
merged 1 commit into from Nov 27, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/test.js
Expand Up @@ -38,6 +38,7 @@ function Test(app, method, path) {
this.buffer(); this.buffer();
this.app = app; this.app = app;
this._fields = {}; this._fields = {};
this._bodies = [];
this.url = 'string' == typeof app this.url = 'string' == typeof app
? app + path ? app + path
: this.serverAddress(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) { if ('number' == typeof a) {
this._status = a; this._status = a;
// body // body
if ('function' != typeof b) this._body = b; if ('function' != typeof b) this._bodies.push(b);
return this; return this;
} }


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


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


return this; return this;
}; };
Expand Down Expand Up @@ -136,8 +137,7 @@ Test.prototype.end = function(fn){
Test.prototype.assert = function(res, fn){ Test.prototype.assert = function(res, fn){
var status = this._status var status = this._status
, fields = this._fields , fields = this._fields
, body = this._body , bodies = this._bodies
, isregexp = body instanceof RegExp
, expected , expected
, actual , actual
, re; , re;
Expand All @@ -148,9 +148,11 @@ Test.prototype.assert = function(res, fn){
var b = http.STATUS_CODES[res.status]; var b = http.STATUS_CODES[res.status];
return fn(new Error('expected ' + status + ' "' + a + '", got ' + res.status + ' "' + b + '"'), res); return fn(new Error('expected ' + status + ' "' + a + '", got ' + res.status + ' "' + b + '"'), res);
} }

// body // body
if (null != body) { for (var i = 0; i < bodies.length; i++) {
var body = bodies[i];
var isregexp = body instanceof RegExp;
// parsed // parsed
if ('object' == typeof body && !isregexp) { if ('object' == typeof body && !isregexp) {
try { try {
Expand Down
18 changes: 18 additions & 0 deletions test/supertest.js
Expand Up @@ -239,6 +239,24 @@ describe('request(app)', function(){
done(); 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(){ describe('.expect(field, value[, fn])', function(){
Expand Down