Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/run-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ function createActionParametersFromRequest ({ req, contextItem, actionInputs = {
const isFormData = req.is('application/x-www-form-urlencoded')
const isRaw = isRawWebAction(contextItem)

if (params.__ow_method === 'post' && req.body !== null) {
if (['post', 'put', 'patch', 'delete'].includes(params.__ow_method?.toLowerCase()) && req.body !== null) {
if (isRaw) {
if (isFormData) {
params.__ow_body = new URLSearchParams(req.body).toString() // convert json back to query string
Expand Down
21 changes: 21 additions & 0 deletions test/lib/run-dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ test('exports', () => {
})

describe('createActionParametersFromRequest', () => {
const methodsWithBody = ['PUT', 'PATCH', 'DELETE']

/** @private */
async function createAsyncFnCall ({ isRaw, mimeType, body, method }) {
const is = jest.fn((_type) => _type === mimeType)
Expand Down Expand Up @@ -243,6 +245,16 @@ describe('createActionParametersFromRequest', () => {
expect(actionParams.__ow_body).not.toBeDefined()
})

test.each(methodsWithBody)('non-raw: %s application/json', async (method) => {
const isRaw = false
const mimeType = 'application/json'
const body = { some: 'json' }

const actionParams = await createAsyncFnCall({ isRaw, mimeType, body, method })
expect(actionParams).toMatchObject(body)
expect(actionParams.__ow_body).not.toBeDefined()
})

test('non-raw: POST application/x-www-form-urlencoded', async () => {
const isRaw = false
const method = 'POST'
Expand Down Expand Up @@ -295,6 +307,15 @@ describe('createActionParametersFromRequest', () => {
expect(actionParams.__ow_body).toEqual(Buffer.from(JSON.stringify(body)).toString('base64')) // raw body will be base64'ed
})

test.each(methodsWithBody)('raw: %s application/json', async (method) => {
const isRaw = true
const mimeType = 'application/json'
const body = { some: 'json' } // simulate middleware processing

const actionParams = await createAsyncFnCall({ isRaw, mimeType, body, method })
expect(actionParams.__ow_body).toEqual(Buffer.from(JSON.stringify(body)).toString('base64')) // raw body will be base64'ed
})

test('raw: POST application/x-www-form-urlencoded', async () => {
const isRaw = true
const method = 'POST'
Expand Down
Loading