Skip to content

Commit 2452624

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

File tree

5 files changed

+56
-474
lines changed

5 files changed

+56
-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

+21
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

@@ -79,3 +81,22 @@ export async function middleware(req: NextRequest) {
7981
return response
8082
}
8183
}
84+
85+
export const config = {
86+
matcher: [
87+
'/api/:all*',
88+
'/headers',
89+
{ source: '/static' },
90+
{ source: '/shows/:all*' },
91+
{
92+
source: '/conditional',
93+
has: [
94+
{
95+
type: 'header',
96+
key: 'x-my-header',
97+
value: 'my-value',
98+
},
99+
],
100+
},
101+
],
102+
}

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)