-
-
Notifications
You must be signed in to change notification settings - Fork 444
/
context.test.ts
145 lines (127 loc) · 4.33 KB
/
context.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { Context } from './context.ts'
describe('Context', () => {
const req = new Request('http://localhost/')
let c: Context
beforeEach(() => {
c = new Context(req)
})
it('c.text()', async () => {
const res = c.text('text in c', 201, { 'X-Custom': 'Message' })
expect(res.status).toBe(201)
expect(res.headers.get('Content-Type')).toBe('text/plain; charset=UTF-8')
expect(await res.text()).toBe('text in c')
expect(res.headers.get('X-Custom')).toBe('Message')
})
it('c.json()', async () => {
const res = c.json({ message: 'Hello' }, 201, { 'X-Custom': 'Message' })
expect(res.status).toBe(201)
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
const text = await res.text()
expect(text).toBe('{"message":"Hello"}')
expect(res.headers.get('X-Custom')).toBe('Message')
})
it('c.json() with c.pretty(true)', async () => {
c.pretty(true)
const res = c.json({ message: 'Hello' })
const text = await res.text()
expect(text).toBe(`{
"message": "Hello"
}`)
})
it('c.json() with c.pretty(true, 4)', async () => {
c.pretty(true, 4)
const res = c.json({ message: 'Hello' })
const text = await res.text()
expect(text).toBe(`{
"message": "Hello"
}`)
})
it('c.html()', async () => {
const res = c.html('<h1>Hello! Hono!</h1>', 201, { 'X-Custom': 'Message' })
expect(res.status).toBe(201)
expect(res.headers.get('Content-Type')).toMatch('text/html')
expect(await res.text()).toBe('<h1>Hello! Hono!</h1>')
expect(res.headers.get('X-Custom')).toBe('Message')
})
it('c.redirect()', async () => {
let res = c.redirect('/destination')
expect(res.status).toBe(302)
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')
})
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()', async () => {
c.status(201)
const res = c.body('Hi')
expect(res.status).toBe(201)
})
it('Complex pattern', async () => {
c.status(404)
const res = c.json({ hono: 'great app' })
expect(res.status).toBe(404)
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
const obj: { [key: string]: string } = await res.json()
expect(obj['hono']).toBe('great app')
})
it('Has headers and status', async () => {
c.header('x-custom1', 'Message1')
c.header('x-custom2', 'Message2')
c.status(200)
const res = c.newResponse('this is body', 201, {
'x-custom3': 'Message3',
'x-custom2': 'Message2-Override',
})
expect(res.headers.get('x-Custom1')).toBe('Message1')
expect(res.headers.get('x-Custom2')).toBe('Message2-Override')
expect(res.headers.get('x-Custom3')).toBe('Message3')
expect(res.status).toBe(201)
expect(await res.text()).toBe('this is body')
// res is already set.
c.res = res
c.header('X-Custom4', 'Message4')
c.status(202)
expect(c.res.headers.get('X-Custom4')).toBe('Message4')
expect(c.res.status).toBe(201)
})
it('Should return 200 response', async () => {
const res = c.text('Text')
expect(res.status).toBe(200)
})
it('Should return 204 response', async () => {
c.status(204)
const res = c.body(null)
expect(res.status).toBe(204)
expect(await res.text()).toBe('')
})
it('Should be able read env', async () => {
const req = new Request('http://localhost/')
const key = 'a-secret-key'
const ctx = new Context(req, {
API_KEY: key,
})
expect(ctx.env.API_KEY).toBe(key)
})
it('set and set', async () => {
const ctx = new Context(req)
expect(ctx.get('k-foo')).toEqual(undefined)
ctx.set('k-foo', 'v-foo')
expect(ctx.get('k-foo')).toEqual('v-foo')
expect(ctx.get('k-bar')).toEqual(undefined)
ctx.set('k-bar', { k: 'v' })
expect(ctx.get('k-bar')).toEqual({ k: 'v' })
})
it('has res object by default', async () => {
c = new Context(req)
c.res.headers.append('foo', 'bar')
const res = c.text('foo')
expect(res.headers.get('foo')).not.toBeNull()
expect(res.headers.get('foo')).toBe('bar')
})
})