Skip to content

Commit 5c0c948

Browse files
committed
test: move middleware-trailing-slash to test variants
1 parent 99df488 commit 5c0c948

File tree

7 files changed

+138
-103
lines changed

7 files changed

+138
-103
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export { middleware } from './middleware-shared'
2+
3+
export const config = {
4+
runtime: 'nodejs',
5+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import type { NextRequest } from 'next/server'
2+
import { NextResponse } from 'next/server'
3+
4+
export function middleware(request: NextRequest) {
5+
const response = getResponse(request)
6+
7+
response.headers.append('Deno' in globalThis ? 'x-deno' : 'x-node', Date.now().toString())
8+
// report Next.js Middleware Runtime (not the execution runtime, but target runtime)
9+
response.headers.append('x-runtime', typeof EdgeRuntime !== 'undefined' ? EdgeRuntime : 'node')
10+
response.headers.set('x-hello-from-middleware-res', 'hello')
11+
12+
return response
13+
}
14+
15+
const getResponse = (request: NextRequest) => {
16+
const requestHeaders = new Headers(request.headers)
17+
18+
requestHeaders.set('x-hello-from-middleware-req', 'hello')
19+
20+
if (request.nextUrl.pathname === '/test/next/') {
21+
return NextResponse.next({
22+
request: {
23+
headers: requestHeaders,
24+
},
25+
})
26+
}
27+
28+
if (request.nextUrl.pathname === '/test/redirect/') {
29+
return NextResponse.redirect(new URL('/other', request.url))
30+
}
31+
32+
if (request.nextUrl.pathname === '/test/redirect-with-headers/') {
33+
return NextResponse.redirect(new URL('/other', request.url), {
34+
headers: { 'x-header-from-redirect': 'hello' },
35+
})
36+
}
37+
38+
if (request.nextUrl.pathname === '/test/rewrite-internal/') {
39+
return NextResponse.rewrite(new URL('/rewrite-target', request.url), {
40+
request: {
41+
headers: requestHeaders,
42+
},
43+
})
44+
}
45+
46+
if (request.nextUrl.pathname === '/test/rewrite-external/') {
47+
const requestURL = new URL(request.url)
48+
const externalURL = new URL(requestURL.searchParams.get('external-url') as string)
49+
50+
externalURL.searchParams.set('from', 'middleware')
51+
52+
return NextResponse.rewrite(externalURL, {
53+
request: {
54+
headers: requestHeaders,
55+
},
56+
})
57+
}
58+
59+
return NextResponse.json({ error: 'Error' }, { status: 500 })
60+
}
Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1 @@
1-
import type { NextRequest } from 'next/server'
2-
import { NextResponse } from 'next/server'
3-
4-
export function middleware(request: NextRequest) {
5-
const response = getResponse(request)
6-
7-
response.headers.append('Deno' in globalThis ? 'x-deno' : 'x-node', Date.now().toString())
8-
response.headers.set('x-hello-from-middleware-res', 'hello')
9-
10-
return response
11-
}
12-
13-
const getResponse = (request: NextRequest) => {
14-
const requestHeaders = new Headers(request.headers)
15-
16-
requestHeaders.set('x-hello-from-middleware-req', 'hello')
17-
18-
if (request.nextUrl.pathname === '/test/next/') {
19-
return NextResponse.next({
20-
request: {
21-
headers: requestHeaders,
22-
},
23-
})
24-
}
25-
26-
if (request.nextUrl.pathname === '/test/redirect/') {
27-
return NextResponse.redirect(new URL('/other', request.url))
28-
}
29-
30-
if (request.nextUrl.pathname === '/test/redirect-with-headers/') {
31-
return NextResponse.redirect(new URL('/other', request.url), {
32-
headers: { 'x-header-from-redirect': 'hello' },
33-
})
34-
}
35-
36-
if (request.nextUrl.pathname === '/test/rewrite-internal/') {
37-
return NextResponse.rewrite(new URL('/rewrite-target', request.url), {
38-
request: {
39-
headers: requestHeaders,
40-
},
41-
})
42-
}
43-
44-
if (request.nextUrl.pathname === '/test/rewrite-external/') {
45-
const requestURL = new URL(request.url)
46-
const externalURL = new URL(requestURL.searchParams.get('external-url') as string)
47-
48-
externalURL.searchParams.set('from', 'middleware')
49-
50-
return NextResponse.rewrite(externalURL, {
51-
request: {
52-
headers: requestHeaders,
53-
},
54-
})
55-
}
56-
57-
return NextResponse.json({ error: 'Error' }, { status: 500 })
58-
}
1+
export { middleware } from './middleware-shared'

tests/fixtures/middleware-trailing-slash/next.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
const nextConfig = {
33
trailingSlash: true,
44
output: 'standalone',
5+
distDir: process.env.NEXT_DIST_DIR ?? '.next',
56
eslint: {
67
ignoreDuringBuilds: true,
78
},
9+
experimental: {
10+
nodeMiddleware: true,
11+
},
812
outputFileTracingRoot: __dirname,
913
}
1014

tests/fixtures/middleware-trailing-slash/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"version": "0.1.0",
44
"private": true,
55
"scripts": {
6-
"postinstall": "next build",
6+
"postinstall": "npm run build",
77
"dev": "next dev",
8-
"build": "next build"
8+
"build": "node ../../utils/build-variants.mjs"
99
},
1010
"dependencies": {
1111
"next": "latest",
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"node-middleware": {
3+
"distDir": ".next-node-middleware",
4+
"files": {
5+
"middleware.ts": "middleware-node.ts"
6+
},
7+
"test": {
8+
"dependencies": {
9+
"next": [
10+
{
11+
"versionConstraint": ">=15.2.0",
12+
"canaryOnly": true
13+
},
14+
{
15+
"versionConstraint": ">=15.5.0"
16+
}
17+
]
18+
}
19+
}
20+
}
21+
}

tests/integration/edge-handler.test.ts

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -337,58 +337,60 @@ for (const {
337337
}
338338
})
339339
})
340-
})
341-
}
342340

343-
describe('should run middleware on data requests', () => {
344-
test<FixtureTestContext>('when `trailingSlash: false`', async (ctx) => {
345-
await createFixture('middleware', ctx)
346-
await runPlugin(ctx)
341+
describe('should run middleware on data requests', () => {
342+
test<FixtureTestContext>('when `trailingSlash: false`', async (ctx) => {
343+
await createFixture('middleware', ctx)
344+
await runPlugin(ctx, runPluginConstants)
347345

348-
const origin = new LocalServer()
349-
const response = await invokeEdgeFunction(ctx, {
350-
functions: [EDGE_MIDDLEWARE_FUNCTION_NAME],
351-
origin,
352-
redirect: 'manual',
353-
url: '/_next/data/dJvEyLV8MW7CBLFf0Ecbk/test/redirect-with-headers.json',
354-
})
346+
const origin = new LocalServer()
347+
const response = await invokeEdgeFunction(ctx, {
348+
functions: [edgeFunctionNameRoot],
349+
origin,
350+
redirect: 'manual',
351+
url: '/_next/data/dJvEyLV8MW7CBLFf0Ecbk/test/redirect-with-headers.json',
352+
})
355353

356-
ctx.cleanup?.push(() => origin.stop())
354+
ctx.cleanup?.push(() => origin.stop())
357355

358-
expect(response.status).toBe(307)
359-
expect(response.headers.get('location'), 'added a location header').toBeTypeOf('string')
360-
expect(
361-
new URL(response.headers.get('location') as string).pathname,
362-
'redirected to the correct path',
363-
).toEqual('/other')
364-
expect(response.headers.get('x-header-from-redirect'), 'hello').toBe('hello')
365-
expect(origin.calls).toBe(0)
366-
})
356+
expect(response.status).toBe(307)
357+
expect(response.headers.get('location'), 'added a location header').toBeTypeOf('string')
358+
expect(
359+
new URL(response.headers.get('location') as string).pathname,
360+
'redirected to the correct path',
361+
).toEqual('/other')
362+
expect(response.headers.get('x-header-from-redirect'), 'hello').toBe('hello')
363+
expect(response.headers.get('x-runtime')).toEqual(expectedRuntime)
364+
expect(origin.calls).toBe(0)
365+
})
367366

368-
test<FixtureTestContext>('when `trailingSlash: true`', async (ctx) => {
369-
await createFixture('middleware-trailing-slash', ctx)
370-
await runPlugin(ctx)
367+
test<FixtureTestContext>('when `trailingSlash: true`', async (ctx) => {
368+
await createFixture('middleware-trailing-slash', ctx)
369+
await runPlugin(ctx, runPluginConstants)
371370

372-
const origin = new LocalServer()
373-
const response = await invokeEdgeFunction(ctx, {
374-
functions: [EDGE_MIDDLEWARE_FUNCTION_NAME],
375-
origin,
376-
redirect: 'manual',
377-
url: '/_next/data/dJvEyLV8MW7CBLFf0Ecbk/test/redirect-with-headers.json',
378-
})
371+
const origin = new LocalServer()
372+
const response = await invokeEdgeFunction(ctx, {
373+
functions: [edgeFunctionNameRoot],
374+
origin,
375+
redirect: 'manual',
376+
url: '/_next/data/dJvEyLV8MW7CBLFf0Ecbk/test/redirect-with-headers.json',
377+
})
379378

380-
ctx.cleanup?.push(() => origin.stop())
379+
ctx.cleanup?.push(() => origin.stop())
381380

382-
expect(response.status).toBe(307)
383-
expect(response.headers.get('location'), 'added a location header').toBeTypeOf('string')
384-
expect(
385-
new URL(response.headers.get('location') as string).pathname,
386-
'redirected to the correct path',
387-
).toEqual('/other')
388-
expect(response.headers.get('x-header-from-redirect'), 'hello').toBe('hello')
389-
expect(origin.calls).toBe(0)
381+
expect(response.status).toBe(307)
382+
expect(response.headers.get('location'), 'added a location header').toBeTypeOf('string')
383+
expect(
384+
new URL(response.headers.get('location') as string).pathname,
385+
'redirected to the correct path',
386+
).toEqual('/other')
387+
expect(response.headers.get('x-header-from-redirect'), 'hello').toBe('hello')
388+
expect(response.headers.get('x-runtime')).toEqual(expectedRuntime)
389+
expect(origin.calls).toBe(0)
390+
})
391+
})
390392
})
391-
})
393+
}
392394

393395
describe('page router', () => {
394396
test<FixtureTestContext>('edge api routes should work with middleware', async (ctx) => {

0 commit comments

Comments
 (0)