-
-
Notifications
You must be signed in to change notification settings - Fork 668
/
Copy pathhandler.test.ts
122 lines (112 loc) · 3.6 KB
/
handler.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
import { describe } from 'vitest'
import { setCookie } from '../../helper/cookie'
import { Hono } from '../../hono'
import { encodeBase64 } from '../../utils/encode'
import type { CloudFrontEdgeEvent } from './handler'
import { createBody, handle, isContentTypeBinary } from './handler'
describe('isContentTypeBinary', () => {
it('Should determine whether it is binary', () => {
expect(isContentTypeBinary('image/png')).toBe(true)
expect(isContentTypeBinary('font/woff2')).toBe(true)
expect(isContentTypeBinary('image/svg+xml')).toBe(false)
expect(isContentTypeBinary('image/svg+xml; charset=UTF-8')).toBe(false)
expect(isContentTypeBinary('text/plain')).toBe(false)
expect(isContentTypeBinary('text/plain; charset=UTF-8')).toBe(false)
expect(isContentTypeBinary('text/css')).toBe(false)
expect(isContentTypeBinary('text/javascript')).toBe(false)
expect(isContentTypeBinary('application/json')).toBe(false)
expect(isContentTypeBinary('application/ld+json')).toBe(false)
expect(isContentTypeBinary('application/json')).toBe(false)
})
})
describe('createBody', () => {
it('Should the request be a GET or HEAD, the Request must not include a Body', () => {
const encoder = new TextEncoder()
const data = encoder.encode('test')
const body = {
action: 'read-only',
data: encodeBase64(data),
encoding: 'base64',
inputTruncated: false,
}
expect(createBody('GET', body)).toEqual(undefined)
expect(createBody('GET', body)).not.toEqual(data)
expect(createBody('HEAD', body)).toEqual(undefined)
expect(createBody('HEAD', body)).not.toEqual(data)
expect(createBody('POST', body)).toEqual(data)
expect(createBody('POST', body)).not.toEqual(undefined)
})
})
describe('handle', () => {
const cloudFrontEdgeEvent: CloudFrontEdgeEvent = {
Records: [
{
cf: {
config: {
distributionDomainName: 'd111111abcdef8.cloudfront.net',
distributionId: 'EDFDVBD6EXAMPLE',
eventType: 'viewer-request',
requestId: '4TyzHTaYWb1GX1qTfsHhEqV6HUDd_BzoBZnwfnvQc_1oF26ClkoUSEQ==',
},
request: {
clientIp: '1.2.3.4',
headers: {
host: [
{
key: 'Host',
value: 'hono.dev',
},
],
accept: [
{
key: 'accept',
value: '*/*',
},
],
},
method: 'GET',
querystring: '',
uri: '/test-path',
},
},
},
],
}
it('Should support alternate domain names', async () => {
const app = new Hono()
app.get('/test-path', (c) => {
return c.text(c.req.url)
})
const handler = handle(app)
const res = await handler(cloudFrontEdgeEvent)
expect(res.body).toBe('https://hono.dev/test-path')
})
it('Should support multiple cookies', async () => {
const app = new Hono()
app.get('/test-path', (c) => {
setCookie(c, 'cookie1', 'value1')
setCookie(c, 'cookie2', 'value2')
return c.text('')
})
const handler = handle(app)
const res = await handler(cloudFrontEdgeEvent)
expect(res.headers).toEqual({
'content-type': [
{
key: 'content-type',
value: 'text/plain; charset=UTF-8',
},
],
'set-cookie': [
{
key: 'set-cookie',
value: 'cookie1=value1; Path=/',
},
{
key: 'set-cookie',
value: 'cookie2=value2; Path=/',
},
],
})
})
})