Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update middleware matcher test for root #37961

Merged
merged 1 commit into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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