index.test.ts
This file
Path
src/middleware/method-override/index.test.ts
221 lines · 7.0 KiB · TypeScript
Last changed by
fix(method-override): set duplex when forwarding a stream body in query mode (#5110)Yusuke Wadabefore adoptione0cdeb0
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 1–187why, one step back1import { Hono } from '../../hono'2import { methodOverride } from './index'34describe('Method Override Middleware', () => {5 describe('Form', () => {6 const app = new Hono()7 app.use('/posts/*', methodOverride({ app }))8 app.use('/posts-custom/*', methodOverride({ app, form: 'custom-input-name' }))9 app.on(['post', 'delete'], ['/posts', '/posts-custom'], async (c) => {10 const form = await c.req.formData()11 return c.json({12 method: c.req.method,13 message: form.get('message'),14 contentType: c.req.header('content-type') ?? '',15 })16 })1718 describe('multipart/form-data', () => {19 it('Should override POST to DELETE', async () => {20 const form = new FormData()21 form.append('message', 'Hello')22 form.append('_method', 'DELETE')23 const res = await app.request('/posts', {24 body: form,25 method: 'POST',26 })27 expect(res.status).toBe(200)28 const data = await res.json()29 expect(data.method).toBe('DELETE')30 expect(data.message).toBe('Hello')31 expect(data.contentType).toMatch(/^multipart\/form-data;/)32 })3334 it('Should override POST to DELETE - with a custom form input name', async () => {35 const form = new FormData()36 form.append('message', 'Hello')37 form.append('custom-input-name', 'DELETE')38 const res = await app.request('/posts-custom', {39 body: form,40 method: 'POST',41 })42 expect(res.status).toBe(200)43 const data = await res.json()44 expect(data.method).toBe('DELETE')45 expect(data.message).toBe('Hello')46 expect(data.contentType).toMatch(/^multipart\/form-data;/)47 })4849 it('Should override POST to PATCH - not found', async () => {50 const form = new FormData()51 form.append('message', 'Hello')52 form.append('_method', 'PATCH')53 const res = await app.request('/posts', {54 body: form,55 method: 'POST',56 })57 expect(res.status).toBe(404)58 })59 })6061 describe('application/x-www-form-urlencoded', () => {62 it('Should override POST to DELETE', async () => {63 const params = new URLSearchParams()64 params.append('message', 'Hello')65 params.append('_method', 'DELETE')66 const res = await app.request('/posts', {67 body: params,68 headers: {69 'Content-Type': 'application/x-www-form-urlencoded',70 },71 method: 'POST',72 })73 expect(res.status).toBe(200)74 const data = await res.json()75 expect(data.method).toBe('DELETE')76 expect(data.message).toBe('Hello')77 expect(data.contentType).toBe('application/x-www-form-urlencoded')78 })7980 it('Should override POST to DELETE - with a custom form input name', async () => {81 const params = new URLSearchParams()82 params.append('message', 'Hello')83 params.append('custom-input-name', 'DELETE')84 const res = await app.request('/posts-custom', {85 body: params,86 headers: {87 'Content-Type': 'application/x-www-form-urlencoded',88 },89 method: 'POST',90 })91 expect(res.status).toBe(200)92 const data = await res.json()93 expect(data.method).toBe('DELETE')94 expect(data.message).toBe('Hello')95 expect(data.contentType).toBe('application/x-www-form-urlencoded')96 })9798 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 })114115 it('Should override POST to PATCH - not found', async () => {116 const form = new FormData()117 form.append('message', 'Hello')118 form.append('_method', 'PATCH')119 const res = await app.request('/posts', {120 body: form,121 method: 'POST',122 })123 expect(res.status).toBe(404)124 })125 })126 })127128 describe('Header', () => {129 const app = new Hono()130 app.use('/posts/*', methodOverride({ app, header: 'X-METHOD-OVERRIDE' }))131 app.on(['get', 'post', 'delete'], '/posts', async (c) => {132 return c.json({133 method: c.req.method,134 headerValue: c.req.header('X-METHOD-OVERRIDE') ?? null,135 })136 })137138 it('Should override POST to DELETE', async () => {139 const res = await app.request('/posts', {140 method: 'POST',141 headers: {142 'X-METHOD-OVERRIDE': 'DELETE',143 },144 })145 expect(res.status).toBe(200)146 expect(await res.json()).toEqual({147 method: 'DELETE',148 headerValue: null,149 })150 })151152 it('Should not override GET request', async () => {153 const res = await app.request('/posts', {154 method: 'GET',155 headers: {156 'X-METHOD-OVERRIDE': 'DELETE',157 },158 })159 expect(res.status).toBe(200)160 expect(await res.json()).toEqual({161 method: 'GET',162 headerValue: 'DELETE', // It does not modify the headers.163 })164 })165 })166167 describe('Query', () => {168 const app = new Hono()169 app.use('/posts/*', methodOverride({ app, query: '_method' }))170 app.on(['get', 'post', 'delete'], '/posts', async (c) => {171 return c.json({172 method: c.req.method,173 queryValue: c.req.query('_method') ?? null,174 })175 })176177 it('Should override POST to DELETE', async () => {178 const res = await app.request('/posts?_method=delete', {179 method: 'POST',180 })181 expect(res.status).toBe(200)182 expect(await res.json()).toEqual({183 method: 'DELETE',184 queryValue: null,185 })186 })187fix(method-override): set duplex when forwarding a stream body in query mode (#5110)Yusuke Wadabefore adoptione0cdeb0
lines 188–209why, one step back188 it('Should override POST to DELETE when the request has a body', async () => {189 const app = new Hono()190 app.use('/posts/*', methodOverride({ app, query: '_method' }))191 app.on(['post', 'delete'], '/posts', async (c) => {192 return c.json({193 method: c.req.method,194 body: await c.req.text(),195 queryValue: c.req.query('_method') ?? null,196 })197 })198 const res = await app.request('/posts?_method=delete', {199 method: 'POST',200 body: new URLSearchParams({ message: 'Hello' }),201 })202 expect(res.status).toBe(200)203 expect(await res.json()).toEqual({204 method: 'DELETE',205 body: 'message=Hello',206 queryValue: null,207 })208 })209Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 210–221why, one step back210 it('Should not override GET request', async () => {211 const res = await app.request('/posts?_method=delete', {212 method: 'GET',213 })214 expect(res.status).toBe(200)215 expect(await res.json()).toEqual({216 method: 'GET',217 queryValue: 'delete', // It does not modify the queries.218 })219 })220 })221})