Unverified Commit 54ff3223 authored by Vitaly Baev's avatar Vitaly Baev Committed by GitHub

Don't throw 500 error when Content-type is invalid (#24818)

Fixes #24768

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
parent 192d42bc
......@@ -114,7 +114,12 @@ export async function parseBody(
req: NextApiRequest,
limit: string | number
): Promise<any> {
const contentType = parse(req.headers['content-type'] || 'text/plain')
let contentType
try {
contentType = parse(req.headers['content-type'] || 'text/plain')
} catch {
contentType = parse('text/plain')
}
const { type, parameters } = contentType
const encoding = parameters.charset || 'utf-8'
......
......@@ -36,6 +36,13 @@ function runTests() {
expect(data).toEqual([{ title: 'Nextjs' }])
killApp(server)
})
it("should not throw if request's content-type is invalid", async () => {
await startServer()
const status = await makeRequestWithInvalidContentType()
expect(status).toBe(200)
killApp(server)
})
}
async function makeRequest() {
......@@ -50,6 +57,18 @@ async function makeRequest() {
return data
}
async function makeRequestWithInvalidContentType() {
const status = await fetchViaHTTP(appPort, '/api', null, {
method: 'POST',
headers: {
'Content-Type': 'application/json;',
},
body: JSON.stringify([{ title: 'Nextjs' }]),
}).then((res) => res.status)
return status
}
const startServer = async (optEnv = {}, opts) => {
const scriptPath = join(appDir, 'server.js')
context.appPort = appPort = await getPort()
......
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