Skip to content

Commit 38a2f3b

Browse files
committed
test: move middleware-i18n-skip-normalize to test variants
1 parent 55d1722 commit 38a2f3b

File tree

7 files changed

+93
-51
lines changed

7 files changed

+93
-51
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+
}

tests/fixtures/middleware-i18n-skip-normalize/middleware.js renamed to tests/fixtures/middleware-i18n-skip-normalize/middleware-shared.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1+
import type { NextRequest } from 'next/server'
12
import { NextResponse } from 'next/server'
23

3-
export async function middleware(request) {
4+
export async function middleware(request: NextRequest) {
5+
const response = getResponse(request)
6+
7+
if (response) {
8+
response.headers.append('Deno' in globalThis ? 'x-deno' : 'x-node', Date.now().toString())
9+
// report Next.js Middleware Runtime (not the execution runtime, but target runtime)
10+
// @ts-expect-error EdgeRuntime global not declared
11+
response.headers.append('x-runtime', typeof EdgeRuntime !== 'undefined' ? EdgeRuntime : 'node')
12+
response.headers.set('x-hello-from-middleware-res', 'hello')
13+
}
14+
}
15+
16+
const getResponse = (request: NextRequest) => {
417
const url = request.nextUrl
518

619
// this is needed for tests to get the BUILD_ID
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { middleware } from './middleware-shared'

tests/fixtures/middleware-i18n-skip-normalize/next.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
22
output: 'standalone',
3+
distDir: process.env.NEXT_DIST_DIR ?? '.next',
34
eslint: {
45
ignoreDuringBuilds: true,
56
},
@@ -11,6 +12,7 @@ module.exports = {
1112
experimental: {
1213
clientRouterFilter: true,
1314
clientRouterFilterRedirects: true,
15+
nodeMiddleware: true,
1416
},
1517
redirects() {
1618
return [

tests/fixtures/middleware-i18n-skip-normalize/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: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -632,59 +632,59 @@ for (const {
632632
expect(bodyFr.nextUrlLocale).toBe('fr')
633633
})
634634

635-
test.skipIf(expectedRuntime === 'node')<FixtureTestContext>(
636-
'should preserve locale in request.nextUrl with skipMiddlewareUrlNormalize',
637-
async (ctx) => {
638-
await createFixture('middleware-i18n-skip-normalize', ctx)
639-
await runPlugin(ctx)
640-
const origin = await LocalServer.run(async (req, res) => {
641-
res.write(
642-
JSON.stringify({
643-
url: req.url,
644-
headers: req.headers,
645-
}),
646-
)
647-
res.end()
648-
})
649-
ctx.cleanup?.push(() => origin.stop())
635+
test<FixtureTestContext>('should preserve locale in request.nextUrl with skipMiddlewareUrlNormalize', async (ctx) => {
636+
await createFixture('middleware-i18n-skip-normalize', ctx)
637+
await runPlugin(ctx, runPluginConstants)
638+
const origin = await LocalServer.run(async (req, res) => {
639+
res.write(
640+
JSON.stringify({
641+
url: req.url,
642+
headers: req.headers,
643+
}),
644+
)
645+
res.end()
646+
})
647+
ctx.cleanup?.push(() => origin.stop())
650648

651-
const response = await invokeEdgeFunction(ctx, {
652-
functions: [EDGE_MIDDLEWARE_FUNCTION_NAME],
653-
origin,
654-
url: `/json`,
655-
})
656-
expect(response.status).toBe(200)
657-
const body = await response.json()
649+
const response = await invokeEdgeFunction(ctx, {
650+
functions: [edgeFunctionNameRoot],
651+
origin,
652+
url: `/json`,
653+
})
654+
expect(response.status).toBe(200)
655+
expect(response.headers.get('x-runtime')).toEqual(expectedRuntime)
656+
const body = await response.json()
657+
658+
expect(body.requestUrlPathname).toBe('/json')
659+
expect(body.nextUrlPathname).toBe('/json')
660+
expect(body.nextUrlLocale).toBe('en')
658661

659-
expect(body.requestUrlPathname).toBe('/json')
660-
expect(body.nextUrlPathname).toBe('/json')
661-
expect(body.nextUrlLocale).toBe('en')
662+
const responseEn = await invokeEdgeFunction(ctx, {
663+
functions: [edgeFunctionNameRoot],
664+
origin,
665+
url: `/en/json`,
666+
})
667+
expect(responseEn.status).toBe(200)
668+
expect(responseEn.headers.get('x-runtime')).toEqual(expectedRuntime)
669+
const bodyEn = await responseEn.json()
662670

663-
const responseEn = await invokeEdgeFunction(ctx, {
664-
functions: [EDGE_MIDDLEWARE_FUNCTION_NAME],
665-
origin,
666-
url: `/en/json`,
667-
})
668-
expect(responseEn.status).toBe(200)
669-
const bodyEn = await responseEn.json()
671+
expect(bodyEn.requestUrlPathname).toBe('/en/json')
672+
expect(bodyEn.nextUrlPathname).toBe('/json')
673+
expect(bodyEn.nextUrlLocale).toBe('en')
670674

671-
expect(bodyEn.requestUrlPathname).toBe('/en/json')
672-
expect(bodyEn.nextUrlPathname).toBe('/json')
673-
expect(bodyEn.nextUrlLocale).toBe('en')
675+
const responseFr = await invokeEdgeFunction(ctx, {
676+
functions: [edgeFunctionNameRoot],
677+
origin,
678+
url: `/fr/json`,
679+
})
680+
expect(responseFr.status).toBe(200)
681+
expect(responseFr.headers.get('x-runtime')).toEqual(expectedRuntime)
682+
const bodyFr = await responseFr.json()
674683

675-
const responseFr = await invokeEdgeFunction(ctx, {
676-
functions: [EDGE_MIDDLEWARE_FUNCTION_NAME],
677-
origin,
678-
url: `/fr/json`,
679-
})
680-
expect(responseFr.status).toBe(200)
681-
const bodyFr = await responseFr.json()
682-
683-
expect(bodyFr.requestUrlPathname).toBe('/fr/json')
684-
expect(bodyFr.nextUrlPathname).toBe('/json')
685-
expect(bodyFr.nextUrlLocale).toBe('fr')
686-
},
687-
)
684+
expect(bodyFr.requestUrlPathname).toBe('/fr/json')
685+
expect(bodyFr.nextUrlPathname).toBe('/json')
686+
expect(bodyFr.nextUrlLocale).toBe('fr')
687+
})
688688
})
689689
})
690690
}

0 commit comments

Comments
 (0)