add a virtual cookie store
See original GitHub issueIs your feature request related to a problem? Please describe.
Let’s say there are two mocked routes which look like this:
rest.get('https://example.org/set-cookie', (_, res, ctx) => {
return res(ctx.cookie('key', 'value'));
}),
rest.get('https://example.org/require-cookie', (req, res, ctx) => {
if (req.cookies.key === 'value') {
return res(ctx.status(204));
}
return res(ctx.status(400));
}),
When invoking those routes with two chained request …
fetch('https://example.org/set-cookie').then(() => fetch('https://example.org/require-cookie'))
… the second request will get a 400 response since there is no cookie header send with the request.
Describe the solution you’d like
As far as I understand, this currently doesn’t work since the cookie is set by using document.cookie. This means that the cookie will always be associated with the domain of the current document which is not necessarily the domain the response came from. There seems to be no way to change this behavior.
I would therefore like to propose adding a virtual cookie store. It would keep track of the cookies set by calling ctx.cookie()
and it would automatically set the appropriate header to each mocked request to the same domain.
Describe alternatives you’ve considered
As mentioned above I think there is no alternative right now since setting cookies via JavaScript is restricted for good reasons.
Additional context
I’m happy to start a PR for this if we conclude that this is something useful.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:10 (6 by maintainers)
Top GitHub Comments
What about adding an option to the
start()
function?We could also warn users in case they turned of the virtual cookie store but attempt to store a cross-origin cookie via
ctx.cookie()
.Thanks @kettanaito for creating the package. I created a new PR which uses that package. #469