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

Cannot set multiple cookies #44

Closed
ernestostifano opened this issue Apr 24, 2024 · 2 comments · Fixed by #45
Closed

Cannot set multiple cookies #44

ernestostifano opened this issue Apr 24, 2024 · 2 comments · Fixed by #45

Comments

@ernestostifano
Copy link

ernestostifano commented Apr 24, 2024

Hello!

I've been trying many different ways to set multiple cookies on my responses with no luck.

Some of the tested approaches work on the browser (usingmsw directly), but none work while using the http-middleware.

For example, the following works perfectly using msw on the browser, but not using the http-middleware:

    return HttpResponse.json(
        body,
        {
            headers: [
                [
                    'Set-Cookie',
                    `${COOKIE_NAMES.ACCESS_TOKEN}=${session.accessToken.value}; Path=/; Max-Age=${TOKENS.ACCESS_TOKEN_MAX_AGE};`
                ],
                [
                    'Set-Cookie',
                    `${COOKIE_NAMES.REFRESH_TOKEN}=${session.refreshToken.value}; Path=/; Max-Age=${TOKENS.REFRESH_TOKEN_EXP_MAX_AGE};`
                ]
            ]
        }
    );

Using the http-middleware, only the last cookie is sent from express.

Other approaches I tried involved using:

  • Headers constructor (using append).
  • headers-polyfill.
  • Different overloads of HttpResponse.json.
  • Native Response.json.

I have the following configuration before the htttp-middleware:

app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', req.get('origin') || '*');

    res.header('Access-Control-Allow-Credentials', 'true');

    res.header('Access-Control-Allow-Methods', '*');

    res.header(
        'Access-Control-Allow-Headers',
        'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers'
    );

    if (req.method === 'OPTIONS') {
        return res.sendStatus(200);
    }

    return next();
});

app.use(express.json());

Potentially related issues:

@kettanaito
Copy link
Member

Hi, @ernestostifano! Thanks for letting me know. The fix is merged, the release will happen later tonight.

@kettanaito
Copy link
Member

Released: v0.10.1 🎉

This has been released in v0.10.1!

Make sure to always update to the latest version (npm i @mswjs/http-middleware@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

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

Successfully merging a pull request may close this issue.

2 participants