-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmod_test.ts
342 lines (319 loc) · 10.8 KB
/
mod_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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import { describe, it } from 'jsr:@std/[email protected]/bdd'
import { expect } from 'jsr:@std/[email protected]/expect'
import { makeFetch } from './mod.ts'
import type { Handler } from './types.ts'
import { AssertionError } from 'jsr:@std/[email protected]/assertion-error'
// this simulates the listener
class PseudoListener {
#listener: Deno.Listener
addr: Deno.NetAddr | undefined
adr = ''
rid = 0
ref = () => {}
unref = () => {};
[Symbol.asyncIterator]: unknown
conn: Deno.Conn | undefined
constructor(port: number) {
if (port === 0) port = this.fetchRandomPort()
this.#listener = Deno.listen({ port })
this.addr = this.#listener.addr as Deno.NetAddr
if (port === -1) this.addr.port = 0
this.rid = this.#listener.rid
}
fetchRandomPort() {
return Math.round(Math.random() * (9000 - 2000)) + 2000
}
accept = () => {
// deno-lint-ignore no-async-promise-executor
return new Promise<Deno.Conn>(async (resolve) => {
this.conn = await this.#listener.accept()
const httpConn = Deno.serveHttp(this.conn)
const requestEvent = await httpConn.nextRequest()
requestEvent?.respondWith(new Response('hello', { status: 200 }))
resolve(this.conn)
})
}
close = () => {
return this.#listener.close()
}
}
const tw = new TextDecoder()
describe('makeFetch', () => {
it('should work with HTTP handler', async () => {
const handler: Handler = () => new Response('Hello World')
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expect('Hello World')
})
it('should not crash with text/plain', async () => {
const handler: Handler = () =>
new Response('Hello World', {
headers: {
'Content-Type': 'text/plain;charset=UTF-8',
},
})
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expect('Hello World')
})
it('should parse JSON if response is JSON', async () => {
const handler: Handler = () =>
new Response(JSON.stringify({ hello: 'world' }), {
headers: { 'Content-Type': 'application/json' },
})
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expect({ hello: 'world' })
})
it('should fallback to arraybuffer', async () => {
const file = await Deno.readFile('README.md')
const handler: Handler = () =>
new Response(file, { headers: { 'Content-Type': 'text/markdown' } })
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expect(tw.decode(file))
})
it('should return empty response if content-type is null', async () => {
const handler: Handler = () => new Response(null)
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expect('')
})
it('should assign different ports if called many times', async () => {
const handler: Handler = () => new Response('hello')
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expect('hello')
const fetch_2 = makeFetch(handler)
const res_2 = await fetch_2('/')
res_2.expect('hello')
expect(res_2.port).not.toEqual(res.port)
})
it('should return port', async () => {
const handler: Handler = () => new Response('hello')
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expect('hello')
expect(res.port).toBe(parseInt(res.url.split(':').slice(-1)[0]))
})
})
describe('expectStatus', () => {
it('should pass with a correct status', async () => {
const handler: Handler = () => new Response('Hello World')
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expectStatus(200)
})
it('should optionally check for status text', async () => {
const handler: Handler = () => new Response('Hello World')
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expectStatus(200, 'OK')
})
it('should throw on incorrect status', async () => {
const handler: Handler = () => new Response('Hello World')
const fetch = makeFetch(handler)
const res = await fetch('/')
try {
res.expectStatus(404)
} catch (e) {
expect(e instanceof AssertionError).toBe(true)
expect((e as Error).message).toMatch(
new RegExp(
'Values are not equal: expected to have status code 404 but was 200',
),
)
}
})
})
describe('expectHeader', () => {
it('should pass with a correct header value', async () => {
const handler: Handler = () =>
new Response('Hello World', { headers: { 'Content-Type': 'text/plain' } })
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expectHeader('Content-Type', 'text/plain')
})
it('supports regex', async () => {
const handler: Handler = () =>
new Response('Hello World', { headers: { 'Content-Type': 'text/plain' } })
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expectHeader('Content-Type', /text/)
})
it('throws if value is incorrect', async () => {
const handler: Handler = () =>
new Response('Hello World', { headers: { 'Content-Type': 'text/html' } })
const fetch = makeFetch(handler)
const res = await fetch('/')
try {
res.expectHeader('Content-Type', 'text/plain')
} catch (e) {
expect(e instanceof AssertionError).toBe(true)
expect((e as Error).message).toMatch(
new RegExp(
'Values are not equal: expected to have header Content-Type with value text/plain, got text/html',
),
)
}
})
it('throws if does not match regex', async () => {
const handler: Handler = () =>
new Response('Hello World', { headers: { 'Content-Type': 'text/html' } })
const fetch = makeFetch(handler)
const res = await fetch('/')
try {
res.expectHeader('Content-Type', /image/)
} catch (e) {
expect((e as Error).message).toEqual(
'Expected actual: "text/html" to match: "/image/": expected header Content-Type to match regexp /image/, got text/html',
)
}
})
it('throws if header does not exist and regex is passed', async () => {
const handler: Handler = () =>
new Response('Hello World', { headers: undefined })
const fetch = makeFetch(handler)
const res = await fetch('/')
try {
res.expectHeader('garbage-header', /image/)
} catch (e) {
expect((e as Error).message).toMatch(
new RegExp('expected header null to not be empty'),
)
}
})
it('throws if header does not exist and array is passed', async () => {
const handler: Handler = () =>
new Response('Hello World', { headers: undefined })
const fetch = makeFetch(handler)
const res = await fetch('/')
try {
res.expectHeader('garbage-header', ['content-type', 'content-length'])
} catch (e) {
expect((e as Error).message).toMatch(
new RegExp('expected header null to not be empty'),
)
}
})
it('can expect array of header values', async () => {
const handler: Handler = () =>
new Response('Hello World', { headers: { A: '1,2,3' } })
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expectHeader('A', ['1', '2', '3'])
})
it('expects if header is not present', async () => {
const handler: Handler = () => new Response('Hello World')
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expectHeader('A', null)
})
it('should fallback to arraybuffer when formData is used', async () => {
const form = new FormData()
form.set('hello', 'world')
const handler: Handler = () =>
new Response(form, { headers: { 'Content-Type': 'multipart/form-data' } })
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expectHeader('Content-Type', 'multipart/form-data')
})
})
describe('expectBody', () => {
it('passes with correct body', async () => {
const handler: Handler = () => new Response('Hello World')
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expectBody('Hello World')
})
it('throws on incorrect body', async () => {
const handler: Handler = () => new Response('Hello World')
const fetch = makeFetch(handler)
const res = await fetch('/')
try {
res.expectBody('Hello World?')
} catch (e) {
expect(e).toBeDefined()
}
})
it('supports regex', async () => {
const handler: Handler = () => new Response('Hello World')
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expectBody(new RegExp('Hello'))
})
})
describe('expect', () => {
it('uses expectStatus if first arg is number', async () => {
const handler: Handler = () => new Response('Hello World')
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expect(200)
})
it('uses expectHeader if two arguments', async () => {
const handler: Handler = () =>
new Response('Hello World', {
headers: { 'Content-Type': 'text/plain' },
})
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expect('Content-Type', 'text/plain')
})
it('uses expectBody otherwise', async () => {
const handler: Handler = () => new Response('Hello World')
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expect('Hello World')
})
})
describe('Deno listener', () => {
it('should accept a listener', async () => {
const fetch = makeFetch(new PseudoListener(0) as unknown as Deno.Listener)
const res = await fetch('/')
res.expectStatus(200).expectBody('hello')
})
it('should throw error if port is -1', async () => {
const listener = new PseudoListener(-1)
try {
const fetch = makeFetch(listener as unknown as Deno.Listener)
await fetch('/')
} catch (e) {
expect((e as Error).message).toMatch(new RegExp('Port cannot be found'))
if (listener.conn?.rid) Deno.close(listener.conn?.rid + 1)
listener.close()
}
})
})
describe('Port randomness', () => {
it('should get a new port is already listening', async () => {
const port = 10649
let changed = false
const l = Deno.listen({ port })
globalThis.Math.random = () => {
if (!changed) {
changed = true
return 0.2
} else return 0.3
}
const handler: Handler = () => new Response('Hello World', { status: 200 })
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expectBody('Hello World')
expect(res.port).toBe(15462)
l.close()
})
it('should throw error if free port cannot be found', async () => {
globalThis.Deno.listen = () => {
throw new Error('bad error!')
}
const handler: Handler = () => new Response('Hello World', { status: 200 })
try {
const fetch = makeFetch(handler)
await fetch('/')
} catch (e) {
expect((e as Error).message).toMatch(
new RegExp('Unable to get free port'),
)
}
})
})