question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to Persist a request (with cookies, ie sessions) Like super-agent

See original GitHub issue

To access some urls in application, one must login first. I must go back to use super-agent to unit test these urls. Can supertest do thing like this?

server = require './testServer'
request = require 'superagent'

r = request.agent()
server.start 3002

fakeUser = 
  username:'tj',
  password:'foobar'

describe 'after login', ->
      beforeEach (done)  ->
        r
          .post("http://localhost:3002/login") 
          .send(fakeUser) 
          .end (err, res) ->
            assert res.statusCode is 200
            done()

      it 'can show restricted page', (done) ->
         r
          .get("http://localhost:3002/restricted")
          .end (err, res) ->
             assert.include res.text,'Wahoo!'
             assert res.statusCode is 200
             done()

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

20reactions
jinwypcommented, Oct 18, 2017

For the records, full example for @alsotang suggest: You don’t need do anything more with the agent.

var supertest = require('supertest');
var agent     = supertest.agent('localhost/api');

describe('Login', function () {
  it('should login superadmin', function(done) {
    agent
      .post('/login')
      .type('form')
      .send({ email: 'email' })
      .send({ password: 'password' })
      .expect(302)
      .expect('Location', '/')
      .expect('set-cookie', /connect.sid/)
      .end(function(err, res) {
        if (err) return done(err);
        // agent.saveCookies(res); don‘t need this line 
        return done();
      });
  };


it('get user info', function(done) {
// don't need do anything with cookies, agent will  attached cookies automatically based on login above
    agent
      .get('/userinfo')
      .expect(200)
      .end(function(err, res) {
        if (err) return done(err);
        return done();
      });
  };
});

17reactions
rafaelgoucommented, May 19, 2016

For the records, full example for @alsotang suggest:

(this is a login form, not an API request)

var supertest = require('supertest');
var app       = require('../path_to_my/app')
var agent     = supertest.agent(app);

describe('Login', function () {
  it('should login superadmin', function(done) {
    agent
      .post('/login')
      .type('form')
      .send({ email: 'email' })
      .send({ password: 'password' })
      .expect(302)
      .expect('Location', '/')
      .expect('set-cookie', /connect.sid/)
      .end(function(err, res) {
        if (err) return done(err);
        agent.saveCookies(res);
        return done();
      });
  };
});

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Persist a request (with cookies, ie sessions) Like super ...
To access some urls in application, one must login first. I must go back to use super-agent to unit test these urls. Can...
Read more >
Superagent share session / cookie info with actual browser
Superagent uses the XMLHttpRequest object in the browser to make http requests. By default, cross-origin requests do not send cookies.
Read more >
Maintaining Session State with Cookies | Microsoft Learn
If you want the cookie information to persist beyond the session, you should create a persistent cookie by specifying an expiration date.
Read more >
How do I save cookies from an HTTP response so I can send ...
I am trying to collect some data from a website but, in order to do this, I first have to send a post...
Read more >
XMLHttpRequest.withCredentials - Web APIs | MDN
The XMLHttpRequest.withCredentials property is a boolean value that indicates whether or not cross-site Access-Control requests should be ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found