Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

assert response body multiple times #37

Merged
merged 1 commit into from

2 participants

@fengmk2

I found out supertest only check body only once.

Some scenes, we need to check different texts check for body.

@fengmk2

Unit test all pass on my mbp.

$ make test


  request(app)
    ✔ should fire up the app on an ephemeral port 
    ✔ should work with an active server 
    ✔ should work with remote server 
    ✔ should work with a https server 
    ✔ should work with .send() etc 
    ✔ should work when unbuffered 
    ✔ should default redirects to 0 (69ms)
    .expect(status[, fn])
      ✔ should assert the response status 
    .expect(status, body[, fn])
      ✔ should assert the response body and status 
      when the body argument is an empty string
        ✔ should not quietly pass on failure 
    .expect(body[, fn])
      ✔ should assert the response body 
      ✔ should assert the response text 
      ✔ should assert the parsed response body 
      ✔ should support regular expressions 
      ✔ should assert response body multiple times 
    .expect(field, value[, fn])
      ✔ should assert the header field presence 
      ✔ should assert the header field value 
      ✔ should assert multiple fields 
      ✔ should support regular expressions 


  ✔ 19 tests complete (333 ms)
@fengmk2

@visionmedia Please review this when you are free.

@fengmk2

@visionmedia Can you review this?

@tj tj merged commit bd84172 into visionmedia:master
@tj
Owner
tj commented

we should also add a test that doesn't pass an exception, to show that it does in fact work with multiple valid assertions

@fengmk2

OK, I do that right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 23, 2012
  1. @fengmk2
This page is out of date. Refresh to see the latest.
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(){
Something went wrong with that request. Please try again.