mswjs / http-middleware

Spawn an HTTP server from your request handlers or apply them to an existing server using a middleware.

Home Page:https://npm.im/@mswjs/http-middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot set multiple cookies

ernestostifano opened this issue · comments

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:

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

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.