2022-01-05 18:22:53 +00:00
|
|
|
import { Context } from '../src/context'
|
|
|
|
|
|
|
|
describe('Context', () => {
|
2022-01-11 17:14:53 +00:00
|
|
|
const req = new Request('http://localhost/')
|
2022-01-12 00:20:16 +00:00
|
|
|
const c = new Context(req)
|
2022-01-05 18:22:53 +00:00
|
|
|
|
2022-01-26 03:38:20 +00:00
|
|
|
it('c.text()', async () => {
|
2022-01-08 21:53:26 +00:00
|
|
|
const res = c.text('text in c', 201, { 'X-Custom': 'Message' })
|
|
|
|
expect(res.status).toBe(201)
|
2022-01-08 04:46:49 +00:00
|
|
|
expect(res.headers.get('Content-Type')).toBe('text/plain')
|
2022-01-05 18:22:53 +00:00
|
|
|
expect(await res.text()).toBe('text in c')
|
2022-01-08 21:53:26 +00:00
|
|
|
expect(res.headers.get('X-Custom')).toBe('Message')
|
2022-01-05 18:22:53 +00:00
|
|
|
})
|
|
|
|
|
2022-01-26 03:38:20 +00:00
|
|
|
it('c.json()', async () => {
|
2022-01-08 21:53:26 +00:00
|
|
|
const res = c.json({ message: 'Hello' }, 201, { 'X-Custom': 'Message' })
|
|
|
|
expect(res.status).toBe(201)
|
2022-01-08 04:46:49 +00:00
|
|
|
expect(res.headers.get('Content-Type')).toMatch('application/json')
|
2022-01-05 18:22:53 +00:00
|
|
|
const text = await res.text()
|
|
|
|
const object = JSON.parse(text)
|
|
|
|
expect(object.message).toBe('Hello')
|
2022-01-08 21:53:26 +00:00
|
|
|
expect(res.headers.get('X-Custom')).toBe('Message')
|
2022-01-05 18:22:53 +00:00
|
|
|
})
|
2022-01-08 04:46:49 +00:00
|
|
|
|
2022-01-26 03:38:20 +00:00
|
|
|
it('c.html()', async () => {
|
2022-01-08 21:53:26 +00:00
|
|
|
const res = c.html('<h1>Hello! Hono!</h1>', 201, { 'X-Custom': 'Message' })
|
|
|
|
expect(res.status).toBe(201)
|
2022-01-08 04:46:49 +00:00
|
|
|
expect(res.headers.get('Content-Type')).toMatch('text/html')
|
|
|
|
expect(await res.text()).toBe('<h1>Hello! Hono!</h1>')
|
2022-01-08 21:53:26 +00:00
|
|
|
expect(res.headers.get('X-Custom')).toBe('Message')
|
2022-01-08 04:46:49 +00:00
|
|
|
})
|
2022-01-09 12:46:46 +00:00
|
|
|
|
2022-01-26 03:38:20 +00:00
|
|
|
it('c.redirect()', async () => {
|
2022-01-09 15:03:48 +00:00
|
|
|
let res = c.redirect('/destination')
|
2022-01-09 12:46:46 +00:00
|
|
|
expect(res.status).toBe(302)
|
2022-01-09 15:03:48 +00:00
|
|
|
expect(res.headers.get('Location')).toMatch(/^https?:\/\/.+\/destination$/)
|
|
|
|
res = c.redirect('https://example.com/destination')
|
|
|
|
expect(res.status).toBe(302)
|
|
|
|
expect(res.headers.get('Location')).toBe('https://example.com/destination')
|
2022-01-09 12:46:46 +00:00
|
|
|
})
|
2022-01-26 03:38:20 +00:00
|
|
|
|
|
|
|
it('c.header()', async () => {
|
|
|
|
c.header('X-Foo', 'Bar')
|
|
|
|
const res = c.body('Hi')
|
|
|
|
const foo = res.headers.get('X-Foo')
|
|
|
|
expect(foo).toBe('Bar')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('c.status() and c.statusText()', async () => {
|
|
|
|
c.status(201)
|
|
|
|
c.statusText('Created!!!!')
|
|
|
|
const res = c.body('Hi')
|
|
|
|
expect(res.status).toBe(201)
|
|
|
|
expect(res.statusText).toBe('Created!!!!')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Complext pattern', async () => {
|
|
|
|
c.status(404)
|
|
|
|
c.statusText('Hono is Not Found')
|
|
|
|
const res = c.json({ hono: 'great app' })
|
|
|
|
expect(res.status).toBe(404)
|
|
|
|
expect(res.statusText).toBe('Hono is Not Found')
|
|
|
|
expect(res.headers.get('Content-Type')).toMatch(/json/)
|
|
|
|
const obj: { [key: string]: string } = await res.json()
|
|
|
|
expect(obj['hono']).toBe('great app')
|
|
|
|
})
|
2022-01-27 00:09:54 +00:00
|
|
|
|
|
|
|
it('Content-Length', async () => {
|
|
|
|
let res = c.text('abcdefg')
|
|
|
|
expect(res.headers.get('Content-Length')).toBe('7')
|
|
|
|
res = c.text('炎')
|
|
|
|
expect(res.headers.get('Content-Length')).toBe('3')
|
|
|
|
})
|
2022-01-05 18:22:53 +00:00
|
|
|
})
|