doug-martin / super-request

super-request is a supertest inspired HTTP assertion tester.
http://doug-martin.github.com/super-request/
MIT License
32 stars 12 forks source link

Chaining requests passing arguments between request not possible #5

Open mauron85 opened 10 years ago

mauron85 commented 10 years ago

Passing argument from one request response to another request is currently not possible

Expected behavior is to pass token from first response to next post request: Actual behavior is token is undefined.

var token;
request
    .post('/token')
    .auth({user: user.username + '#' + user.company, pass: user.password})
    .json(true)
    .end(function(err, res) {
        if (err) {
            return done(err);
        }
        token = res.body.access_token;
    })
    .post('/users')
    .headers({'Authorization': 'Bearer ' + token})
    .json(users)
    .end(function(err, res) {
        done();
    });

The current workaround is to use then:

request
    .post('/token')
    .auth({user: user.username + '#' + user.company, pass: user.password})
    .json(true)
    .end(function(err, res) {
        if (err) {
            return done(err);
        }
        token = res.body.access_token;
    }).then(function () {
        request
            .post('/users')
            .headers({'Authorization': 'Bearer ' + token})
            .json(users)
            .end(function(err, res) {
                done();
            });
    });
JemiloII commented 9 years ago

This is still an issue. I was trying the same exact thing.

dbellavista commented 8 years ago

If you use ES6 you can use a Proxy (I actually haven't tested it with headers, but should work):

"use strict";
class Header {
  get Authorization() {
    return this._auth;
  }
  set Authorization(value) {
    this._auth = value;
  }
}
const hd = new Header();

request
    .post('/token')
    .auth({user: user.username + '#' + user.company, pass: user.password})
    .json(true)
    .end(function(err, res) {
        if (err) {
            return done(err);
        }
        hd.Authorization = 'Bearer ' + res.body.access_token;
    })
    .post('/users')
    .headers(hd)
    .json(users)
    .end(function(err, res) {
        done();
    });