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
|
/* eslint no-unused-expressions: "off" */
const { format } = require('util')
const chai = require('chai')
const chaiIterator = require('chai-iterator')
const { Headers } = require('../../lib/fetch/headers.js')
chai.use(chaiIterator)
const { expect } = chai
describe('Headers', () => {
it('should have attributes conforming to Web IDL', () => {
const headers = new Headers()
expect(Object.getOwnPropertyNames(headers)).to.be.empty
const enumerableProperties = []
for (const property in headers) {
enumerableProperties.push(property)
}
for (const toCheck of [
'append',
'delete',
'entries',
'forEach',
'get',
'has',
'keys',
'set',
'values'
]) {
expect(enumerableProperties).to.contain(toCheck)
}
})
it('should allow iterating through all headers with forEach', () => {
const headers = new Headers([
['b', '2'],
['c', '4'],
['b', '3'],
['a', '1']
])
expect(headers).to.have.property('forEach')
const result = []
for (const [key, value] of headers.entries()) {
result.push([key, value])
}
expect(result).to.deep.equal([
['a', '1'],
['b', '2, 3'],
['c', '4']
])
})
it('should be iterable with forEach', () => {
const headers = new Headers()
headers.append('Accept', 'application/json')
headers.append('Accept', 'text/plain')
headers.append('Content-Type', 'text/html')
const results = []
headers.forEach((value, key, object) => {
results.push({ value, key, object })
})
expect(results.length).to.equal(2)
expect({ key: 'accept', value: 'application/json, text/plain', object: headers }).to.deep.equal(results[0])
expect({ key: 'content-type', value: 'text/html', object: headers }).to.deep.equal(results[1])
})
xit('should set "this" to undefined by default on forEach', () => {
const headers = new Headers({ Accept: 'application/json' })
headers.forEach(function () {
expect(this).to.be.undefined
})
})
it('should accept thisArg as a second argument for forEach', () => {
const headers = new Headers({ Accept: 'application/json' })
const thisArg = {}
headers.forEach(function () {
expect(this).to.equal(thisArg)
}, thisArg)
})
it('should allow iterating through all headers with for-of loop', () => {
const headers = new Headers([
['b', '2'],
['c', '4'],
['a', '1']
])
headers.append('b', '3')
expect(headers).to.be.iterable
const result = []
for (const pair of headers) {
result.push(pair)
}
expect(result).to.deep.equal([
['a', '1'],
['b', '2, 3'],
['c', '4']
])
})
it('should allow iterating through all headers with entries()', () => {
const headers = new Headers([
['b', '2'],
['c', '4'],
['a', '1']
])
headers.append('b', '3')
expect(headers.entries()).to.be.iterable
.and.to.deep.iterate.over([
['a', '1'],
['b', '2, 3'],
['c', '4']
])
})
it('should allow iterating through all headers with keys()', () => {
const headers = new Headers([
['b', '2'],
['c', '4'],
['a', '1']
])
headers.append('b', '3')
expect(headers.keys()).to.be.iterable
.and.to.iterate.over(['a', 'b', 'c'])
})
it('should allow iterating through all headers with values()', () => {
const headers = new Headers([
['b', '2'],
['c', '4'],
['a', '1']
])
headers.append('b', '3')
expect(headers.values()).to.be.iterable
.and.to.iterate.over(['1', '2, 3', '4'])
})
it('should reject illegal header', () => {
const headers = new Headers()
expect(() => new Headers({ 'He y': 'ok' })).to.throw(TypeError)
expect(() => new Headers({ 'Hé-y': 'ok' })).to.throw(TypeError)
expect(() => new Headers({ 'He-y': 'ăk' })).to.throw(TypeError)
expect(() => headers.append('Hé-y', 'ok')).to.throw(TypeError)
expect(() => headers.delete('Hé-y')).to.throw(TypeError)
expect(() => headers.get('Hé-y')).to.throw(TypeError)
expect(() => headers.has('Hé-y')).to.throw(TypeError)
expect(() => headers.set('Hé-y', 'ok')).to.throw(TypeError)
// Should reject empty header
expect(() => headers.append('', 'ok')).to.throw(TypeError)
})
xit('should ignore unsupported attributes while reading headers', () => {
const FakeHeader = function () {}
// Prototypes are currently ignored
// This might change in the future: #181
FakeHeader.prototype.z = 'fake'
const res = new FakeHeader()
res.a = 'string'
res.b = ['1', '2']
res.c = ''
res.d = []
res.e = 1
res.f = [1, 2]
res.g = { a: 1 }
res.h = undefined
res.i = null
res.j = Number.NaN
res.k = true
res.l = false
res.m = Buffer.from('test')
const h1 = new Headers(res)
h1.set('n', [1, 2])
h1.append('n', ['3', 4])
const h1Raw = h1.raw()
expect(h1Raw.a).to.include('string')
expect(h1Raw.b).to.include('1,2')
expect(h1Raw.c).to.include('')
expect(h1Raw.d).to.include('')
expect(h1Raw.e).to.include('1')
expect(h1Raw.f).to.include('1,2')
expect(h1Raw.g).to.include('[object Object]')
expect(h1Raw.h).to.include('undefined')
expect(h1Raw.i).to.include('null')
expect(h1Raw.j).to.include('NaN')
expect(h1Raw.k).to.include('true')
expect(h1Raw.l).to.include('false')
expect(h1Raw.m).to.include('test')
expect(h1Raw.n).to.include('1,2')
expect(h1Raw.n).to.include('3,4')
expect(h1Raw.z).to.be.undefined
})
xit('should wrap headers', () => {
const h1 = new Headers({
a: '1'
})
const h1Raw = h1.raw()
const h2 = new Headers(h1)
h2.set('b', '1')
const h2Raw = h2.raw()
const h3 = new Headers(h2)
h3.append('a', '2')
const h3Raw = h3.raw()
expect(h1Raw.a).to.include('1')
expect(h1Raw.a).to.not.include('2')
expect(h2Raw.a).to.include('1')
expect(h2Raw.a).to.not.include('2')
expect(h2Raw.b).to.include('1')
expect(h3Raw.a).to.include('1')
expect(h3Raw.a).to.include('2')
expect(h3Raw.b).to.include('1')
})
it('should accept headers as an iterable of tuples', () => {
let headers
headers = new Headers([
['a', '1'],
['b', '2'],
['a', '3']
])
expect(headers.get('a')).to.equal('1, 3')
expect(headers.get('b')).to.equal('2')
headers = new Headers([
new Set(['a', '1']),
['b', '2'],
new Map([['a', null], ['3', null]]).keys()
])
expect(headers.get('a')).to.equal('1, 3')
expect(headers.get('b')).to.equal('2')
headers = new Headers(new Map([
['a', '1'],
['b', '2']
]))
expect(headers.get('a')).to.equal('1')
expect(headers.get('b')).to.equal('2')
})
it('should throw a TypeError if non-tuple exists in a headers initializer', () => {
expect(() => new Headers([['b', '2', 'huh?']])).to.throw(TypeError)
expect(() => new Headers(['b2'])).to.throw(TypeError)
expect(() => new Headers('b2')).to.throw(TypeError)
expect(() => new Headers({ [Symbol.iterator]: 42 })).to.throw(TypeError)
})
xit('should use a custom inspect function', () => {
const headers = new Headers([
['Host', 'thehost'],
['Host', 'notthehost'],
['a', '1'],
['b', '2'],
['a', '3']
])
// eslint-disable-next-line quotes
expect(format(headers)).to.equal("{ a: [ '1', '3' ], b: '2', host: 'thehost' }")
})
})
|