Skip to content
Myr

feat: auto set `statusText` (#93)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* refactor: context * tweak * feat: auto set status text

authored by Yusuke Wada · committed by GitHub · parent c883563 · Browse files · 6 files+75 added−20 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+75 added−20 removed

  • tests:+21 added−8 removed
  • other:+54 added−12 removed

6 files+75 added−20 removed

added:src/utils/http-status.test.ts+7 added
added:src/utils/http-status.ts+43 added
modified:src/context.ts+10 added-10 removed
modified:test/context.test.ts+11 added-6 removed
46 unmodified lines
47 })4849 it('c.status() and c.statusText()', async () => {50 c.status(201)51 c.statusText('Created!!!!')51 const res = c.body('Hi')52 expect(res.status).toBe(201)54 expect(res.statusText).toBe('Created!!!!')53 expect(res.statusText).toBe('Created')54 })5556 it('Complext pattern', async () => {57 c.status(404)59 c.statusText('Hono is Not Found')58 const res = c.json({ hono: 'great app' })59 expect(res.status).toBe(404)62 expect(res.statusText).toBe('Hono is Not Found')60 expect(res.statusText).toBe('Not Found')61 expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')62 const obj: { [key: string]: string } = await res.json()63 expect(obj['hono']).toBe('great app')64 })
4 unmodified lines
69 res = c.text('炎')70 expect(res.headers.get('Content-Length')).toBe('3')71 })7275 it('Headers', async () => {73 it('Headers, status, statusText', async () => {74 c.header('X-Custom1', 'Message1')75 c.header('X-Custom2', 'Message2')76 c.status(200)79 c.statusText('OK')77 const res = c.newResponse('this is body', {78 status: 201,79 headers: {80 'X-Custom3': 'Message3',
4 unmodified lines
85 expect(res.headers.get('X-Custom2')).toBe('Message2-Override')86 expect(res.headers.get('X-Custom3')).toBe('Message3')87 expect(res.status).toBe(201)88 expect(await res.text()).toBe('this is body')8990 // res is already setted.91 c.res = res92 c.header('X-Custom4', 'Message4')93 c.status(202)94 expect(c.res.headers.get('X-Custom4')).toBe('Message4')95 expect(c.res.status).toBe(201)96 expect(c.res.statusText).toBe('OK')97 })98})
modified:test/hono.test.ts+3 added-2 removed
4 unmodified lines
56 app.get('/hello', () => {7 return new Response('hello', {8 status: 200,9 statusText: 'Hono is OK',10 })11 })1213 app.get('/hello-with-shortcuts', (c) => {14 c.header('X-Custom', 'This is Hono')14 c.statusText('Hono is created')15 c.status(201)16 return c.html('<h1>Hono!!!</h1>')17 })18
7 unmodified lines
26 const req = new Request('http://localhost/hello')27 const res = await app.dispatch(req)28 expect(res).not.toBeNull()29 expect(res.status).toBe(200)30 expect(res.statusText).toBe('Hono is OK')31 expect(await res.text()).toBe('hello')32 })3334 it('GET /hell-with-shortcuts is ok', async () => {35 const req = new Request('http://localhost/hello-with-shortcuts')36 const res = await app.dispatch(req)37 expect(res).not.toBeNull()38 expect(res.status).toBe(201)38 expect(res.statusText).toBe('Hono is created')39 expect(res.statusText).toBe('Created')40 expect(res.headers.get('X-Custom')).toBe('This is Hono')41 expect(res.headers.get('Content-Type')).toMatch(/text\/html/)42 expect(await res.text()).toBe('<h1>Hono!!!</h1>')43 })
274 unmodified lines
modified:README.md+1 added-2 removed
251 unmodified lines
252app.get('/welcome', (c) => {253 c.header('X-Message', 'Hello!')254 c.header('Content-Type', 'text/plain')255 c.status(201)256 c.statusText('201 Content Created')256257 return c.body('Thank you for comming')258259 /*260 Same as:261 return new Response('Thank you for comming', {262 status: 201,264 statusText: '201 Content Created',263 statusText: 'Created',264 headers: {265 'X-Message': 'Hello',266 'Content-Type': 'text/plain',267 'Content-Length: '22'
203 unmodified lines