Unverified Commit 57c5e2a0 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update middleware matcher test for root (#37961)

parent 7bb247ca
...@@ -2,6 +2,7 @@ import { NextResponse } from 'next/server' ...@@ -2,6 +2,7 @@ import { NextResponse } from 'next/server'
export const config = { export const config = {
matcher: [ matcher: [
'/',
'/with-middleware/:path*', '/with-middleware/:path*',
'/another-middleware/:path*', '/another-middleware/:path*',
// the below is testing special characters don't break the build // the below is testing special characters don't break the build
......
...@@ -15,9 +15,9 @@ describe('Middleware can set the matcher in its config', () => { ...@@ -15,9 +15,9 @@ describe('Middleware can set the matcher in its config', () => {
}) })
afterAll(() => next.destroy()) 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, '/') 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') expect(await response.text()).toContain('root page')
}) })
...@@ -65,7 +65,7 @@ describe('Middleware can set the matcher in its config', () => { ...@@ -65,7 +65,7 @@ describe('Middleware can set the matcher in its config', () => {
expect(response.headers.get('X-From-Middleware')).toBe('true') 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( const response = await fetchViaHTTP(
next.url, next.url,
`/_next/data/${next.buildId}/index.json`, `/_next/data/${next.buildId}/index.json`,
...@@ -77,7 +77,7 @@ describe('Middleware can set the matcher in its config', () => { ...@@ -77,7 +77,7 @@ describe('Middleware can set the matcher in its config', () => {
message: 'Hello, world.', 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 () => { it('should load matches in client manifest correctly', async () => {
...@@ -173,6 +173,21 @@ describe('using a single matcher', () => { ...@@ -173,6 +173,21 @@ describe('using a single matcher', () => {
}) })
afterAll(() => next.destroy()) 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 () => { it('adds the header for a matched path', async () => {
const response = await fetchViaHTTP(next.url, '/middleware/works') const response = await fetchViaHTTP(next.url, '/middleware/works')
expect(await response.text()).toContain('Hello from /middleware/works') expect(await response.text()).toContain('Hello from /middleware/works')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment