Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

if(!req.session.authentication){res.send(401);} 

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?

like image 642
Kuryaki Avatar asked Aug 12 '12 04:08

Kuryaki


People also ask

What is Super Agent app?

Super Agent helps you pick which cookies you want and which cookies you don't want. It doesn't store your data by default, informs you of any action taken, and warns you whenever it finds a website not respecting your preferences.

What is SuperAgent in node JS?

SuperAgent is light-weight progressive ajax API crafted for flexibility, readability, and a low learning curve after being frustrated with many of the existing request APIs. It also works with Node. js!


1 Answers

Use superagent.agent() (instead of plain old superagent) to make requests have persistent cookies. See 'Preserving cookies' in the superagent docs, or the code examples: agency.js, controller.test.js.

like image 142
Gaurav Avatar answered Sep 24 '22 12:09

Gaurav