index.test.ts
This file
Path
src/middleware/timeout/index.test.ts
70 lines · 2.4 KiB · TypeScript
Last changed by
chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
lines 1–2why, one step back1import type { HTTPExceptionFunction } from '.'2import { timeout } from '.'Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 3–70why, one step back3import type { Context } from '../../context'4import { Hono } from '../../hono'5import { HTTPException } from '../../http-exception'67describe('Timeout API', () => {8 const app = new Hono()910 app.use('/slow-endpoint', timeout(1000))11 app.use(12 '/slow-endpoint/custom',13 timeout(14 1100,15 () => new HTTPException(408, { message: 'Request timeout. Please try again later.' })16 )17 )18 const exception500: HTTPExceptionFunction = (context: Context) =>19 new HTTPException(500, { message: `Internal Server Error at ${context.req.path}` })20 app.use('/slow-endpoint/error', timeout(1200, exception500))21 app.use('/normal-endpoint', timeout(1000))2223 app.get('/slow-endpoint', async (c) => {24 await new Promise((resolve) => setTimeout(resolve, 1100))25 return c.text('This should not show up')26 })2728 app.get('/slow-endpoint/custom', async (c) => {29 await new Promise((resolve) => setTimeout(resolve, 1200))30 return c.text('This should not show up')31 })3233 app.get('/slow-endpoint/error', async (c) => {34 await new Promise((resolve) => setTimeout(resolve, 1300))35 return c.text('This should not show up')36 })3738 app.get('/normal-endpoint', async (c) => {39 await new Promise((resolve) => setTimeout(resolve, 900))40 return c.text('This should not show up')41 })4243 it('Should trigger default timeout exception', async () => {44 const res = await app.request('http://localhost/slow-endpoint')45 expect(res).not.toBeNull()46 expect(res.status).toBe(504)47 expect(await res.text()).toContain('Gateway Timeout')48 })4950 it('Should apply custom exception with function', async () => {51 const res = await app.request('http://localhost/slow-endpoint/custom')52 expect(res).not.toBeNull()53 expect(res.status).toBe(408)54 expect(await res.text()).toContain('Request timeout. Please try again later.')55 })5657 it('Error timeout with custom status code and message', async () => {58 const res = await app.request('http://localhost/slow-endpoint/error')59 expect(res).not.toBeNull()60 expect(res.status).toBe(500)61 expect(await res.text()).toContain('Internal Server Error at /slow-endpoint/error')62 })6364 it('No Timeout should pass', async () => {65 const res = await app.request('http://localhost/normal-endpoint')66 expect(res).not.toBeNull()67 expect(res.status).toBe(200)68 expect(await res.text()).toContain('This should not show up')69 })70})