Skip to content

Commit

Permalink
Update middleware matcher test for root (#37961)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk authored Jun 23, 2022
1 parent 7bb247c commit 57c5e2a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions test/e2e/middleware-matcher/app/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NextResponse } from 'next/server'

export const config = {
matcher: [
'/',
'/with-middleware/:path*',
'/another-middleware/:path*',
// the below is testing special characters don't break the build
Expand Down
23 changes: 19 additions & 4 deletions test/e2e/middleware-matcher/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ describe('Middleware can set the matcher in its config', () => {
})
afterAll(() => next.destroy())

it('does not add the header for root request', async () => {
it('does add the header for root request', async () => {
const response = await fetchViaHTTP(next.url, '/')
expect(response.headers.get('X-From-Middleware')).toBeNull()
expect(response.headers.get('X-From-Middleware')).toBe('true')
expect(await response.text()).toContain('root page')
})

Expand Down Expand Up @@ -65,7 +65,7 @@ describe('Middleware can set the matcher in its config', () => {
expect(response.headers.get('X-From-Middleware')).toBe('true')
})

it('does not add the header for root data request', async () => {
it('does add the header for root data request', async () => {
const response = await fetchViaHTTP(
next.url,
`/_next/data/${next.buildId}/index.json`,
Expand All @@ -77,7 +77,7 @@ describe('Middleware can set the matcher in its config', () => {
message: 'Hello, world.',
},
})
expect(response.headers.get('X-From-Middleware')).toBeNull()
expect(response.headers.get('X-From-Middleware')).toBe('true')
})

it('should load matches in client manifest correctly', async () => {
Expand Down Expand Up @@ -173,6 +173,21 @@ describe('using a single matcher', () => {
})
afterAll(() => next.destroy())

it('does not add the header for root request', async () => {
const response = await fetchViaHTTP(next.url, '/')
expect(response.headers.get('X-From-Middleware')).toBeFalsy()
})

it('does not add the header for root data request', async () => {
const response = await fetchViaHTTP(
next.url,
`/_next/data/${next.buildId}/index.json`,
undefined,
{ headers: { 'x-nextjs-data': '1' } }
)
expect(response.headers.get('X-From-Middleware')).toBeFalsy()
})

it('adds the header for a matched path', async () => {
const response = await fetchViaHTTP(next.url, '/middleware/works')
expect(await response.text()).toContain('Hello from /middleware/works')
Expand Down

0 comments on commit 57c5e2a

Please sign in to comment.