Skip to content

Commit 3a36f8d

Browse files
committed
fix tests
1 parent 1f65018 commit 3a36f8d

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

packages/next/server/web/utils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ export function validateURL(url: string | URL): string {
155155
try {
156156
return String(new URL(String(url)))
157157
} catch (error: any) {
158-
error.message = `URLs is malformed. Please use only absolute URLs - https://nextjs.org/docs/messages/middleware-relative-urls\n${error.message}`
159-
throw error
158+
throw new Error(
159+
`URLs is malformed. Please use only absolute URLs - https://nextjs.org/docs/messages/middleware-relative-urls`,
160+
// @ts-expect-error This will work for people who enable the error causes polyfill
161+
{ cause: error }
162+
)
160163
}
161164
}

test/integration/middleware/core/pages/rewrites/_middleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export async function middleware(request) {
1212
) {
1313
const isExternal = url.searchParams.get('override') === 'external'
1414
return NextResponse.rewrite(
15-
isExternal ? 'https://vercel.com' : '/rewrites/a'
15+
isExternal ? 'https://vercel.com' : new URL('/rewrites/a', request.url)
1616
)
1717
}
1818

test/integration/middleware/core/test/index.test.js

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const context = {}
1919
context.appDir = join(__dirname, '../')
2020

2121
const middlewareWarning = 'using beta Middleware (not covered by semver)'
22-
const urlsWarning = 'using relative URLs for Middleware will be deprecated soon'
22+
const urlsError = 'Please use only absolute URLs'
2323

2424
describe('Middleware base tests', () => {
2525
describe('dev mode', () => {
@@ -110,7 +110,7 @@ describe('Middleware base tests', () => {
110110
})
111111
})
112112

113-
function urlTests(log, locale = '') {
113+
function urlTests(_log, locale = '') {
114114
it('rewrites by default to a target location', async () => {
115115
const res = await fetchViaHTTP(context.appPort, `${locale}/urls`)
116116
const html = await res.text()
@@ -146,18 +146,39 @@ function urlTests(log, locale = '') {
146146
})
147147

148148
it('warns when using Response.redirect with a relative URL', async () => {
149-
await fetchViaHTTP(context.appPort, `${locale}/urls/relative-redirect`)
150-
expect(log.output).toContain(urlsWarning)
149+
const response = await fetchViaHTTP(
150+
context.appPort,
151+
`${locale}/urls/relative-redirect`
152+
)
153+
expect(await response.json()).toEqual({
154+
error: {
155+
message: expect.stringContaining(urlsError),
156+
},
157+
})
151158
})
152159

153160
it('warns when using NextResponse.redirect with a relative URL', async () => {
154-
await fetchViaHTTP(context.appPort, `${locale}/urls/relative-next-redirect`)
155-
expect(log.output).toContain(urlsWarning)
161+
const response = await fetchViaHTTP(
162+
context.appPort,
163+
`${locale}/urls/relative-next-redirect`
164+
)
165+
expect(await response.json()).toEqual({
166+
error: {
167+
message: expect.stringContaining(urlsError),
168+
},
169+
})
156170
})
157171

158-
it('warns when using NextResponse.rewrite with a relative URL', async () => {
159-
await fetchViaHTTP(context.appPort, `${locale}/urls/relative-next-rewrite`)
160-
expect(log.output).toContain(urlsWarning)
172+
it('throws when using NextResponse.rewrite with a relative URL', async () => {
173+
const response = await fetchViaHTTP(
174+
context.appPort,
175+
`${locale}/urls/relative-next-rewrite`
176+
)
177+
expect(await response.json()).toEqual({
178+
error: {
179+
message: expect.stringContaining(urlsError),
180+
},
181+
})
161182
})
162183
}
163184

0 commit comments

Comments
 (0)