Skip to content

Commit 99df488

Browse files
committed
test: move middleware-conditions to test variants
1 parent 3e7238c commit 99df488

File tree

7 files changed

+152
-106
lines changed

7 files changed

+152
-106
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export { middleware } from './middleware-shared'
2+
3+
export const config = {
4+
runtime: 'nodejs',
5+
matcher: [
6+
{
7+
source: '/foo',
8+
missing: [{ type: 'header', key: 'x-custom-header', value: 'custom-value' }],
9+
},
10+
{
11+
source: '/hello',
12+
},
13+
{
14+
source: '/nl/about',
15+
locale: false,
16+
},
17+
],
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { NextRequest } from 'next/server'
2+
import { NextResponse } from 'next/server'
3+
4+
export function middleware(request: NextRequest) {
5+
const response: NextResponse = NextResponse.next()
6+
7+
response.headers.set('x-hello-from-middleware-res', 'hello')
8+
9+
return response
10+
}

tests/fixtures/middleware-conditions/middleware.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
import type { NextRequest } from 'next/server'
2-
import { NextResponse } from 'next/server'
3-
4-
export function middleware(request: NextRequest) {
5-
const response: NextResponse = NextResponse.next()
6-
7-
response.headers.set('x-hello-from-middleware-res', 'hello')
8-
9-
return response
10-
}
1+
export { middleware } from './middleware-shared'
112

123
export const config = {
134
matcher: [

tests/fixtures/middleware-conditions/next.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
/** @type {import('next').NextConfig} */
22
const nextConfig = {
33
output: 'standalone',
4+
distDir: process.env.NEXT_DIST_DIR ?? '.next',
45
i18n: {
56
locales: ['en', 'fr', 'nl', 'es'],
67
defaultLocale: 'en',
78
},
89
eslint: {
910
ignoreDuringBuilds: true,
1011
},
12+
experimental: {
13+
nodeMiddleware: true,
14+
},
1115
outputFileTracingRoot: __dirname,
1216
}
1317

tests/fixtures/middleware-conditions/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: 96 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -223,120 +223,122 @@ for (const {
223223
expect(response.headers.get('x-runtime')).toEqual(expectedRuntime)
224224
})
225225
})
226-
})
227-
}
228-
229-
describe("aborts middleware execution when the matcher conditions don't match the request", () => {
230-
test<FixtureTestContext>('when the path is excluded', async (ctx) => {
231-
await createFixture('middleware', ctx)
232-
await runPlugin(ctx)
233226

234-
const origin = await LocalServer.run(async (req, res) => {
235-
expect(req.url).toBe('/_next/data')
236-
expect(req.headers['x-hello-from-middleware-req']).toBeUndefined()
237-
238-
res.write('Hello from origin!')
239-
res.end()
240-
})
227+
describe("aborts middleware execution when the matcher conditions don't match the request", () => {
228+
test<FixtureTestContext>('when the path is excluded', async (ctx) => {
229+
await createFixture('middleware', ctx)
230+
await runPlugin(ctx, runPluginConstants)
241231

242-
ctx.cleanup?.push(() => origin.stop())
232+
const origin = await LocalServer.run(async (req, res) => {
233+
expect(req.url).toBe('/_next/data')
234+
expect(req.headers['x-hello-from-middleware-req']).toBeUndefined()
243235

244-
const response = await invokeEdgeFunction(ctx, {
245-
functions: [EDGE_MIDDLEWARE_FUNCTION_NAME],
246-
origin,
247-
url: '/_next/data',
248-
})
236+
res.write('Hello from origin!')
237+
res.end()
238+
})
249239

250-
expect(await response.text()).toBe('Hello from origin!')
251-
expect(response.status).toBe(200)
252-
expect(response.headers.has('x-hello-from-middleware-res')).toBeFalsy()
253-
expect(origin.calls).toBe(1)
254-
})
240+
ctx.cleanup?.push(() => origin.stop())
255241

256-
test<FixtureTestContext>('when a request header matches a condition', async (ctx) => {
257-
await createFixture('middleware-conditions', ctx)
258-
await runPlugin(ctx)
242+
const response = await invokeEdgeFunction(ctx, {
243+
functions: [edgeFunctionNameRoot],
244+
origin,
245+
url: '/_next/data',
246+
})
259247

260-
const origin = await LocalServer.run(async (req, res) => {
261-
expect(req.url).toBe('/foo')
262-
expect(req.headers['x-hello-from-middleware-req']).toBeUndefined()
248+
expect(await response.text()).toBe('Hello from origin!')
249+
expect(response.status).toBe(200)
250+
expect(response.headers.has('x-hello-from-middleware-res')).toBeFalsy()
251+
expect(origin.calls).toBe(1)
252+
})
263253

264-
res.write('Hello from origin!')
265-
res.end()
266-
})
254+
test<FixtureTestContext>('when a request header matches a condition', async (ctx) => {
255+
await createFixture('middleware-conditions', ctx)
256+
await runPlugin(ctx, runPluginConstants)
267257

268-
ctx.cleanup?.push(() => origin.stop())
258+
const origin = await LocalServer.run(async (req, res) => {
259+
expect(req.url).toBe('/foo')
260+
expect(req.headers['x-hello-from-middleware-req']).toBeUndefined()
269261

270-
// Request 1: Middleware should run because we're not sending the header.
271-
const response1 = await invokeEdgeFunction(ctx, {
272-
functions: [EDGE_MIDDLEWARE_FUNCTION_NAME],
273-
origin,
274-
url: '/foo',
275-
})
262+
res.write('Hello from origin!')
263+
res.end()
264+
})
276265

277-
expect(await response1.text()).toBe('Hello from origin!')
278-
expect(response1.status).toBe(200)
279-
expect(response1.headers.has('x-hello-from-middleware-res')).toBeTruthy()
280-
expect(origin.calls).toBe(1)
266+
ctx.cleanup?.push(() => origin.stop())
281267

282-
// Request 2: Middleware should not run because we're sending the header.
283-
const response2 = await invokeEdgeFunction(ctx, {
284-
headers: {
285-
'x-custom-header': 'custom-value',
286-
},
287-
functions: [EDGE_MIDDLEWARE_FUNCTION_NAME],
288-
origin,
289-
url: '/foo',
290-
})
268+
// Request 1: Middleware should run because we're not sending the header.
269+
const response1 = await invokeEdgeFunction(ctx, {
270+
functions: [edgeFunctionNameRoot],
271+
origin,
272+
url: '/foo',
273+
})
291274

292-
expect(await response2.text()).toBe('Hello from origin!')
293-
expect(response2.status).toBe(200)
294-
expect(response2.headers.has('x-hello-from-middleware-res')).toBeFalsy()
295-
expect(origin.calls).toBe(2)
296-
})
275+
expect(await response1.text()).toBe('Hello from origin!')
276+
expect(response1.status).toBe(200)
277+
expect(response1.headers.has('x-hello-from-middleware-res')).toBeTruthy()
278+
expect(response1.headers.get('x-runtime')).toEqual(expectedRuntime)
279+
expect(origin.calls).toBe(1)
280+
281+
// Request 2: Middleware should not run because we're sending the header.
282+
const response2 = await invokeEdgeFunction(ctx, {
283+
headers: {
284+
'x-custom-header': 'custom-value',
285+
},
286+
functions: [edgeFunctionNameRoot],
287+
origin,
288+
url: '/foo',
289+
})
297290

298-
test<FixtureTestContext>('should handle locale matching correctly', async (ctx) => {
299-
await createFixture('middleware-conditions', ctx)
300-
await runPlugin(ctx)
291+
expect(await response2.text()).toBe('Hello from origin!')
292+
expect(response2.status).toBe(200)
293+
expect(response2.headers.has('x-hello-from-middleware-res')).toBeFalsy()
294+
expect(origin.calls).toBe(2)
295+
})
301296

302-
const origin = await LocalServer.run(async (req, res) => {
303-
expect(req.headers['x-hello-from-middleware-req']).toBeUndefined()
297+
test<FixtureTestContext>('should handle locale matching correctly', async (ctx) => {
298+
await createFixture('middleware-conditions', ctx)
299+
await runPlugin(ctx, runPluginConstants)
304300

305-
res.write('Hello from origin!')
306-
res.end()
307-
})
301+
const origin = await LocalServer.run(async (req, res) => {
302+
expect(req.headers['x-hello-from-middleware-req']).toBeUndefined()
308303

309-
ctx.cleanup?.push(() => origin.stop())
304+
res.write('Hello from origin!')
305+
res.end()
306+
})
310307

311-
for (const path of ['/hello', '/en/hello', '/nl/hello', '/nl/about']) {
312-
const response = await invokeEdgeFunction(ctx, {
313-
functions: [EDGE_MIDDLEWARE_FUNCTION_NAME],
314-
origin,
315-
url: path,
316-
})
317-
expect(
318-
response.headers.has('x-hello-from-middleware-res'),
319-
`should match ${path}`,
320-
).toBeTruthy()
321-
expect(await response.text()).toBe('Hello from origin!')
322-
expect(response.status).toBe(200)
323-
}
308+
ctx.cleanup?.push(() => origin.stop())
324309

325-
for (const path of ['/invalid/hello', '/hello/invalid', '/about', '/en/about']) {
326-
const response = await invokeEdgeFunction(ctx, {
327-
functions: [EDGE_MIDDLEWARE_FUNCTION_NAME],
328-
origin,
329-
url: path,
310+
for (const path of ['/hello', '/en/hello', '/nl/hello', '/nl/about']) {
311+
const response = await invokeEdgeFunction(ctx, {
312+
functions: [edgeFunctionNameRoot],
313+
origin,
314+
url: path,
315+
})
316+
expect(
317+
response.headers.has('x-hello-from-middleware-res'),
318+
`should match ${path}`,
319+
).toBeTruthy()
320+
expect(response.headers.get('x-runtime')).toEqual(expectedRuntime)
321+
expect(await response.text()).toBe('Hello from origin!')
322+
expect(response.status).toBe(200)
323+
}
324+
325+
for (const path of ['/invalid/hello', '/hello/invalid', '/about', '/en/about']) {
326+
const response = await invokeEdgeFunction(ctx, {
327+
functions: [edgeFunctionNameRoot],
328+
origin,
329+
url: path,
330+
})
331+
expect(
332+
response.headers.has('x-hello-from-middleware-res'),
333+
`should not match ${path}`,
334+
).toBeFalsy()
335+
expect(await response.text()).toBe('Hello from origin!')
336+
expect(response.status).toBe(200)
337+
}
330338
})
331-
expect(
332-
response.headers.has('x-hello-from-middleware-res'),
333-
`should not match ${path}`,
334-
).toBeFalsy()
335-
expect(await response.text()).toBe('Hello from origin!')
336-
expect(response.status).toBe(200)
337-
}
339+
})
338340
})
339-
})
341+
}
340342

341343
describe('should run middleware on data requests', () => {
342344
test<FixtureTestContext>('when `trailingSlash: false`', async (ctx) => {

0 commit comments

Comments
 (0)