Skip to content
Myr

index.test.ts

This file

Path

src/middleware/logger/index.test.ts

204 lines · 6.2 KiB · TypeScript

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than f0b094d point at it.

Merge commit from forkYusuke Wadabefore adoptionf0b094d

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

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

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

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 3–204why, one step back
3
4describe('Logger by Middleware', () => {5 let app: Hono6 let log: string78 beforeEach(() => {9 function sleep(time: number) {10 return new Promise((resolve) => setTimeout(resolve, time))11 }1213 app = new Hono()1415 const logFn = (str: string) => {16 log = str17 }1819 const shortRandomString = 'hono'20 const longRandomString = 'hono'.repeat(1000)2122 app.use('*', logger(logFn))23 app.get('/short', (c) => c.text(shortRandomString))24 app.get('/long', (c) => c.text(longRandomString))25 app.get('/seconds', async (c) => {26 await sleep(1000)2728 return c.text(longRandomString)29 })30 app.get('/empty', (c) => c.text(''))31 app.get('/redirect', (c) => {32 return c.redirect('/empty', 301)33 })34 app.get('/server-error', (c) => {35 const res = new Response('', { status: 511 })36 if (c.req.query('status')) {37 // test status code not yet supported by runtime `Response` object38 Object.defineProperty(res, 'status', { value: parseInt(c.req.query('status') as string) })39 }40 return res41 })42 })4344 it('Log status 200 with empty body', async () => {45 const res = await app.request('http://localhost/empty')46 expect(res).not.toBeNull()47 expect(res.status).toBe(200)48 expect(log.startsWith('--> GET /empty \x1b[32m200\x1b[0m')).toBe(true)49 expect(log).toMatch(/m?s$/)50 })5152 it('Log status 200 with small body', async () => {53 const res = await app.request('http://localhost/short')54 expect(res).not.toBeNull()55 expect(res.status).toBe(200)56 expect(log.startsWith('--> GET /short \x1b[32m200\x1b[0m')).toBe(true)57 expect(log).toMatch(/m?s$/)58 })5960 it('Log status 200 with small body and query param', async () => {61 const res = await app.request('http://localhost/short?foo=bar')62 expect(res).not.toBeNull()63 expect(res.status).toBe(200)64 expect(log.startsWith('--> GET /short?foo=bar \x1b[32m200\x1b[0m')).toBe(true)65 expect(log).toMatch(/m?s$/)66 })6768 it('Log status 200 with big body', async () => {69 const res = await app.request('http://localhost/long')70 expect(res).not.toBeNull()71 expect(res.status).toBe(200)72 expect(log.startsWith('--> GET /long \x1b[32m200\x1b[0m')).toBe(true)73 expect(log).toMatch(/m?s$/)74 })7576 it('Time in seconds', async () => {77 const res = await app.request('http://localhost/seconds')78 expect(res).not.toBeNull()79 expect(res.status).toBe(200)80 expect(log.startsWith('--> GET /seconds \x1b[32m200\x1b[0m')).toBe(true)81 expect(log).toMatch(/1s/)82 })8384 it('Log status 301 with empty body', async () => {85 const res = await app.request('http://localhost/redirect')86 expect(res).not.toBeNull()87 expect(res.status).toBe(301)88 expect(log.startsWith('--> GET /redirect \x1b[36m301\x1b[0m')).toBe(true)89 expect(log).toMatch(/m?s$/)90 })9192 it('Log status 404', async () => {93 const msg = 'Default 404 Not Found'94 app.all('*', (c) => {95 return c.text(msg, 404)96 })97 const res = await app.request('http://localhost/notfound')98 expect(res).not.toBeNull()99 expect(res.status).toBe(404)100 expect(log.startsWith('--> GET /notfound \x1b[33m404\x1b[0m')).toBe(true)101 expect(log).toMatch(/m?s$/)102 })103104 it('Log status 511 with empty body', async () => {105 const res = await app.request('http://localhost/server-error')106 expect(res).not.toBeNull()107 expect(res.status).toBe(511)108 expect(log.startsWith('--> GET /server-error \x1b[31m511\x1b[0m')).toBe(true)109 expect(log).toMatch(/m?s$/)110 })111112 it('Log status 100', async () => {113 const res = await app.request('http://localhost/server-error?status=100')114 expect(res).not.toBeNull()115 expect(res.status).toBe(100)116 expect(log.startsWith('--> GET /server-error?status=100 100')).toBe(true)117 expect(log).toMatch(/m?s$/)118 })119120 it('Log status 700', async () => {121 const res = await app.request('http://localhost/server-error?status=700')122 expect(res).not.toBeNull()123 expect(res.status).toBe(700)124 expect(log.startsWith('--> GET /server-error?status=700 700')).toBe(true)125 expect(log).toMatch(/m?s$/)126 })127})128129describe('Logger by Middleware in NO_COLOR', () => {130 let app: Hono131 let log: string132133 beforeEach(() => {134 vi.stubEnv('NO_COLOR', '1')135 function sleep(time: number) {136 return new Promise((resolve) => setTimeout(resolve, time))137 }138139 app = new Hono()140141 const logFn = (str: string) => {142 log = str143 }144145 const shortRandomString = 'hono'146 const longRandomString = 'hono'.repeat(1000)147148 app.use('*', logger(logFn))149 app.get('/short', (c) => c.text(shortRandomString))150 app.get('/long', (c) => c.text(longRandomString))151 app.get('/seconds', async (c) => {152 await sleep(1000)153154 return c.text(longRandomString)155 })156 app.get('/empty', (c) => c.text(''))157 })158 afterAll(() => {159 vi.unstubAllEnvs()160 })161 it('Log status 200 with empty body', async () => {162 const res = await app.request('http://localhost/empty')163 expect(res).not.toBeNull()164 expect(res.status).toBe(200)165 expect(log.startsWith('--> GET /empty 200')).toBe(true)166 expect(log).toMatch(/m?s$/)167 })168169 it('Log status 200 with small body', async () => {170 const res = await app.request('http://localhost/short')171 expect(res).not.toBeNull()172 expect(res.status).toBe(200)173 expect(log.startsWith('--> GET /short 200')).toBe(true)174 expect(log).toMatch(/m?s$/)175 })176177 it('Log status 200 with big body', async () => {178 const res = await app.request('http://localhost/long')179 expect(res).not.toBeNull()180 expect(res.status).toBe(200)181 expect(log.startsWith('--> GET /long 200')).toBe(true)182 expect(log).toMatch(/m?s$/)183 })184185 it('Time in seconds', async () => {186 const res = await app.request('http://localhost/seconds')187 expect(res).not.toBeNull()188 expect(res.status).toBe(200)189 expect(log.startsWith('--> GET /seconds 200')).toBe(true)190 expect(log).toMatch(/1s/)191 })192193 it('Log status 404', async () => {194 const msg = 'Default 404 Not Found'195 app.all('*', (c) => {196 return c.text(msg, 404)197 })198 const res = await app.request('http://localhost/notfound')199 expect(res).not.toBeNull()200 expect(res.status).toBe(404)201 expect(log.startsWith('--> GET /notfound 404')).toBe(true)202 expect(log).toMatch(/m?s$/)203 })204})