-
Notifications
You must be signed in to change notification settings - Fork 759
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
How to Persist a request (with cookies, ie sessions) Like super-agent #46
Comments
@FZX As discussed in #26, it seems Though not optimal at all, here's how I dealt with the case describe "Login", ->
# Login first
beforeEach ( done )->
@agent = superagent.agent()
request( app ).post( '/login' )
.send( { username: 'myname', password: 'secur1ty' } )
.end ( err, res ) =>
@agent.saveCookies( res ) # Store cookies to `@agent`
done()
# Now you can test :)
describe 'GET /admin/articles', ->
beforeEach ( done )->
req = request( app ).get( '/admin/articles' )
@agent.attachCookies( req ) # Attach cookies (session info) to the request
req.end ( err, res )=>
@res = res
done()
it 'should render Hello', ->
@res.should.have.status( 200 )
@res.text.should.include( 'Hello' )
|
@mnmly,thank you very much, your code is very helpful! |
@mnmly Best method I've seen so far to get the niceties of both supertest and superagent. Thx. |
@mnmly Login session without the agent https://gist.github.com/joaoneto/5152248 |
@joaoneto this is much better and cleaner 👍 |
Would be really great if this got resolved - such a common use case. |
Using @mnmly approach you can then monkey patch supertest and chain on the agent.
|
neither the agent nor cookies solution seems to work with express 3.2.6. |
Ah, the cookies solution works fine if I set the Cookie header instead of the cookies property. req.set( 'Cookie', cookies ) |
thanks @timmywil, it's weird, but really works! |
var supertest = require('supertest');
var app = express();
var agent = supertest.agent(app);
// then you can persist cookie |
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();
});
};
}); |
In my case, was smoothly replaced by
|
For the records, full example for @alsotang suggest: 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();
});
};
}); |
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?
The text was updated successfully, but these errors were encountered: