Unverified Commit c0e8e55a authored by Florian Liebig's avatar Florian Liebig Committed by GitHub

Add test for external middleware rewrite with changed headers (#49606)

### What?

Within Issue https://github.com/vercel/next.js/issues/48040 / PR #49487
the middleware proxy for external rewrites was fixed. Unfortunately,
this does not work when you change the headers in your middleware
(according to
https://vercel.com/templates/next.js/edge-functions-modify-request-header)

### Why?

We still have issues with the latest canary

### How?

Unfortunately I was not able to find the issue, just recreating the bug
in a testcase

---------
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
parent 0dd4ba3f
......@@ -23,6 +23,44 @@ export async function middleware(request) {
})
}
if (
url.pathname.includes(
'/middleware-external-rewrite-body-headers-return-body'
)
) {
const tmpHeaders = new Headers(request.headers)
tmpHeaders.set('x-hello-from-middleware1', 'hello')
return NextResponse.rewrite(
'https://next-data-api-endpoint.vercel.app/api/echo-body',
{
request: {
headers: tmpHeaders,
},
}
)
}
if (
url.pathname.includes(
'/middleware-external-rewrite-body-headers-return-headers'
)
) {
const tmpHeaders = new Headers(request.headers)
tmpHeaders.set('x-hello-from-middleware1', 'hello')
return NextResponse.rewrite(
'https://next-data-api-endpoint.vercel.app/api/echo-headers',
{
request: {
headers: tmpHeaders,
},
}
)
}
if (url.pathname.includes('/middleware-external-rewrite-body')) {
return NextResponse.rewrite(
'https://next-data-api-endpoint.vercel.app/api/echo-body'
......
......@@ -72,6 +72,34 @@ describe('Middleware Rewrite', () => {
expect(await res.text()).toEqual(body)
})
it('should handle middleware rewrite with body and headers correctly', async () => {
const body = JSON.stringify({ hello: 'world' })
const res = await next.fetch(
'/middleware-external-rewrite-body-headers-return-body',
{
redirect: 'manual',
method: 'POST',
body,
}
)
expect(res.status).toBe(200)
expect(await res.text()).toEqual(body)
const resWithHeaders = await next.fetch(
'/middleware-external-rewrite-body-headers-return-headers',
{
redirect: 'manual',
method: 'POST',
body,
}
)
expect(resWithHeaders.status).toBe(200)
const json = await resWithHeaders.json()
expect(json.headers['x-hello-from-middleware1']).toBe('hello')
})
it('should handle static dynamic rewrite from middleware correctly', async () => {
const browser = await webdriver(next.url, '/rewrite-to-static')
......
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