-
-
Notifications
You must be signed in to change notification settings - Fork 478
/
HttpResponse.test.ts
231 lines (200 loc) · 7.52 KB
/
HttpResponse.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* @vitest-environment node
*/
import { TextEncoder } from 'util'
import { HttpResponse } from './HttpResponse'
it('creates a plain response', async () => {
const response = new HttpResponse(null, { status: 301 })
expect(response.status).toBe(301)
expect(response.statusText).toBe('Moved Permanently')
expect(response.body).toBe(null)
expect(await response.text()).toBe('')
expect(Object.fromEntries(response.headers.entries())).toEqual({})
})
describe('HttpResponse.text()', () => {
it('creates a text response', async () => {
const response = HttpResponse.text('hello world', { status: 201 })
expect(response.status).toBe(201)
expect(response.statusText).toBe('Created')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(await response.text()).toBe('hello world')
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-length': '11',
'content-type': 'text/plain',
})
})
it('allows overriding the "Content-Type" response header', async () => {
const response = HttpResponse.text('hello world', {
headers: { 'Content-Type': 'text/plain; charset=utf-8' },
})
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(await response.text()).toBe('hello world')
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-length': '11',
'content-type': 'text/plain; charset=utf-8',
})
})
it('allows overriding the "Content-Length" response header', async () => {
const response = HttpResponse.text('hello world', {
headers: { 'Content-Length': '32' },
})
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-length': '32',
'content-type': 'text/plain',
})
})
})
describe('HttpResponse.json()', () => {
it('creates a json response given an object', async () => {
const response = HttpResponse.json({ firstName: 'John' })
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(await response.json()).toEqual({ firstName: 'John' })
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-length': '20',
'content-type': 'application/json',
})
})
it('creates a json response given an array', async () => {
const response = HttpResponse.json([1, 2, 3])
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(await response.json()).toEqual([1, 2, 3])
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-length': '7',
'content-type': 'application/json',
})
})
it('creates a json response given a plain string', async () => {
const response = HttpResponse.json(`"hello"`)
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(await response.json()).toBe(`"hello"`)
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-length': '11',
'content-type': 'application/json',
})
})
it('creates a json response given a number', async () => {
const response = HttpResponse.json(123)
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(await response.json()).toBe(123)
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-length': '3',
'content-type': 'application/json',
})
})
it('creates a json response given a json ReadableStream', async () => {
const encoder = new TextEncoder()
const stream = new ReadableStream({
start(controller) {
controller.enqueue(encoder.encode(`{"firstName`))
controller.enqueue(encoder.encode(`":"John`))
controller.enqueue(encoder.encode(`"}`))
controller.close()
},
})
const response = HttpResponse.json(stream)
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
// A ReadableStream instance is not a valid body init
// for the "Response.json()" static method. It gets serialized
// into a plain object.
expect(await response.json()).toEqual({})
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-length': '2',
'content-type': 'application/json',
})
})
it('allows overriding the "Content-Type" response header', async () => {
const response = HttpResponse.json(
{ a: 1 },
{
headers: {
'Content-Type': 'application/hal+json',
},
},
)
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(await response.json()).toEqual({ a: 1 })
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-length': '7',
'content-type': 'application/hal+json',
})
})
it('allows overriding the "Content-Length" response header', async () => {
const response = HttpResponse.json(
{ a: 1 },
{
headers: { 'Content-Length': '32' },
},
)
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-length': '32',
'content-type': 'application/json',
})
})
})
describe('HttpResponse.xml()', () => {
it('creates an xml response', async () => {
const response = HttpResponse.xml('<user name="John" />')
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(await response.text()).toBe('<user name="John" />')
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-type': 'text/xml',
})
})
it('allows overriding the "Content-Type" response header', async () => {
const response = HttpResponse.xml('<user name="John" />', {
headers: {
'Content-Type': 'text/xml; charset=utf-8',
},
})
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(await response.text()).toBe('<user name="John" />')
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-type': 'text/xml; charset=utf-8',
})
})
})
it('creates an array buffer response', async () => {
const buffer = new TextEncoder().encode('hello world')
const response = HttpResponse.arrayBuffer(buffer)
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
const responseData = await response.arrayBuffer()
expect(responseData).toEqual(buffer.buffer)
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-length': '11',
})
})
it('creates a form data response', async () => {
const formData = new FormData()
formData.append('firstName', 'John')
const response = HttpResponse.formData(formData)
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
const responseData = await response.formData()
expect(responseData.get('firstName')).toBe('John')
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-type': expect.stringContaining(
'multipart/form-data; boundary=----',
),
})
})