Skip to content

Commit 636449d

Browse files
Skn0ttorinokai
andauthoredJan 26, 2024
fix: overriding headers in route handlers should work (#195)
* chore: add acceptance test * chore: remove only from test --------- Co-authored-by: Rob Stanford <me@robstanford.com>
1 parent b55ca06 commit 636449d

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { cookies } from 'next/headers'
2+
3+
export const GET = async () => {
4+
cookies().set('foo', 'foo1')
5+
cookies().set('bar', 'bar1')
6+
7+
// Key, value, options
8+
cookies().set('test1', 'value1', { secure: true })
9+
10+
// One object
11+
cookies().set({
12+
name: 'test2',
13+
value: 'value2',
14+
httpOnly: true,
15+
path: '/handler',
16+
})
17+
18+
// Cookies here will be merged with the ones above
19+
return new Response('Hello, world!', {
20+
headers: [
21+
['Content-Type', 'text/custom'],
22+
['Set-Cookie', 'bar=bar2'],
23+
['Set-Cookie', 'baz=baz2'],
24+
],
25+
})
26+
}

‎tests/integration/simple-app.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,17 @@ test<FixtureTestContext>('handlers receive correct site domain', async (ctx) =>
9494
const url = new URL(data.url)
9595
expect(url.hostname).toBe('example.netlify')
9696
})
97+
98+
// adapted from https://github.com/vercel/next.js/blob/bd605245aae4c8545bdd38a597b89ad78ca3d978/test/e2e/app-dir/actions/app-action.test.ts#L119-L127
99+
test<FixtureTestContext>('handlers can add cookies in route handlers with the correct overrides', async (ctx) => {
100+
await createFixture('simple-next-app', ctx)
101+
await runPlugin(ctx)
102+
const index = await invokeFunction(ctx, { url: '/api/headers' })
103+
expect(index.headers['content-type']).toEqual('text/custom')
104+
const setCookieHeader = index.headers['set-cookie']
105+
expect(setCookieHeader).toContain('bar=bar2; Path=/')
106+
expect(setCookieHeader).toContain('baz=baz2; Path=/')
107+
expect(setCookieHeader).toContain('foo=foo1; Path=/')
108+
expect(setCookieHeader).toContain('test1=value1; Path=/; Secure')
109+
expect(setCookieHeader).toContain('test2=value2; Path=/handler; HttpOnly')
110+
})

0 commit comments

Comments
 (0)
Please sign in to comment.