Skip to content
Myr

context.test.ts

This file

Path

src/context.test.ts

98 lines · 3.2 KiB · TypeScript

Last changed by

refactor: directory structure (#114)Yusuke Wadabefore adoption0a6afc2

History of this file

Take it

refactor: directory structure (#114)Yusuke Wadabefore adoption0a6afc2

lines 1–98why, one step back
1import { Context } from './context'
23describe('Context', () => {4 const req = new Request('http://localhost/')5 const c = new Context(req)67 it('c.text()', async () => {8 const res = c.text('text in c', 201, { 'X-Custom': 'Message' })9 expect(res.status).toBe(201)10 expect(res.headers.get('Content-Type')).toBe('text/plain; charset=UTF-8')11 expect(await res.text()).toBe('text in c')12 expect(res.headers.get('X-Custom')).toBe('Message')13 })1415 it('c.json()', async () => {16 const res = c.json({ message: 'Hello' }, 201, { 'X-Custom': 'Message' })17 expect(res.status).toBe(201)18 expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')19 const text = await res.text()20 const object = JSON.parse(text)21 expect(object.message).toBe('Hello')22 expect(res.headers.get('X-Custom')).toBe('Message')23 })2425 it('c.html()', async () => {26 const res = c.html('<h1>Hello! Hono!</h1>', 201, { 'X-Custom': 'Message' })27 expect(res.status).toBe(201)28 expect(res.headers.get('Content-Type')).toMatch('text/html')29 expect(await res.text()).toBe('<h1>Hello! Hono!</h1>')30 expect(res.headers.get('X-Custom')).toBe('Message')31 })3233 it('c.redirect()', async () => {34 let res = c.redirect('/destination')35 expect(res.status).toBe(302)36 expect(res.headers.get('Location')).toMatch(/^https?:\/\/.+\/destination$/)37 res = c.redirect('https://example.com/destination')38 expect(res.status).toBe(302)39 expect(res.headers.get('Location')).toBe('https://example.com/destination')40 })4142 it('c.header()', async () => {43 c.header('X-Foo', 'Bar')44 const res = c.body('Hi')45 const foo = res.headers.get('X-Foo')46 expect(foo).toBe('Bar')47 })4849 it('c.status() and c.statusText()', async () => {50 c.status(201)51 const res = c.body('Hi')52 expect(res.status).toBe(201)53 expect(res.statusText).toBe('Created')54 })5556 it('Complext pattern', async () => {57 c.status(404)58 const res = c.json({ hono: 'great app' })59 expect(res.status).toBe(404)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 })6566 it('Content-Length', async () => {67 let res = c.text('abcdefg')68 expect(res.headers.get('Content-Length')).toBe('7')69 res = c.text('炎')70 expect(res.headers.get('Content-Length')).toBe('3')71 })7273 it('Headers, status, statusText', async () => {74 c.header('X-Custom1', 'Message1')75 c.header('X-Custom2', 'Message2')76 c.status(200)77 const res = c.newResponse('this is body', {78 status: 201,79 headers: {80 'X-Custom3': 'Message3',81 'X-Custom2': 'Message2-Override',82 },83 })84 expect(res.headers.get('X-Custom1')).toBe('Message1')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})