Unverified Commit b60bb5e7 authored by Michael Wenzel's avatar Michael Wenzel Committed by GitHub

[Testing] Update middleware/next-config tests to include search params (#76354)

parent 51984a96
...@@ -49,6 +49,27 @@ describe('config-testing-utils', () => { ...@@ -49,6 +49,27 @@ describe('config-testing-utils', () => {
) )
}) })
it('handles redirect with query params', async () => {
const response = await unstable_getResponseFromNextConfig({
url: 'https://nextjs.org/test/foo',
nextConfig: {
async redirects() {
return [
{
source: '/test/:slug',
destination: '/test2?slug=:slug',
permanent: true,
},
]
},
},
})
expect(response.status).toEqual(308)
expect(response.headers.get('location')).toEqual(
'https://nextjs.org/test2?slug=foo'
)
})
it("ignores redirect that doesn't match has", async () => { it("ignores redirect that doesn't match has", async () => {
const response = await unstable_getResponseFromNextConfig({ const response = await unstable_getResponseFromNextConfig({
url: 'https://nextjs.org/test/foo', url: 'https://nextjs.org/test/foo',
......
...@@ -18,6 +18,7 @@ import type { ...@@ -18,6 +18,7 @@ import type {
import type { BaseNextRequest } from '../../../server/base-http' import type { BaseNextRequest } from '../../../server/base-http'
import type { Params } from '../../../server/request/params' import type { Params } from '../../../server/request/params'
import { constructRequest } from './utils' import { constructRequest } from './utils'
import { parsedUrlQueryToParams } from '../../../server/route-modules/app-route/helpers/parsed-url-query-to-params'
/** /**
* Tries to match the current request against the provided route. If there is * Tries to match the current request against the provided route. If there is
...@@ -122,8 +123,11 @@ export async function unstable_getResponseFromNextConfig({ ...@@ -122,8 +123,11 @@ export async function unstable_getResponseFromNextConfig({
params, params,
query: parsedUrl.query, query: parsedUrl.query,
}) })
const searchParams = new URLSearchParams(
parsedUrlQueryToParams(parsedDestination.query) as Record<string, string>
)
return new URL( return new URL(
newUrl, searchParams.size > 0 ? `${newUrl}?${searchParams.toString()}` : newUrl,
parsedDestination.hostname parsedDestination.hostname
? `${parsedDestination.protocol}//${parsedDestination.hostname}` ? `${parsedDestination.protocol}//${parsedDestination.hostname}`
: parsedUrl.host : parsedUrl.host
......
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