mirror of
https://github.com/hanzoai/next.js.git
synced 2026-09-24 17:00:51 +00:00
### What? When cloning a `Request` with `NextRequest` the second argument needs to take precedence over the `Headers` set on the original request. ### Why? Follow-up of #53157 I checked the `Request` spec https://fetch.spec.whatwg.org/#request-class and following the order of execution, `init.headers` should indeed override the headers if the first param was `Request`. I verified this in the browser too: ```js const req1 = new Request("http://n", {headers: {"x-header": "foo"}}) const req2 = new Request(req1, {headers: {"x-header-2": "bar"}}) Object.fromEntries(req2.headers) // {x-header-2: "bar"} ``` So we should match `NextRequest` with this behavior. ### How? Pass the `init` to `super` when cloning the request. Closes NEXT-1521 Fixes #54094
44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
/**
|
|
* @jest-environment @edge-runtime/jest-environment
|
|
*/
|
|
|
|
import { expectTypeOf } from 'expect-type'
|
|
import { NextRequest } from 'next/src/server/web/spec-extension/request'
|
|
|
|
it('should have 1 required parameter for constructor', () => {
|
|
expect(NextRequest.length).toBe(1)
|
|
})
|
|
|
|
it('should allow the 2nd parameter to be undefined', () => {
|
|
const request = new NextRequest('https://vercel.com')
|
|
expectTypeOf(request).toMatchTypeOf<NextRequest>()
|
|
|
|
expect(
|
|
new NextRequest('https://vercel.com', { geo: { city: 'Mars' } })
|
|
).toHaveProperty('geo.city', 'Mars')
|
|
expect(new NextRequest('https://vercel.com')).toHaveProperty(
|
|
'nextUrl.pathname',
|
|
'/'
|
|
)
|
|
})
|
|
|
|
it('should clone Request with headers', () => {
|
|
const request = new Request('https://example.com', {
|
|
headers: { 'x-foo': 'bar' },
|
|
})
|
|
|
|
const nextRequest = new NextRequest(request)
|
|
|
|
expect(Object.fromEntries(nextRequest.headers)).toEqual(
|
|
Object.fromEntries(request.headers)
|
|
)
|
|
|
|
// Second argument should override headers
|
|
const headers = new Headers({ 'x-header': 'some header' })
|
|
const nextRequest2 = new NextRequest(request, { headers })
|
|
|
|
expect(Object.fromEntries(nextRequest2.headers)).toEqual(
|
|
Object.fromEntries(headers)
|
|
)
|
|
})
|