Skip to content
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

Any method to directly get cookie from the agent? #614

Closed
hacker0limbo opened this issue Dec 28, 2019 · 4 comments
Closed

Any method to directly get cookie from the agent? #614

hacker0limbo opened this issue Dec 28, 2019 · 4 comments

Comments

@hacker0limbo
Copy link

Hello:

According to the doc, it seems like it only mentions that agent is able to store the cookie and persist its with the second request.

However what i wanna implement is: After login request, i store my jwt token in my cookie and send the Set-Cookie header to client, and then i wanna perform the second request and for this request i need to grab the token i stored in my agent cookie and set it in the Authentication header.

I look at the doc and there seems no such way like agent.getCookie(cookieName: string) to directly get the cookie, so is there anyway to do this? My English is not very well and i hope i explain my confusion clearly.

Cheers!

@hacker0limbo
Copy link
Author

I actually found a solution here. It manually store the cookie in a variable after login:

  const agent = request.agent(api);
  let cookie;

  beforeAll(() => agent
        .post('/login')
        .send({
          username: testerName,
          password: testerPassword,
        })
        .expect(200)
        .then((res) => {
          const cookies = res.headers['set-cookie'][0].split(',').map(item => item.split(';')[0]);
          cookie = cookies.join(';');
         }));

  describe('GET logout', () => {
    it('logs user out', () => agent
        .get('/logout')
        .set('Cookie', cookie)
        .expect(302));
  });

Not sure if this is the correct way to implement this, any ideas?

@jesperp
Copy link

jesperp commented Jan 6, 2020

You were almost correct to start with. The cookies are stored in agent.jar and you can access them through agent.jar.getCookie(cookie_name, access_info) (see https://github.com/bmeck/node-cookiejar )

So, you need to match not just on the cookie name but also access info. You can use the CookieAccessInfo function to create an access info object, with this you can match on e.g specific paths and domains.

const { CookieAccessInfo } = require('cookiejar')

...

await myagent
  .post('/api/auth/login')
  .send({ user: 'test', pass: 'test'})
  .set('Accept', 'application/json')
  .expect('set-cookie', /connect.sid=.*; Path=\/; HttpOnly/)
  .expect(200)

const access_info = CookieAccessInfo() // can provide path, etc also...
const cookies = myagent.jar.getCookie('connect.sid', access_info)

Not sure this is the intended way to do it but I think this is quite clean...

@hacker0limbo
Copy link
Author

Sweet, This is exactly what i am looking for. Thanks for your detailed explanation.

@benjaoholeguy
Copy link

benjaoholeguy commented Jun 16, 2022

The thing is that using a package for testing cannot manage cookies like browsers or postman.

This would work:

Sign in. After that use the get method to extract the generated cookie

const authResponse = await request(app)
.post('/api/xxx/signup')
.send({
email: "[email protected]",
password: "password"
}) .expect(201);

Get the cookie:
const cookie = authResponse.get('Set-Cookie');

Finally, use it with set method like so
const response = await request(app)
.get('/api/xxx/useit')
.set('Cookie', cookie)
.send()
.expect(200);

Hope it helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants