|
1 | 1 | import { expect, Response } from '@playwright/test'
|
2 |
| -import { nextVersionSatisfies } from '../utils/next-version-helpers.mjs' |
| 2 | +import { hasNodeMiddlewareSupport, nextVersionSatisfies } from '../utils/next-version-helpers.mjs' |
3 | 3 | import { test } from '../utils/playwright-helpers.js'
|
4 | 4 | import { getImageSize } from 'next/dist/server/image-optimizer.js'
|
| 5 | +import type { Fixture } from '../utils/create-e2e-fixture.js' |
5 | 6 |
|
6 | 7 | type ExtendedWindow = Window & {
|
7 | 8 | didReload?: boolean
|
8 | 9 | }
|
9 | 10 |
|
10 |
| -test('Runs edge middleware', async ({ page, middleware }) => { |
11 |
| - await page.goto(`${middleware.url}/test/redirect`) |
| 11 | +for (const { expectedRuntime, label, testWithSwitchableMiddlewareRuntime } of [ |
| 12 | + { |
| 13 | + expectedRuntime: 'edge-runtime', |
| 14 | + label: 'Edge runtime middleware', |
| 15 | + testWithSwitchableMiddlewareRuntime: test.extend<{}, { edgeOrNodeMiddleware: Fixture }>({ |
| 16 | + edgeOrNodeMiddleware: [ |
| 17 | + async ({ middleware }, use) => { |
| 18 | + await use(middleware) |
| 19 | + }, |
| 20 | + { |
| 21 | + scope: 'worker', |
| 22 | + }, |
| 23 | + ], |
| 24 | + }), |
| 25 | + }, |
| 26 | + hasNodeMiddlewareSupport() |
| 27 | + ? { |
| 28 | + expectedRuntime: 'node', |
| 29 | + label: 'Node.js runtime middleware', |
| 30 | + testWithSwitchableMiddlewareRuntime: test.extend<{}, { edgeOrNodeMiddleware: Fixture }>({ |
| 31 | + edgeOrNodeMiddleware: [ |
| 32 | + async ({ middlewareNode }, use) => { |
| 33 | + await use(middlewareNode) |
| 34 | + }, |
| 35 | + { |
| 36 | + scope: 'worker', |
| 37 | + }, |
| 38 | + ], |
| 39 | + }), |
| 40 | + } |
| 41 | + : undefined, |
| 42 | +].filter(function isDefined<T>(argument: T | undefined): argument is T { |
| 43 | + return typeof argument !== 'undefined' |
| 44 | +})) { |
| 45 | + const test = testWithSwitchableMiddlewareRuntime |
| 46 | + |
| 47 | + test.describe(label, () => { |
| 48 | + test('Runs middleware', async ({ page, edgeOrNodeMiddleware }) => { |
| 49 | + const res = await page.goto(`${edgeOrNodeMiddleware.url}/test/redirect`) |
| 50 | + |
| 51 | + await expect(page).toHaveTitle('Simple Next App') |
| 52 | + |
| 53 | + const h1 = page.locator('h1') |
| 54 | + await expect(h1).toHaveText('Other') |
| 55 | + }) |
12 | 56 |
|
13 |
| - await expect(page).toHaveTitle('Simple Next App') |
| 57 | + test('Does not run middleware at the origin', async ({ page, edgeOrNodeMiddleware }) => { |
| 58 | + const res = await page.goto(`${edgeOrNodeMiddleware.url}/test/next`) |
14 | 59 |
|
15 |
| - const h1 = page.locator('h1') |
16 |
| - await expect(h1).toHaveText('Other') |
17 |
| -}) |
18 |
| - |
19 |
| -test('Does not run edge middleware at the origin', async ({ page, middleware }) => { |
20 |
| - const res = await page.goto(`${middleware.url}/test/next`) |
| 60 | + expect(await res?.headerValue('x-deno')).toBeTruthy() |
| 61 | + expect(await res?.headerValue('x-node')).toBeNull() |
21 | 62 |
|
22 |
| - expect(await res?.headerValue('x-deno')).toBeTruthy() |
23 |
| - expect(await res?.headerValue('x-node')).toBeNull() |
| 63 | + await expect(page).toHaveTitle('Simple Next App') |
24 | 64 |
|
25 |
| - await expect(page).toHaveTitle('Simple Next App') |
| 65 | + const h1 = page.locator('h1') |
| 66 | + await expect(h1).toHaveText('Message from middleware: hello') |
26 | 67 |
|
27 |
| - const h1 = page.locator('h1') |
28 |
| - await expect(h1).toHaveText('Message from middleware: hello') |
29 |
| -}) |
| 68 | + expect(await res?.headerValue('x-runtime')).toEqual(expectedRuntime) |
| 69 | + }) |
| 70 | + }) |
| 71 | +} |
30 | 72 |
|
31 | 73 | test('does not run middleware again for rewrite target', async ({ page, middleware }) => {
|
32 | 74 | const direct = await page.goto(`${middleware.url}/test/rewrite-target`)
|
|
0 commit comments