Testing requests that redirect with mocha/supertest in node

There is built-in assertion for this in supertest: should = require(‘should’) request = require(‘supertest’) app = require(‘../../app’) describe ‘authentication’, -> describe ‘POST /sessions’, -> describe ‘success’, -> it ‘redirects to the right path’, (done) -> request(app) .post(‘/sessions’) .send(user: ‘username’, password: ‘password’) .expect(302) .expect(‘Location’, ‘/home’) .end(done)

How to chain http calls with superagent/supertest?

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(“https://stackoverflow.com/”).expect(404, cb); }, function(cb) { request(app).get(‘/new’).expect(200, cb); }, function(cb) { request(app).post(“https://stackoverflow.com/”).send({prop1: ‘new’}).expect(404, cb); }, function(cb) { request(app).get(‘/0’).expect(200, cb); }, function(cb) { … Read more

How to send query string parameters using supertest?

Although supertest is not that well documented, you can have a look into the tests/supertest.js. There you have a test suite only for the query strings. Something like: request(app) .get(“https://stackoverflow.com/”) .query({ val: ‘Test1’ }) .expect(200, function(err, res) { res.text.should.be.equal(‘Test1’); done(); }); Therefore: .query({ key1: value1, … keyN: valueN }) should work.

How to authenticate Supertest requests with Passport?

As zeMirco points out, the underlying superagent module supports sessions, automatically maintaining cookies for you. However, it is possible to use the superagent.agent() functionality from supertest, through an undocumented feature. Simply use require(‘supertest’).agent(‘url’) instead of require(‘supertest’)(‘url’): var request = require(‘supertest’); var server = request.agent(‘http://localhost:3000’); describe(‘GET /api/getDir’, function(){ it(‘login’, loginUser()); it(‘uri that requires user to be … Read more