Skip to content
Myr

index.test.ts

This file

Path

src/middleware/method-not-allowed/index.test.ts

292 lines · 9.4 KiB · TypeScript

Last changed by

chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2

History of this file

Take it

Merge pull request #5154 from honojs/nextYusuke Wadabefore adoptionb0c2d90

line 1why, one step back
1import { methodNotAllowed } from '.'

chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2

line 2why, one step back
2import { Hono } from '../../hono'

Merge pull request #5154 from honojs/nextYusuke Wadabefore adoptionb0c2d90

lines 3–292why, one step back
3
4describe('Method Not Allowed Middleware', () => {5 it('infers the environment from the app', () => {6 const app = new Hono<{ Variables: { requestId: string } }>()7 const middleware = methodNotAllowed({8 app,9 onMethodNotAllowed: (c) => c.text(c.var.requestId, 405),10 })1112 expect(middleware).toBeTypeOf('function')13 })1415 it('returns 405 with every allowed method for an existing path', async () => {16 const app = new Hono()17 app.use(methodNotAllowed({ app }))18 app.get('/resource', (c) => c.text('GET'))19 app.post('/resource', (c) => c.text('POST'))20 app.delete('/resource', (c) => c.text('DELETE'))2122 const res = await app.request('/resource', { method: 'PUT' })2324 expect(res.status).toBe(405)25 expect(res.headers.get('Allow')).toBe('GET, HEAD, POST, DELETE')26 expect(await res.text()).toBe('Method Not Allowed')2728 const resUsingCachedRouter = await app.request('/resource', { method: 'OPTIONS' })29 expect(resUsingCachedRouter.status).toBe(405)30 expect(resUsingCachedRouter.headers.get('Allow')).toBe('GET, HEAD, POST, DELETE')31 })3233 it('does not affect an allowed method', async () => {34 const app = new Hono()35 app.use(methodNotAllowed({ app }))36 app.get('/resource', (c) => c.text('GET'))3738 const res = await app.request('/resource')3940 expect(res.status).toBe(200)41 expect(res.headers.has('Allow')).toBe(false)42 expect(await res.text()).toBe('GET')43 })4445 it('returns 404 when the path does not exist', async () => {46 const app = new Hono()47 app.use(methodNotAllowed({ app }))48 app.get('/resource', (c) => c.text('GET'))4950 const res = await app.request('/missing', { method: 'POST' })5152 expect(res.status).toBe(404)53 expect(res.headers.has('Allow')).toBe(false)54 })5556 it('matches parameterized and overlapping routes without duplicate methods', async () => {57 const app = new Hono()58 app.use(methodNotAllowed({ app }))59 app.get('/users/:id', (_c, next) => next())60 app.get('/users/me', (c) => c.text('me'))61 app.patch('/users/:id', (c) => c.text(c.req.param('id')))6263 const res = await app.request('/users/me', { method: 'POST' })6465 expect(res.status).toBe(405)66 expect(res.headers.get('Allow')).toBe('GET, HEAD, PATCH')67 })6869 it('preserves an intentional 404 from a handler registered for the request method', async () => {70 const app = new Hono()71 app.use(methodNotAllowed({ app }))72 app.get('/users/:id', (c) => c.notFound())73 app.post('/users/:id', (c) => c.text('Created'))7475 const res = await app.request('/users/123')7677 expect(res.status).toBe(404)78 expect(res.headers.has('Allow')).toBe(false)79 })8081 it('preserves an intentional 404 for HEAD when a GET route is registered', async () => {82 const app = new Hono()83 app.use(methodNotAllowed({ app }))84 app.get('/users/:id', (c) => c.notFound())85 app.post('/users/:id', (c) => c.text('Created'))8687 const res = await app.request('/users/123', { method: 'HEAD' })8889 expect(res.status).toBe(404)90 expect(res.headers.has('Allow')).toBe(false)91 })9293 it('preserves a 404 returned by the error handler', async () => {94 const app = new Hono()95 app.onError((_error, c) => c.text('Handled error', 404))96 app.use(methodNotAllowed({ app }))97 app.use('/resource', () => {98 throw new Error('boom')99 })100 app.get('/resource', (c) => c.text('GET'))101102 const res = await app.request('/resource', { method: 'POST' })103104 expect(res.status).toBe(404)105 expect(res.headers.has('Allow')).toBe(false)106 expect(await res.text()).toBe('Handled error')107 })108109 it('returns 405 for HEAD when the path does not support GET', async () => {110 const app = new Hono()111 app.use(methodNotAllowed({ app }))112 app.post('/resource', (c) => c.text('POST'))113114 const res = await app.request('/resource', { method: 'HEAD' })115116 expect(res.status).toBe(405)117 expect(res.headers.get('Allow')).toBe('POST')118 expect(await res.text()).toBe('')119 })120121 it('does not advertise explicit HEAD routes that Hono cannot dispatch', async () => {122 const app = new Hono()123 app.use(methodNotAllowed({ app }))124 app.on('HEAD', '/resource', (c) => c.text('HEAD'))125126 const headRes = await app.request('/resource', { method: 'HEAD' })127 expect(headRes.status).toBe(404)128 expect(headRes.headers.has('Allow')).toBe(false)129130 const putRes = await app.request('/resource', { method: 'PUT' })131 expect(putRes.status).toBe(404)132 expect(putRes.headers.has('Allow')).toBe(false)133 })134135 it('supports custom methods', async () => {136 const app = new Hono()137 app.use(methodNotAllowed({ app }))138 app.on('PURGE', '/cache', (c) => c.text('Purged'))139140 const res = await app.request('/cache', { method: 'POST' })141142 expect(res.status).toBe(405)143 expect(res.headers.get('Allow')).toBe('PURGE')144 })145146 it('compares request methods case-sensitively', async () => {147 const app = new Hono()148 app.use(methodNotAllowed({ app }))149 app.patch('/item', (c) => c.text('PATCH'))150151 const request = new Request('http://localhost/item', { method: 'PATCH' })152 Object.defineProperty(request, 'method', { value: 'patch' })153 const res = await app.request(request)154155 expect(res.status).toBe(405)156 expect(res.headers.get('Allow')).toBe('PATCH')157 })158159 it('works with routes mounted using app.route()', async () => {160 const api = new Hono()161 api.get('/resource', (c) => c.text('GET'))162 api.post('/resource', (c) => c.text('POST'))163164 const app = new Hono()165 app.use(methodNotAllowed({ app }))166 app.route('/api', api)167168 const res = await app.request('/api/resource', { method: 'DELETE' })169170 expect(res.status).toBe(405)171 expect(res.headers.get('Allow')).toBe('GET, HEAD, POST')172 })173174 it('works when installed in an app mounted using app.route()', async () => {175 const api = new Hono().basePath('/v1')176 api.use(methodNotAllowed({ app: api }))177 api.get('/resource', (c) => c.text('GET'))178 api.post('/resource', (c) => c.text('POST'))179180 const app = new Hono()181 app.route('/api', api)182183 const res = await app.request('/api/v1/resource', { method: 'DELETE' })184185 expect(res.status).toBe(405)186 expect(res.headers.get('Allow')).toBe('GET, HEAD, POST')187 })188189 it('ignores ALL routes when collecting allowed methods', async () => {190 const app = new Hono()191 app.use(methodNotAllowed({ app }))192 app.all('/resource', (_c, next) => next())193194 const res = await app.request('/resource', { method: 'POST' })195196 expect(res.status).toBe(404)197 expect(res.headers.has('Allow')).toBe(false)198 })199200 it('does not invoke the error handler for a 405 response', async () => {201 const app = new Hono()202 const onError = vi.fn(() => new Response('error', { status: 500 }))203 app.onError(onError)204 app.use(methodNotAllowed({ app }))205 app.get('/resource', (c) => c.text('GET'))206207 const res = await app.request('/resource', { method: 'POST' })208209 expect(res.status).toBe(405)210 expect(onError).not.toHaveBeenCalled()211 })212213 it('preserves downstream headers and lets outer middleware observe the final status', async () => {214 const app = new Hono()215 let observedStatus: number | undefined216217 app.use(async (c, next) => {218 await next()219 observedStatus = c.res.status220 })221 app.use(methodNotAllowed({ app }))222 app.use(async (c, next) => {223 await next()224 c.header('X-Downstream', 'true')225 })226 app.get('/resource', (c) => c.text('GET'))227228 const res = await app.request('/resource', { method: 'POST' })229230 expect(res.status).toBe(405)231 expect(res.headers.get('X-Downstream')).toBe('true')232 expect(observedStatus).toBe(405)233 })234235 it('replaces stale representation headers from the not-found response', async () => {236 const app = new Hono()237 app.use(methodNotAllowed({ app }))238 app.get('/resource', (c) => c.text('GET'))239 app.post('/resource', (c) => c.text('POST'))240 app.notFound((c) =>241 c.text('Missing', 404, {242 Allow: 'BOGUS',243 'Content-Length': '7',244 })245 )246247 const res = await app.request('/resource', { method: 'PUT' })248249 expect(res.status).toBe(405)250 expect(res.headers.get('Allow')).toBe('GET, HEAD, POST')251 expect(res.headers.has('Content-Length')).toBe(false)252 expect(await res.text()).toBe('Method Not Allowed')253 })254255 it('supports a custom method-not-allowed response', async () => {256 const app = new Hono()257 app.use(258 methodNotAllowed({259 app,260 onMethodNotAllowed: (c, allowedMethods) =>261 c.json({ error: 'Method Not Allowed', allowedMethods }, 405, {262 Allow: allowedMethods.join(', '),263 }),264 })265 )266 app.get('/resource', (c) => c.text('GET'))267 app.post('/resource', (c) => c.text('POST'))268269 const res = await app.request('/resource', { method: 'PUT' })270271 expect(res.status).toBe(405)272 expect(res.headers.get('Allow')).toBe('GET, HEAD, POST')273 expect(res.headers.get('Content-Type')).toMatch(/^application\/json/)274 expect(await res.json()).toEqual({275 error: 'Method Not Allowed',276 allowedMethods: ['GET', 'HEAD', 'POST'],277 })278 })279280 it('leaves a non-404 custom not-found response unchanged', async () => {281 const app = new Hono()282 app.use(methodNotAllowed({ app }))283 app.get('/resource', (c) => c.text('GET'))284 app.notFound((c) => c.text('Missing', 410))285286 const res = await app.request('/resource', { method: 'POST' })287288 expect(res.status).toBe(410)289 expect(res.headers.has('Allow')).toBe(false)290 expect(await res.text()).toBe('Missing')291 })292})