Skip to content

Commit

Permalink
Fix missing request body in DELETE and OPTIONS Route Handlers (#51874)
Browse files Browse the repository at this point in the history
It looks like the `content-length` header can't be ignored for the request forwarded to the render worker. See nodejs/node#27880.

Closes #48096, closes #49310. Fix NEXT-1194
fix NEXT-1114
  • Loading branch information
shuding authored Jun 27, 2023
1 parent 363b236 commit 7dfa56c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 0 additions & 1 deletion packages/next/src/server/lib/server-ipc/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export const forbiddenHeaders = [
'accept-encoding',
'content-length',
'keepalive',
'content-encoding',
'transfer-encoding',
Expand Down
22 changes: 22 additions & 0 deletions test/e2e/app-dir/app-routes/app-custom-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,28 @@ createNextDescribe(
expect(meta.body).toEqual(body)
})

it('can read a JSON encoded body for DELETE requests', async () => {
const body = { name: 'foo' }
const res = await next.fetch('/advanced/body/json', {
method: 'DELETE',
body: JSON.stringify(body),
})

expect(res.status).toEqual(200)
expect(await res.text()).toEqual('delete foo')
})

it('can read a JSON encoded body for OPTIONS requests', async () => {
const body = { name: 'bar' }
const res = await next.fetch('/advanced/body/json', {
method: 'OPTIONS',
body: JSON.stringify(body),
})

expect(res.status).toEqual(200)
expect(await res.text()).toEqual('options bar')
})

// we can't stream a body to a function currently only stream response
if (!isNextDeploy) {
it('can read a streamed JSON encoded body', async () => {
Expand Down
14 changes: 14 additions & 0 deletions test/e2e/app-dir/app-routes/app/advanced/body/json/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,17 @@ export async function POST(request: NextRequest) {
headers: withRequestMeta({ body }),
})
}

export async function DELETE(request: NextRequest) {
const body = await request.json()
return new Response('delete ' + body.name, {
status: 200,
})
}

export async function OPTIONS(request: NextRequest) {
const body = await request.json()
return new Response('options ' + body.name, {
status: 200,
})
}

0 comments on commit 7dfa56c

Please sign in to comment.