pactum
pactum copied to clipboard
Cookie jar feature
Is your feature request related to a problem? Please describe. If you want to reuse the cookies from a previous request (or across multiple ones), you need to extract the cookies manually from the "set-cookie" from the first request (and from any request that might add a new cookie), and send it along with every subsequent request.
Describe the solution you'd like It would be nice to be able to do something like:
const cookieJar = require('pactum').cookieJar();
await pactum.spec().get('/...').withCookieJar(cookieJar).expect(...);
await pactum.spec().post('/...').withCookieJar(cookieJar).expect(...);
await pactum.spec().post('/...').withCookieJar(cookieJar).expect(...);
cookieJar
should also have some utility methods to getCookie/setCookie
.
Maybe it also could be done automatically with a new setting pactum.request.setUseCookieJar(true);
that will make all requests made in the same test to use the same cookie jar. Not sure if it's even possible, but it would be nice :) You could override it for a specific request sending a new cookie jar, for instance.
Thanks! I hope it's not too much asking 🙏
Should probably close this issue?
For user-facing APIs it's typical to keep authentication tokens hidden inside the application and issue an "opaque" cookie to represent the session to the user agent upon successful sign-in. In this case it's really nice to have a built-in way to store the cookie(s) and automatically send the right ones on subsequent requests, as a cookie-supporting HTTP user agent would do.
Would be willing to work on a PR for this if you can provide a bit of guidance on how to integrate.