Skip to content

Commit bb00a09

Browse files
emspishakmknichelijjk
authored
Fix a bug in the recently added unstable_getResponseFromNextConfig. (#72355)
This utility was added in #70731 but incorrectly said there was a match even if `has` and `missing` didn't match. This also updates the test utility to automatically set the `Host` header when constructing a request (if the URL has a host). <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # --> --------- Co-authored-by: Mark Knichel <[email protected]> Co-authored-by: JJ Kasper <[email protected]>
1 parent 47dea26 commit bb00a09

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

packages/next/src/experimental/testing/server/config-testing-utils.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,64 @@ describe('config-testing-utils', () => {
4949
)
5050
})
5151

52+
it("ignores redirect that doesn't match has", async () => {
53+
const response = await unstable_getResponseFromNextConfig({
54+
url: 'https://nextjs.org/test/foo',
55+
nextConfig: {
56+
async redirects() {
57+
return [
58+
{
59+
source: '/test/:slug',
60+
destination: '/test2/:slug',
61+
permanent: false,
62+
has: [
63+
{
64+
type: 'header',
65+
key: 'host',
66+
value: 'othersite.com',
67+
},
68+
],
69+
},
70+
]
71+
},
72+
},
73+
})
74+
expect(response.status).toEqual(200)
75+
})
76+
77+
it('redirects with has and missing', async () => {
78+
const response = await unstable_getResponseFromNextConfig({
79+
url: 'https://nextjs.org/test/foo',
80+
nextConfig: {
81+
async redirects() {
82+
return [
83+
{
84+
source: '/test/:slug',
85+
destination: '/test2/:slug',
86+
permanent: false,
87+
has: [
88+
{
89+
type: 'host',
90+
value: 'nextjs.org',
91+
},
92+
],
93+
missing: [
94+
{
95+
type: 'host',
96+
value: 'othersite.com',
97+
},
98+
],
99+
},
100+
]
101+
},
102+
},
103+
})
104+
expect(response.status).toEqual(307)
105+
expect(response.headers.get('location')).toEqual(
106+
'https://nextjs.org/test2/foo'
107+
)
108+
})
109+
52110
it('redirects take precedence over rewrites', async () => {
53111
const response = await unstable_getResponseFromNextConfig({
54112
url: 'https://nextjs.org/test/foo',

packages/next/src/experimental/testing/server/config-testing-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ function matchRoute(
4343
)
4444
}
4545
if (route.has || route.missing) {
46-
if (matchHas(request, parsedUrl.query, route.has, route.missing)) {
47-
return pathMatch.params
46+
if (!matchHas(request, parsedUrl.query, route.has, route.missing)) {
47+
return
4848
}
4949
}
5050
return pathMatch.params

packages/next/src/experimental/testing/server/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { MockedRequest } from '../../../server/lib/mock-request'
33
import { NodeNextRequest } from '../../../server/base-http/node'
44
import type { BaseNextRequest } from '../../../server/base-http'
55
import type { NextResponse } from '../../../server/web/exports'
6+
import { parseUrl } from '../../../lib/url'
67

78
export function constructRequest({
89
url,
@@ -16,6 +17,9 @@ export function constructRequest({
1617
if (!headers) {
1718
headers = {}
1819
}
20+
if (!headers.host) {
21+
headers.host = parseUrl(url)?.host
22+
}
1923
if (cookies) {
2024
headers = {
2125
...headers,

0 commit comments

Comments
 (0)