-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.test.ts
89 lines (76 loc) · 2.85 KB
/
fetch.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
import { FsCache } from './cache.ts'
import { asserts, porter } from './deps.ts'
import { cachingFetch, decodeResponse, encodeResponse, SimplifiedFetchParams, simplifyFetchParams } from './fetch.ts'
import { Md5 } from './mod.ts'
import { concatenate } from './string.ts'
const furtherSimplify = (simplified: SimplifiedFetchParams) => ({
method: simplified.method,
headers: [...simplified.headers.entries()],
body: simplified.body,
url: simplified.url,
})
Deno.test(`simplifyFetchParams does just that`, async () => {
const selfSimplified = await simplifyFetchParams('https://example.com', {
method: 'POST',
body: 'Hello there!',
headers: { 'x-whatever': 'be happy' },
})
asserts.assertEquals(
furtherSimplify(selfSimplified),
furtherSimplify({
body: new TextEncoder().encode('Hello there!'),
headers: new Headers({ 'x-whatever': 'be happy', 'content-type': 'text/plain;charset=UTF-8' }),
method: 'POST',
url: 'https://example.com/',
}),
)
})
Deno.test(`simplifyFetchParams simplifies even when the details are in the first argument`, async () => {
const selfSimplified = await simplifyFetchParams(
new Request('https://example.com', {
method: 'POST',
body: 'Hello there!',
headers: { 'x-whatever': 'be happy' },
}),
)
asserts.assertEquals(
furtherSimplify(selfSimplified),
furtherSimplify({
body: new TextEncoder().encode('Hello there!'),
headers: new Headers({ 'x-whatever': 'be happy', 'content-type': 'text/plain;charset=UTF-8' }),
method: 'POST',
url: 'https://example.com/',
}),
)
})
Deno.test('Response encoding/decoding basically works', async () => {
const response = decodeResponse(
await encodeResponse(
new Response('Hello there!', {
status: 202,
statusText: 'Custom Ok',
headers: { 'x-cool': 'true' },
}),
),
)
asserts.assertEquals(await response.text(), 'Hello there!')
asserts.assertEquals(response.status, 202)
asserts.assertEquals(response.statusText, 'Custom Ok')
asserts.assertEquals(response.headers.get('x-cool'), 'true')
})
Deno.test('cachingFetch caches fetches', async () => {
const cache = new FsCache(concatenate([Md5.hash(import.meta.url), 'responses']))
const trueBody = 'This is the real deal here'
const port = await porter.getAvailablePort()
const abortController = new AbortController()
await cache.clear() // Clear the cache to prevent any existing cache hits for the fetch that is supposed go to the server
const server = Deno.serve({ port, onListen() {}, signal: abortController.signal }, () => {
return new Response(trueBody)
})
const networkBody = await cachingFetch(cache, `http://localhost:${port}`).then((res) => res.text())
asserts.assertEquals(networkBody, trueBody)
abortController.abort()
await server.finished
const cacheBody = await cachingFetch(cache, `http://localhost:${port}/`).then((res) => res.text())
asserts.assertEquals(cacheBody, trueBody)
})