|
1 | 1 | import { v4 } from 'uuid'
|
2 |
| -import { beforeEach, expect, test, vi } from 'vitest' |
| 2 | +import { beforeEach, describe, expect, test, vi } from 'vitest' |
3 | 3 | import {
|
4 | 4 | createFixture,
|
5 | 5 | invokeEdgeFunction,
|
@@ -94,3 +94,73 @@ test<FixtureTestContext>('should rewrite to an external URL', async (ctx) => {
|
94 | 94 | expect(external.calls).toBe(1)
|
95 | 95 | expect(origin.calls).toBe(0)
|
96 | 96 | })
|
| 97 | + |
| 98 | +describe("aborts middleware execution when the matcher conditions don't match the request", () => { |
| 99 | + test<FixtureTestContext>('when the path is excluded', async (ctx) => { |
| 100 | + await createFixture('middleware', ctx) |
| 101 | + await runPlugin(ctx) |
| 102 | + |
| 103 | + const origin = await LocalServer.run(async (req, res) => { |
| 104 | + expect(req.url).toBe('/_next/data') |
| 105 | + expect(req.headers['x-hello-from-middleware-req']).toBeUndefined() |
| 106 | + |
| 107 | + res.write('Hello from origin!') |
| 108 | + res.end() |
| 109 | + }) |
| 110 | + |
| 111 | + ctx.cleanup?.push(() => origin.stop()) |
| 112 | + |
| 113 | + const response1 = await invokeEdgeFunction(ctx, { |
| 114 | + functions: ['___netlify-edge-handler-middleware'], |
| 115 | + origin, |
| 116 | + url: '/_next/data', |
| 117 | + }) |
| 118 | + |
| 119 | + expect(await response1.text()).toBe('Hello from origin!') |
| 120 | + expect(response1.status).toBe(200) |
| 121 | + expect(response1.headers.has('x-hello-from-middleware-res')).toBeFalsy() |
| 122 | + expect(origin.calls).toBe(1) |
| 123 | + }) |
| 124 | + |
| 125 | + test<FixtureTestContext>('when a request header matches a condition', async (ctx) => { |
| 126 | + await createFixture('middleware-conditions', ctx) |
| 127 | + await runPlugin(ctx) |
| 128 | + |
| 129 | + const origin = await LocalServer.run(async (req, res) => { |
| 130 | + expect(req.url).toBe('/foo') |
| 131 | + expect(req.headers['x-hello-from-middleware-req']).toBeUndefined() |
| 132 | + |
| 133 | + res.write('Hello from origin!') |
| 134 | + res.end() |
| 135 | + }) |
| 136 | + |
| 137 | + ctx.cleanup?.push(() => origin.stop()) |
| 138 | + |
| 139 | + // Request 1: Middleware should run because we're not sending the header. |
| 140 | + const response1 = await invokeEdgeFunction(ctx, { |
| 141 | + functions: ['___netlify-edge-handler-middleware'], |
| 142 | + origin, |
| 143 | + url: '/foo', |
| 144 | + }) |
| 145 | + |
| 146 | + expect(await response1.text()).toBe('Hello from origin!') |
| 147 | + expect(response1.status).toBe(200) |
| 148 | + expect(response1.headers.has('x-hello-from-middleware-res')).toBeTruthy() |
| 149 | + expect(origin.calls).toBe(1) |
| 150 | + |
| 151 | + // Request 1: Middleware should not run because we're sending the header. |
| 152 | + const response2 = await invokeEdgeFunction(ctx, { |
| 153 | + headers: { |
| 154 | + 'x-custom-header': 'custom-value', |
| 155 | + }, |
| 156 | + functions: ['___netlify-edge-handler-middleware'], |
| 157 | + origin, |
| 158 | + url: '/foo', |
| 159 | + }) |
| 160 | + |
| 161 | + expect(await response2.text()).toBe('Hello from origin!') |
| 162 | + expect(response2.status).toBe(200) |
| 163 | + expect(response2.headers.has('x-hello-from-middleware-res')).toBeFalsy() |
| 164 | + expect(origin.calls).toBe(2) |
| 165 | + }) |
| 166 | +}) |
0 commit comments