Skip to content

Commit dde04aa

Browse files
committed
chore: add e2e tests
1 parent 8a69634 commit dde04aa

File tree

5 files changed

+62
-474
lines changed

5 files changed

+62
-474
lines changed

cypress/integration/middleware/standard.spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,26 @@ describe('Standard middleware', () => {
2020
})
2121
})
2222
})
23+
24+
describe('Middleware matchers', () => {
25+
it('does not apply "has" matcher when headers are not sent', () => {
26+
cy.request('/conditional').then((response) => {
27+
expect(response.headers).not.to.have.property('x-is-deno', 'true')
28+
expect(response.headers).not.to.have.property('x-modified-edge', 'true')
29+
})
30+
})
31+
32+
it('matches when headers are sent', () => {
33+
cy.request({ url: '/conditional', headers: { 'x-my-header': 'my-value' } }).then((response) => {
34+
expect(response.headers).to.have.property('x-is-deno', 'true')
35+
expect(response.headers).to.have.property('x-modified-edge', 'true')
36+
})
37+
})
38+
39+
it('matches when headers are sent', () => {
40+
cy.request('/_next/data/build-id/static.json').then((response) => {
41+
expect(response.headers).to.have.property('x-is-deno', 'true')
42+
expect(response.headers).to.have.property('x-modified-edge', 'true')
43+
})
44+
})
45+
})

demos/custom-routes/middleware.ts

-30
This file was deleted.

demos/middleware/middleware.ts

+27
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export async function middleware(req: NextRequest) {
1818
res.setPageProp('message', message)
1919
res.setPageProp('showAd', true)
2020

21+
res.headers.set('x-modified-edge', 'true')
22+
res.headers.set('x-is-deno', 'Deno' in globalThis ? 'true' : 'false')
2123
return res
2224
}
2325

@@ -39,6 +41,12 @@ export async function middleware(req: NextRequest) {
3941
return response
4042
}
4143

44+
if (pathname.startsWith('/conditional')) {
45+
response.headers.set('x-modified-edge', 'true')
46+
response.headers.set('x-is-deno', 'Deno' in globalThis ? 'true' : 'false')
47+
return response
48+
}
49+
4250
if (pathname.startsWith('/shows')) {
4351
if (pathname.startsWith('/shows/rewrite-absolute')) {
4452
response = NextResponse.rewrite(new URL('/shows/100', req.url))
@@ -79,3 +87,22 @@ export async function middleware(req: NextRequest) {
7987
return response
8088
}
8189
}
90+
91+
export const config = {
92+
matcher: [
93+
'/api/:all*',
94+
'/headers',
95+
{ source: '/static' },
96+
{ source: '/shows/:all*' },
97+
{
98+
source: '/conditional',
99+
has: [
100+
{
101+
type: 'header',
102+
key: 'x-my-header',
103+
value: 'my-value',
104+
},
105+
],
106+
},
107+
],
108+
}

demos/middleware/pages/conditional.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as React from 'react'
2+
3+
const Page = () => {
4+
return (
5+
<div>
6+
<p>Will set middleware headers only if request header is set</p>
7+
</div>
8+
)
9+
}
10+
11+
export default Page

0 commit comments

Comments
 (0)