Skip to content

Commit 9f5c107

Browse files
gatsbybotpieh
andauthored
fix(function): prioritize raw body parser (#35780) (#35786)
Co-authored-by: Michal Piechowiak <[email protected]>
1 parent e7a3e6e commit 9f5c107

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

integration-tests/functions/test-helpers.js

+50-1
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ export function runTests(env, host) {
637637
})
638638
})
639639

640-
describe(`custom type`, () => {
640+
describe(`custom type (using 'custom/type' payload)`, () => {
641641
const body = JSON.stringify({
642642
content: `test-string`,
643643
})
@@ -682,6 +682,55 @@ export function runTests(env, host) {
682682
`)
683683
})
684684
})
685+
686+
describe(`'application/json' payload`, () => {
687+
const body = JSON.stringify({
688+
content: `test-string`,
689+
})
690+
it(`on default config`, async () => {
691+
const result = await fetch(`${host}/api/config/defaults`, {
692+
method: `POST`,
693+
body,
694+
headers: {
695+
"content-type": "application/json",
696+
},
697+
})
698+
699+
expect(result.status).toBe(200)
700+
const responseBody = await result.json()
701+
702+
// default config will use default config for "application/json" type
703+
expect(responseBody).toMatchInlineSnapshot(`
704+
Object {
705+
"body": "{ content: 'test-string' }",
706+
}
707+
`)
708+
})
709+
710+
it(`on { bodyParser: { raw: { type: "*/*" }}}`, async () => {
711+
const result = await fetch(
712+
`${host}/api/config/body-parser-raw-type`,
713+
{
714+
method: `POST`,
715+
body,
716+
headers: {
717+
"content-type": "application/json",
718+
},
719+
}
720+
)
721+
722+
expect(result.status).toBe(200)
723+
const responseBody = await result.json()
724+
725+
// despite application/json payload, we get
726+
// expected Buffer
727+
expect(responseBody).toMatchInlineSnapshot(`
728+
Object {
729+
"body": "<Buffer 7b 22 63 6f 6e 74 65 6e 74 22 3a 22 74 65 73 74 2d 73 74 72 69 6e 67 22 7d>",
730+
}
731+
`)
732+
})
733+
})
685734
})
686735

687736
describe(`urlencoded`, () => {

packages/gatsby/src/internal-plugins/functions/middleware.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,10 @@ export function functionMiddlewares(
223223
setCookies,
224224
setContext,
225225
multer().any(),
226+
bodyParserMiddlewareWithConfig(`raw`),
226227
bodyParserMiddlewareWithConfig(`text`),
227228
bodyParserMiddlewareWithConfig(`urlencoded`),
228229
bodyParserMiddlewareWithConfig(`json`),
229-
bodyParserMiddlewareWithConfig(`raw`),
230230
executeFunction,
231231
]
232232
}

0 commit comments

Comments
 (0)