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
|
'use strict'
const { test } = require('tap')
const {
URLSerializer,
collectASequenceOfCodePoints,
stringPercentDecode,
parseMIMEType,
collectAnHTTPQuotedString
} = require('../../lib/fetch/dataURL')
const { fetch } = require('../..')
test('https://url.spec.whatwg.org/#concept-url-serializer', (t) => {
t.test('url scheme gets appended', (t) => {
const url = new URL('https://www.google.com/')
const serialized = URLSerializer(url)
t.ok(serialized.startsWith(url.protocol))
t.end()
})
t.test('non-null url host with authentication', (t) => {
const url = new URL('https://username:password@google.com')
const serialized = URLSerializer(url)
t.ok(serialized.includes(`//${url.username}:${url.password}`))
t.ok(serialized.endsWith('@google.com/'))
t.end()
})
t.test('null url host', (t) => {
for (const url of ['web+demo:/.//not-a-host/', 'web+demo:/path/..//not-a-host/']) {
t.equal(
URLSerializer(new URL(url)),
'web+demo:/.//not-a-host/'
)
}
t.end()
})
t.test('url with query works', (t) => {
t.equal(
URLSerializer(new URL('https://www.google.com/?fetch=undici')),
'https://www.google.com/?fetch=undici'
)
t.end()
})
t.test('exclude fragment', (t) => {
t.equal(
URLSerializer(new URL('https://www.google.com/#frag')),
'https://www.google.com/#frag'
)
t.equal(
URLSerializer(new URL('https://www.google.com/#frag'), true),
'https://www.google.com/'
)
t.end()
})
t.end()
})
test('https://infra.spec.whatwg.org/#collect-a-sequence-of-code-points', (t) => {
const input = 'text/plain;base64,'
const position = { position: 0 }
const result = collectASequenceOfCodePoints(
(char) => char !== ';',
input,
position
)
t.strictSame(result, 'text/plain')
t.strictSame(position.position, input.indexOf(';'))
t.end()
})
test('https://url.spec.whatwg.org/#string-percent-decode', (t) => {
t.test('encodes %{2} in range properly', (t) => {
const input = '%FF'
const percentDecoded = stringPercentDecode(input)
t.same(percentDecoded, new Uint8Array([255]))
t.end()
})
t.test('encodes %{2} not in range properly', (t) => {
const input = 'Hello %XD World'
const percentDecoded = stringPercentDecode(input)
const expected = [...input].map(c => c.charCodeAt(0))
t.same(percentDecoded, expected)
t.end()
})
t.test('normal string works', (t) => {
const input = 'Hello world'
const percentDecoded = stringPercentDecode(input)
const expected = [...input].map(c => c.charCodeAt(0))
t.same(percentDecoded, Uint8Array.from(expected))
t.end()
})
t.end()
})
test('https://mimesniff.spec.whatwg.org/#parse-a-mime-type', (t) => {
t.same(parseMIMEType('text/plain'), {
type: 'text',
subtype: 'plain',
parameters: new Map(),
essence: 'text/plain'
})
t.same(parseMIMEType('text/html;charset="shift_jis"iso-2022-jp'), {
type: 'text',
subtype: 'html',
parameters: new Map([['charset', 'shift_jis']]),
essence: 'text/html'
})
t.same(parseMIMEType('application/javascript'), {
type: 'application',
subtype: 'javascript',
parameters: new Map(),
essence: 'application/javascript'
})
t.end()
})
test('https://fetch.spec.whatwg.org/#collect-an-http-quoted-string', (t) => {
// https://fetch.spec.whatwg.org/#example-http-quoted-string
t.test('first', (t) => {
const position = { position: 0 }
t.strictSame(collectAnHTTPQuotedString('"\\', {
position: 0
}), '"\\')
t.strictSame(collectAnHTTPQuotedString('"\\', position, true), '\\')
t.strictSame(position.position, 2)
t.end()
})
t.test('second', (t) => {
const position = { position: 0 }
const input = '"Hello" World'
t.strictSame(collectAnHTTPQuotedString(input, {
position: 0
}), '"Hello"')
t.strictSame(collectAnHTTPQuotedString(input, position, true), 'Hello')
t.strictSame(position.position, 7)
t.end()
})
t.end()
})
// https://github.com/nodejs/undici/issues/1574
test('too long base64 url', async (t) => {
const inputStr = 'a'.repeat(1 << 20)
const base64 = Buffer.from(inputStr).toString('base64')
const dataURIPrefix = 'data:application/octet-stream;base64,'
const dataURL = dataURIPrefix + base64
try {
const res = await fetch(dataURL)
const buf = await res.arrayBuffer()
const outputStr = Buffer.from(buf).toString('ascii')
t.same(outputStr, inputStr)
} catch (e) {
t.fail(`failed to fetch ${dataURL}`)
}
})
test('https://domain.com/#', (t) => {
t.plan(1)
const domain = 'https://domain.com/#a'
const serialized = URLSerializer(new URL(domain))
t.equal(serialized, domain)
})
test('https://domain.com/?', (t) => {
t.plan(1)
const domain = 'https://domain.com/?a=b'
const serialized = URLSerializer(new URL(domain))
t.equal(serialized, domain)
})
// https://github.com/nodejs/undici/issues/2474
test('hash url', (t) => {
t.plan(1)
const domain = 'https://domain.com/#a#b'
const url = new URL(domain)
const serialized = URLSerializer(url, true)
t.equal(serialized, url.href.substring(0, url.href.length - url.hash.length))
})
// https://github.com/nodejs/undici/issues/2474
test('data url that includes the hash', async (t) => {
t.plan(1)
const dataURL = 'data:,node#js#'
try {
const res = await fetch(dataURL)
t.equal(await res.text(), 'node')
} catch (error) {
t.fail(`failed to fetch ${dataURL}`)
}
})
|