Skip to content

Commit c937811

Browse files
Copilotshazron
andauthored
Parse request bodies for PUT/PATCH/DELETE in local dev web action params (#165)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: shazron <36107+shazron@users.noreply.github.com>
1 parent 87c8af9 commit c937811

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

src/lib/run-dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ function createActionParametersFromRequest ({ req, contextItem, actionInputs = {
601601
const isFormData = req.is('application/x-www-form-urlencoded')
602602
const isRaw = isRawWebAction(contextItem)
603603

604-
if (params.__ow_method === 'post' && req.body !== null) {
604+
if (['post', 'put', 'patch', 'delete'].includes(params.__ow_method?.toLowerCase()) && req.body !== null) {
605605
if (isRaw) {
606606
if (isFormData) {
607607
params.__ow_body = new URLSearchParams(req.body).toString() // convert json back to query string

test/lib/run-dev.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ test('exports', () => {
192192
})
193193

194194
describe('createActionParametersFromRequest', () => {
195+
const methodsWithBody = ['PUT', 'PATCH', 'DELETE']
196+
195197
/** @private */
196198
async function createAsyncFnCall ({ isRaw, mimeType, body, method }) {
197199
const is = jest.fn((_type) => _type === mimeType)
@@ -243,6 +245,16 @@ describe('createActionParametersFromRequest', () => {
243245
expect(actionParams.__ow_body).not.toBeDefined()
244246
})
245247

248+
test.each(methodsWithBody)('non-raw: %s application/json', async (method) => {
249+
const isRaw = false
250+
const mimeType = 'application/json'
251+
const body = { some: 'json' }
252+
253+
const actionParams = await createAsyncFnCall({ isRaw, mimeType, body, method })
254+
expect(actionParams).toMatchObject(body)
255+
expect(actionParams.__ow_body).not.toBeDefined()
256+
})
257+
246258
test('non-raw: POST application/x-www-form-urlencoded', async () => {
247259
const isRaw = false
248260
const method = 'POST'
@@ -295,6 +307,15 @@ describe('createActionParametersFromRequest', () => {
295307
expect(actionParams.__ow_body).toEqual(Buffer.from(JSON.stringify(body)).toString('base64')) // raw body will be base64'ed
296308
})
297309

310+
test.each(methodsWithBody)('raw: %s application/json', async (method) => {
311+
const isRaw = true
312+
const mimeType = 'application/json'
313+
const body = { some: 'json' } // simulate middleware processing
314+
315+
const actionParams = await createAsyncFnCall({ isRaw, mimeType, body, method })
316+
expect(actionParams.__ow_body).toEqual(Buffer.from(JSON.stringify(body)).toString('base64')) // raw body will be base64'ed
317+
})
318+
298319
test('raw: POST application/x-www-form-urlencoded', async () => {
299320
const isRaw = true
300321
const method = 'POST'

0 commit comments

Comments
 (0)