Kuryaki Kuryaki - 1 year ago 426
Node.js Question

How can you use cookies with superagent?

I'm doing cookie session management with express with something like this:

req.session.authentication = auth;

And I verify the authenticated urls with something like


Now I'm building tests for the URLs with mocha, superagent and should, however I can't seem to find a way to get/set the cookie with superagent. I even tried to request the login before the authenticated test but it is not working,

I have tried adding the request to the login in the before statement for the mocha BDD suite, however it is still telling me that the request is unauthorized, I have tested the authentication doing the requests from the browser, however it is not working from the suite any ideas why?

Answer Source

I've had this exact same issue (using mocha + should + superagent) and using superagent.agent() (instead of plain old superagent) to make requests does the trick.

For more info, check out superagent's Persisting an agent (with cookies, ie sessions) docs or the code examples: agency.js, controller.test.js.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download