Unverified Commit 761c2930 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Add test case for #49235 (#49488)

This PR adds a test case for `headers()` and `cookies().set()` in Client
Component imported actions.
parent 4171cf38
...@@ -59,6 +59,32 @@ createNextDescribe( ...@@ -59,6 +59,32 @@ createNextDescribe(
}, 'same') }, 'same')
}) })
it('should support headers in client imported actions', async () => {
const logs: string[] = []
next.on('stdout', (log) => {
logs.push(log)
})
next.on('stderr', (log) => {
logs.push(log)
})
const currentTimestamp = Date.now()
const browser = await next.browser('/client')
await browser.elementByCss('#get-header').click()
await check(() => {
return logs.some((log) =>
log.includes('accept header: text/x-component')
)
? 'yes'
: ''
}, 'yes')
expect(
await browser.eval('+document.cookie.match(/test-cookie=(\\d+)/)[1]')
).toBeGreaterThanOrEqual(currentTimestamp)
})
it('should support setting cookies in route handlers with the correct overrides', async () => { it('should support setting cookies in route handlers with the correct overrides', async () => {
const res = await next.fetch('/handler') const res = await next.fetch('/handler')
const setCookieHeader = res.headers.get('set-cookie') as string[] const setCookieHeader = res.headers.get('set-cookie') as string[]
......
'use server' 'use server'
import { redirect } from 'next/navigation' import { redirect } from 'next/navigation'
import { headers, cookies } from 'next/headers'
export async function getHeaders() {
console.log('accept header:', headers().get('accept'))
cookies().set('test-cookie', Date.now())
}
export async function inc(value) { export async function inc(value) {
return value + 1 return value + 1
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import { useState } from 'react' import { useState } from 'react'
import double, { inc, dec, redirectAction } from './actions' import double, { inc, dec, redirectAction, getHeaders } from './actions'
export default function Counter() { export default function Counter() {
const [count, setCount] = useState(0) const [count, setCount] = useState(0)
...@@ -52,6 +52,11 @@ export default function Counter() { ...@@ -52,6 +52,11 @@ export default function Counter() {
redirect external redirect external
</button> </button>
</form> </form>
<form action={getHeaders}>
<button type="submit" id="get-header">
submit
</button>
</form>
</div> </div>
) )
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment