Skip to content

Commit

Permalink
add parsed body assertion support. Closes ladjs#1
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jul 2, 2012
1 parent 0fb6410 commit cb54dc1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 20 deletions.
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ describe('GET /users', function(){

### .expect(body[, fn])

Assert response `body` text with a string or regular expression.
Assert response `body` text with a string, regular expression, or
parsed body object.

### .expect(field, value[, fn])

Expand Down
54 changes: 35 additions & 19 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
var request = require('superagent')
, util = require('util')
, http = require('http')
, assert = require('assert')
, Request = request.Request;

/**
Expand Down Expand Up @@ -70,19 +71,18 @@ Test.prototype.expect = function(val, fn){
this._status = val;
}

// callback
if ('function' == typeof fn) return this.end(fn);

// header field
if ('string' == typeof fn || fn instanceof RegExp) {
this._fields[val] = fn;
fn = arguments[2];
// 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;
}
} else {
this._body = val;
}

// callback
if ('function' == typeof fn) this.end(fn);

return this;
};
Expand All @@ -101,6 +101,7 @@ Test.prototype.end = function(fn){
end.call(this, function(res){
self.assert(res, fn);
});
return this;
};

/**
Expand All @@ -115,6 +116,7 @@ Test.prototype.assert = function(res, fn){
var status = this._status
, fields = this._fields
, body = this._body
, isregexp = body instanceof RegExp
, expected
, actual
, re;
Expand All @@ -127,17 +129,31 @@ Test.prototype.assert = function(res, fn){
}

// body
if (null != body && body !== res.text) {
var a = util.inspect(body);
var b = util.inspect(res.text);

// regexp
if (body instanceof RegExp) {
if (!body.test(res.text)) {
return fn(new Error('expected body ' + b + ' to match ' + body));
if (null != body) {
// parsed
if ('object' == typeof body && !isregexp) {
try {
assert.deepEqual(body, res.body);
} catch (err) {
var a = util.inspect(body);
var b = util.inspect(res.body);
return fn(new Error('expected ' + a + ' response body, got ' + b));
}
} else {
return fn(new Error('expected ' + a + ' response body, got ' + b));
// string
if (body !== res.text) {
var a = util.inspect(body);
var b = util.inspect(res.text);

// regexp
if (isregexp) {
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));
}
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions test/supertest.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,28 @@ describe('request(app)', function(){
.expect('{"foo":"bar"}', done);
})

it('should assert the parsed response body', function(done){
var app = express();

app.set('json spaces', 0);

app.get('/', function(req, res){
res.send({ foo: 'bar' });
});

request(app)
.get('/')
.expect({ foo: 'baz' })
.end(function(err, res){
err.message.should.equal('expected { foo: \'baz\' } response body, got { foo: \'bar\' }');

request(app)
.get('/')
.expect({ foo: 'bar' })
.end(done);
});
})

it('should support regular expressions', function(done){
var app = express();

Expand Down

0 comments on commit cb54dc1

Please sign in to comment.