Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access to superagent's agent? #26

Closed
truk opened this issue Sep 30, 2012 · 18 comments
Closed

Access to superagent's agent? #26

truk opened this issue Sep 30, 2012 · 18 comments

Comments

@truk
Copy link

truk commented Sep 30, 2012

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

@aseemk
Copy link

aseemk commented Oct 2, 2012

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!

@truk
Copy link
Author

truk commented Oct 2, 2012

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.

@aseemk
Copy link

aseemk commented Oct 2, 2012

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

@truk
Copy link
Author

truk commented Oct 2, 2012

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')

@trevorgerhardt
Copy link

+1

@twojcik
Copy link

twojcik commented Oct 18, 2012

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 :(

@aseemk
Copy link

aseemk commented Oct 18, 2012

Perhaps supertest could expose a similar agent API as superagent?

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

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

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

@twojcik
Copy link

twojcik commented Oct 30, 2012

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.

@twojcik
Copy link

twojcik commented Nov 4, 2012

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');
         });

@jasonmadigan
Copy link

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

@jaketrent
Copy link

+1

2 similar comments
@airandfingers
Copy link

+1

@vjpr
Copy link

vjpr commented Jun 6, 2013

+1

@agsha
Copy link

agsha commented Jul 24, 2013

+1, unusable without it.

@kb19
Copy link

kb19 commented Sep 8, 2013

+1

1 similar comment
@grydstedt
Copy link

+1

@acornejo
Copy link

[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

@gjohnson
Copy link
Contributor

Just need to add docs for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.