Skip to content

Commit f0fb695

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

File tree

5 files changed

+63
-474
lines changed

5 files changed

+63
-474
lines changed

cypress/integration/middleware/standard.spec.ts

Lines changed: 23 additions & 0 deletions
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

Lines changed: 0 additions & 30 deletions
This file was deleted.

demos/middleware/middleware.ts

Lines changed: 28 additions & 0 deletions
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,13 @@ export async function middleware(req: NextRequest) {
3941
return response
4042
}
4143

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

demos/middleware/pages/conditional.js

Lines changed: 11 additions & 0 deletions
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)