Skip to content

Commit 027e3df

Browse files
mixelburgmast1ff
andauthored
fix(method-override): handle Content-Type with charset parameter (#4894)
Co-authored-by: mast1ff <mast1ff@users.noreply.github.com>
1 parent f774f8d commit 027e3df

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

src/middleware/method-override/index.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,23 @@ describe('Method Override Middleware', () => {
9595
expect(data.contentType).toBe('application/x-www-form-urlencoded')
9696
})
9797

98+
it('Should override POST to DELETE - with charset in Content-Type', async () => {
99+
const params = new URLSearchParams()
100+
params.append('message', 'Hello')
101+
params.append('_method', 'DELETE')
102+
const res = await app.request('/posts', {
103+
body: params,
104+
headers: {
105+
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
106+
},
107+
method: 'POST',
108+
})
109+
expect(res.status).toBe(200)
110+
const data = await res.json()
111+
expect(data.method).toBe('DELETE')
112+
expect(data.message).toBe('Hello')
113+
})
114+
98115
it('Should override POST to PATCH - not found', async () => {
99116
const form = new FormData()
100117
form.append('message', 'Hello')

src/middleware/method-override/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export const methodOverride = (options: MethodOverrideOptions): MiddlewareHandle
8989
}
9090
}
9191
// Content-Type is `application/x-www-form-urlencoded`
92-
if (contentType === 'application/x-www-form-urlencoded') {
92+
if (contentType?.startsWith('application/x-www-form-urlencoded')) {
9393
const params = await parseBody<Record<string, string>>(clonedRequest)
9494
const method = params[methodFormName]
9595
if (method) {

0 commit comments

Comments
 (0)