Code Monkey home page Code Monkey logo

Comments (18)

acornejo avatar acornejo commented on May 23, 2024 2

[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

from supertest.

aseemk avatar aseemk commented on May 23, 2024

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!

from supertest.

truk avatar truk commented on May 23, 2024

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.

from supertest.

aseemk avatar aseemk commented on May 23, 2024

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

from supertest.

truk avatar truk commented on May 23, 2024

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

from supertest.

trevorgerhardt avatar trevorgerhardt commented on May 23, 2024

+1

from supertest.

twojcik avatar twojcik commented on May 23, 2024

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

from supertest.

aseemk avatar aseemk commented on May 23, 2024

Perhaps supertest could expose a similar agent API as superagent?

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

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

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

from supertest.

twojcik avatar twojcik commented on May 23, 2024

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.

from supertest.

twojcik avatar twojcik commented on May 23, 2024

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

from supertest.

jasonmadigan avatar jasonmadigan commented on May 23, 2024

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

from supertest.

jaketrent avatar jaketrent commented on May 23, 2024

+1

from supertest.

airandfingers avatar airandfingers commented on May 23, 2024

+1

from supertest.

vjpr avatar vjpr commented on May 23, 2024

+1

from supertest.

agsha avatar agsha commented on May 23, 2024

+1, unusable without it.

from supertest.

kb19 avatar kb19 commented on May 23, 2024

+1

from supertest.

grydstedt avatar grydstedt commented on May 23, 2024

+1

from supertest.

gjohnson avatar gjohnson commented on May 23, 2024

Just need to add docs for this.

from supertest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.