How to set cookie header? #36
-
I'm trying to set a cookie header so that my handler can access I want the equivalent of this: const response = await supertest(app)
.get(PATH)
.set('Cookie', `magic-auth-token=${token}`)
.expect(200); I tried: requestPatcher: request =>
(request.headers = { 'set-cookie': [`magic-auth-token=${token}`] }), and requestPatcher: request =>
(request.headers = { cookie: `magic-auth-token=${token}` }), both of which do not work. How can I set a cookie? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
The following test is currently passing. What behavior are you experiencing/expecting (see the issue template for an example)? it('respects setting cookie using request patcher', async () => {
expect.hasAssertions();
const token = 'my-token';
await testApiHandler({
requestPatcher: (req) => (req.headers = { cookie: `magic-auth-token=${token}` }),
handler: async (req: NextApiRequest, res: NextApiResponse) => {
res.status(200).send({ data: req.headers.cookie });
},
test: async ({ fetch }) => {
expect((await fetch()).status).toBe(200);
expect(await (await fetch()).json()).toStrictEqual({
data: `magic-auth-token=${token}`
});
}
});
}); |
Beta Was this translation helpful? Give feedback.
-
Apparently the latter with test: async ({ fetch }) => {
expect((await fetch()).status).toBe(200);
expect(await (await fetch()).json()).toStrictEqual({
data: `magic-auth-token=${token}`
});
} And can't do something like: test: async ({ fetch }) => {
const response = await fetch();
const json = await response.json();
expect(response.status).toBe(200);
expect(json).toStrictEqual({ data: `magic-auth-token=${token}` });
} |
Beta Was this translation helpful? Give feedback.
-
Just per my curiosity, I threw this into a test and it also passed: it('respects setting cookie header using request patcher', async () => {
expect.hasAssertions();
const token = 'my-token';
await testApiHandler({
requestPatcher: (req) => (req.headers = { cookie: `magic-auth-token=${token}` }),
handler: async (req: NextApiRequest, res: NextApiResponse) => {
res.status(200).send({ data: req.headers.cookie });
},
test: async ({ fetch }) => {
const response = await fetch();
const json = await response.json();
expect(response.status).toBe(200);
expect(json).toStrictEqual({ data: `magic-auth-token=${token}` });
}
});
}); Most of the really weird issues I've encountered with fetch and Next are usually because I forgot to |
Beta Was this translation helpful? Give feedback.
The following test is currently passing. What behavior are you experiencing/expecting (see the issue template for an example)?