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
|
import { assert, test } from '../run/test.setup.js'
import { polyfill } from '../mod.js'
test(() => {
return [
{
name: 'Fetch functionality',
test() {
const target = {}
polyfill(target)
assert.equal(Reflect.has(target, 'fetch'), true)
assert.equal(typeof target.fetch, 'function')
},
},
{
name: 'Fetch with https',
async test() {
const target = {}
polyfill(target)
const { fetch } = target
const response = await fetch('https://api.openbrewerydb.org/breweries')
assert.equal(response.constructor, target.Response)
const json = await response.json()
assert.equal(Array.isArray(json), true)
},
},
{
name: 'Fetch with file',
async test() {
const target = {}
polyfill(target)
const { fetch } = target
const url = new URL('../package.json', import.meta.url)
const response = await fetch(url)
assert.equal(response.constructor, target.Response)
assert.equal(response.status, 200)
assert.equal(response.statusText, '')
assert.equal(response.headers.has('date'), true)
assert.equal(response.headers.has('content-length'), true)
assert.equal(response.headers.has('last-modified'), true)
const json = await response.json()
assert.equal(json.name, '@astrojs/webapi')
},
},
{
name: 'Fetch with missing file',
async test() {
const target = {}
polyfill(target)
const { fetch } = target
const url = new URL('../missing.json', import.meta.url)
const response = await fetch(url)
assert.equal(response.constructor, target.Response)
assert.equal(response.status, 404)
assert.equal(response.statusText, '')
assert.equal(response.headers.has('date'), true)
assert.equal(response.headers.has('content-length'), false)
assert.equal(response.headers.has('last-modified'), false)
},
},
{
name: 'Fetch with (file) Request',
async test() {
const target = {}
polyfill(target)
const { Request, fetch } = target
const request = new Request(new URL('../package.json', import.meta.url))
const response = await fetch(request)
assert.equal(response.constructor, target.Response)
const json = await response.json()
assert.equal(json.name, '@astrojs/webapi')
},
},
{
name: 'Fetch with relative file',
async test() {
const target = {}
polyfill(target)
const { fetch } = target
const response = await fetch('package.json')
const json = await response.json()
assert.equal(json.name, '@astrojs/webapi')
},
},
{
name: 'Fetch with data',
async test() {
const target = {}
polyfill(target)
const { fetch } = target
const jsonURI = `data:application/json,${encodeURIComponent(JSON.stringify({
name: '@astrojs/webapi'
}))}`
const response = await fetch(jsonURI)
const json = await response.json()
assert.equal(json.name, '@astrojs/webapi')
},
},
]
})
|