Skip to content
This repository

Access to superagent's agent? #26

Closed
truk opened this Issue September 30, 2012 · 18 comments
Kurt Eldridge

Supertest exposes the superagent Request methods, but not the agent. Could it expose agent so you could use supertest with sessions/cookies?

Aseem Kishore

I was just about to request this too. I'm using Supertest to test our website and Ajax APIs, and was really looking forward to the automatic cookie/session support in the update to Supertest/Superagent. Thanks!

Kurt Eldridge

A workaround is that you can require both supertest and superagent and use superagent to get the agent for the tests that need sessions and cookies. But you also have to then fully qualify your urls. This addition would be a nice convenience.

Aseem Kishore

How do you hook up the Superagent agent to the Supertest functionality?

Kurt Eldridge

I didn't. Obviously you could, and that's what I'm suggesting in this issue. For now, I was just using both of them

var request = require('supertest')
, agent = require('superagent').agent
, express = require('express')
, app = require('../app');
...

// supertest
describe('GET /admin/project', function(){
describe('when the user is not looged in as admin', function(){
it('should return to the admin home page', function(done){
request(app)
.get('/admin/project')

...
// superagent
describe('when an admin is logged in', function(){
it('should show the list of projects', function(done){
var user = agent();
user
.post('http://localhost:3000/admin')

I'd love too see that feature implemented, even wanted to do this myself, but unfortunatelly superagent's agent implementation doesn't fit well in current supertest design :(

Aseem Kishore

Perhaps supertest could expose a similar agent API as superagent?

var request = require('supertest').agent();

request(app)
  .get('/login')
  // ...

request(app)
  .get('/admin')
  // ...

I made it working although in not prettiest way (#32). In my fork you can use supertest right now like that:
request(app).agent()

to access superagent's agent.

Finally I've wrote separate module (https://github.com/codefather/supertest-chai) which is not based but very similar to supertest.
It doesn't have sexy supertest's expect API, but relies on chai.js (with some customs assertions) to do the work.
Superagent's agent works with no problems.

Example:

var chaiSupertest = require('chai-supertest');
var request = chaiSupertest.request;
var chai = require("chai");
chai.should();
chai.use(chaiSupertest.httpAsserts);

var express = require('express');

var app = express();

app.get('/user', function (req, res) {
    res.send(201, { name: 'tobi' });
});

app.get('/questions', function (req, res) {
    res.send(200, 'test');
});

request(app)
    .get('/user')
    .end(function (res) {
             res.should.be.json;
             res.should.have.status(201);
             res.should.have.header('Content-Length', '15');
             res.body.should.deep.equal({name: 'tobi'});
         });

var user = request(app).agent();

user
    .get('/questions')
    .end(function (res) {
             res.should.be.html;
             res.should.have.status(200);
             res.should.have.header('Content-Length', '4');
             res.text.should.equal('test');
         });
Jason Madigan

+1 - this'd be really nice to have.

Vaughan Rouesnel
vjpr commented June 06, 2013

+1

agsha
agsha commented July 24, 2013

+1, unusable without it.

Michael Succi

+1

Alex Cornejo

[Original: +1, is there a recommended way of testing sessions without this functionality?]

After inspecting the code I realized this functionality is already built into supertest (just not very well documented).

Just do:

var supertest = require('supertest');
var app = require('../lib/your_app_location');

var request = supertest(app);
var agent = supertest.agent(app);

Works like a charm, this issue should be closed, and so should be #46

Garrett Johnson
Collaborator

Just need to add docs for this.

Garrett Johnson gjohnson closed this October 19, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.