How to chain http calls with superagent/supertest?

2019-03-10 16:35发布

I am testing an express API with supertest.

I couldn't get multiple requests in a test case to work with supertest. Below is what i tried in a test case. But the test case seem to only execute the last call which is the HTTP GET.

it('should respond to GET with added items', function(done) {
    var agent = request(app);
    agent.post('/player').type('json').send({name:"Messi"});
    agent.post('/player').type('json').send({name:"Maradona"});
    agent.get('/player').set("Accept", "application/json")
        .expect(200)
        .end(function(err, res) {
            res.body.should.have.property('items').with.lengthOf(2);
            done();
    });
);

Anything i am missing here, or is there another way to chain http calls with superagent?

4条回答
我想做一个坏孩纸
2楼-- · 2019-03-10 17:03

I built upon Tim’s reply but used async.waterfall instead, to be able to do assert tests on the results (note: I use Tape here instead of Mocha):

test('Test the entire API', function (assert) {
    const app = require('../app/app');
    async.waterfall([
            (cb) => request(app).get('/api/accounts').expect(200, cb),
            (results, cb) => { assert.ok(results.body.length, 'Returned accounts list'); cb(null, results); },
            (results, cb) => { assert.ok(results.body[0].reference, 'account #0 has reference'); cb(null, results); },
            (results, cb) => request(app).get('/api/plans').expect(200, cb),
            (results, cb) => request(app).get('/api/services').expect(200, cb),
            (results, cb) => request(app).get('/api/users').expect(200, cb),
        ],
        (err, results) => {
            app.closeDatabase();
            assert.end();
        }
    );
});
查看更多
Lonely孤独者°
3楼-- · 2019-03-10 17:09

This can be most elegantly solved with promises, and there's a really useful library to use promises with supertest: https://www.npmjs.com/package/supertest-as-promised

Their example:

return request(app)
  .get("/user")
  .expect(200)
  .then(function (res) {
    return request(app)
      .post("/kittens")
      .send({ userId: res})
      .expect(201);
  })
  .then(function (res) {
    // ... 
  });
查看更多
一夜七次
4楼-- · 2019-03-10 17:23

Tried to put this in a comment above, formatting wasn't working out.

I'm using async, which is really standard and works really well.

it('should respond to only certain methods', function(done) {
    async.series([
        function(cb) { request(app).get('/').expect(404, cb); },
        function(cb) { request(app).get('/new').expect(200, cb); },
        function(cb) { request(app).post('/').send({prop1: 'new'}).expect(404, cb); },
        function(cb) { request(app).get('/0').expect(200, cb); },
        function(cb) { request(app).get('/0/edit').expect(404, cb); },
        function(cb) { request(app).put('/0').send({prop1: 'new value'}).expect(404, cb); },
        function(cb) { request(app).delete('/0').expect(404, cb); },
    ], done);
});
查看更多
等我变得足够好
5楼-- · 2019-03-10 17:29

The calls are made asynchronous, so you need to use callback functions to chain them.

it('should respond to GET with added items', function(done) {
    var agent = request(app);
    agent.post('/player').type('json').send({name:"Messi"}).end(function(){
        agent.post('/player').type('json').send({name:"Maradona"}).end(function(){
            agent.get('/player')
                .set("Accept", "application/json")
                .expect(200)
                .end(function(err, res) {
                    res.body.should.have.property('items').with.lengthOf(2);
                    done();
                });
        });
    });
});
查看更多
登录 后发表回答