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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222import { Hono } from '../../hono'import { methodOverride } from './index'
describe('Method Override Middleware', () => { describe('Form', () => { const app = new Hono() app.use('/posts/*', methodOverride({ app })) app.use('/posts-custom/*', methodOverride({ app, form: 'custom-input-name' })) app.on(['post', 'delete'], ['/posts', '/posts-custom'], async (c) => { const form = await c.req.formData() return c.json({ method: c.req.method, message: form.get('message'), contentType: c.req.header('content-type') ?? '', }) })
describe('multipart/form-data', () => { it('Should override POST to DELETE', async () => { const form = new FormData() form.append('message', 'Hello') form.append('_method', 'DELETE') const res = await app.request('/posts', { body: form, method: 'POST', }) expect(res.status).toBe(200) const data = await res.json() expect(data.method).toBe('DELETE') expect(data.message).toBe('Hello') expect(data.contentType).toMatch(/^multipart\/form-data;/) })
it('Should override POST to DELETE - with a custom form input name', async () => { const form = new FormData() form.append('message', 'Hello') form.append('custom-input-name', 'DELETE') const res = await app.request('/posts-custom', { body: form, method: 'POST', }) expect(res.status).toBe(200) const data = await res.json() expect(data.method).toBe('DELETE') expect(data.message).toBe('Hello') expect(data.contentType).toMatch(/^multipart\/form-data;/) })
it('Should override POST to PATCH - not found', async () => { const form = new FormData() form.append('message', 'Hello') form.append('_method', 'PATCH') const res = await app.request('/posts', { body: form, method: 'POST', }) expect(res.status).toBe(404) }) })
describe('application/x-www-form-urlencoded', () => { it('Should override POST to DELETE', async () => { const params = new URLSearchParams() params.append('message', 'Hello') params.append('_method', 'DELETE') const res = await app.request('/posts', { body: params, headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, method: 'POST', }) expect(res.status).toBe(200) const data = await res.json() expect(data.method).toBe('DELETE') expect(data.message).toBe('Hello') expect(data.contentType).toBe('application/x-www-form-urlencoded') })
it('Should override POST to DELETE - with a custom form input name', async () => { const params = new URLSearchParams() params.append('message', 'Hello') params.append('custom-input-name', 'DELETE') const res = await app.request('/posts-custom', { body: params, headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, method: 'POST', }) expect(res.status).toBe(200) const data = await res.json() expect(data.method).toBe('DELETE') expect(data.message).toBe('Hello') expect(data.contentType).toBe('application/x-www-form-urlencoded') })
it('Should override POST to DELETE - with charset in Content-Type', async () => { const params = new URLSearchParams() params.append('message', 'Hello') params.append('_method', 'DELETE') const res = await app.request('/posts', { body: params, headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', }, method: 'POST', }) expect(res.status).toBe(200) const data = await res.json() expect(data.method).toBe('DELETE') expect(data.message).toBe('Hello') })
it('Should override POST to PATCH - not found', async () => { const form = new FormData() form.append('message', 'Hello') form.append('_method', 'PATCH') const res = await app.request('/posts', { body: form, method: 'POST', }) expect(res.status).toBe(404) }) }) })
describe('Header', () => { const app = new Hono() app.use('/posts/*', methodOverride({ app, header: 'X-METHOD-OVERRIDE' })) app.on(['get', 'post', 'delete'], '/posts', async (c) => { return c.json({ method: c.req.method, headerValue: c.req.header('X-METHOD-OVERRIDE') ?? null, }) })
it('Should override POST to DELETE', async () => { const res = await app.request('/posts', { method: 'POST', headers: { 'X-METHOD-OVERRIDE': 'DELETE', }, }) expect(res.status).toBe(200) expect(await res.json()).toEqual({ method: 'DELETE', headerValue: null, }) })
it('Should not override GET request', async () => { const res = await app.request('/posts', { method: 'GET', headers: { 'X-METHOD-OVERRIDE': 'DELETE', }, }) expect(res.status).toBe(200) expect(await res.json()).toEqual({ method: 'GET', headerValue: 'DELETE', // It does not modify the headers. }) }) })
describe('Query', () => { const app = new Hono() app.use('/posts/*', methodOverride({ app, query: '_method' })) app.on(['get', 'post', 'delete'], '/posts', async (c) => { return c.json({ method: c.req.method, queryValue: c.req.query('_method') ?? null, }) })
it('Should override POST to DELETE', async () => { const res = await app.request('/posts?_method=delete', { method: 'POST', }) expect(res.status).toBe(200) expect(await res.json()).toEqual({ method: 'DELETE', queryValue: null, }) })
it('Should override POST to DELETE when the request has a body', async () => { const app = new Hono() app.use('/posts/*', methodOverride({ app, query: '_method' })) app.on(['post', 'delete'], '/posts', async (c) => { return c.json({ method: c.req.method, body: await c.req.text(), queryValue: c.req.query('_method') ?? null, }) }) const res = await app.request('/posts?_method=delete', { method: 'POST', body: new URLSearchParams({ message: 'Hello' }), }) expect(res.status).toBe(200) expect(await res.json()).toEqual({ method: 'DELETE', body: 'message=Hello', queryValue: null, }) })
it('Should not override GET request', async () => { const res = await app.request('/posts?_method=delete', { method: 'GET', }) expect(res.status).toBe(200) expect(await res.json()).toEqual({ method: 'GET', queryValue: 'delete', // It does not modify the queries. }) }) })})