forked from web-std/io
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathheaders.js
380 lines (317 loc) · 9.7 KB
/
headers.js
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import util from 'util';
import {Headers} from '@remix-run/web-fetch';
import chai from 'chai';
import chaiIterator from 'chai-iterator';
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 = [];
headers.forEach((value, key) => {
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]);
});
it('should allow iterating through multiple set-cookie headers with forEach', () => {
let headers = new Headers([
['a', '1'],
['Set-Cookie', 'b=2']
]);
headers.append('Set-Cookie', 'c=3');
expect(headers.entries()).to.be.iterable;
const results = [];
headers.forEach((value, key, object) => {
results.push({value, key, object});
});
expect(results).to.deep.equal([
{ value: '1', key: 'a', object: headers },
{ value: 'b=2', key: 'set-cookie', object: headers },
{ value: 'c=3', key: 'set-cookie', object: headers },
]);
})
it('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 multiple set-cookie headers with for-of loop', () => {
let headers = new Headers([
['a', '1'],
['Set-Cookie', 'b=2']
]);
headers.append('Set-Cookie', 'c=3');
expect(headers.entries()).to.be.iterable;
const result = [];
for (const pair of headers) {
result.push(pair);
}
expect(result).to.deep.equal([['a', '1'], ['set-cookie', 'b=2'], ['set-cookie', 'c=3']]);
})
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 multiple set-cookie headers with entries()', ()=> {
let headers = new Headers([
['a', '1'],
['Set-Cookie', 'b=2']
]);
headers.append('Set-Cookie', 'c=3');
expect(headers.entries()).to.be.iterable
.and.to.deep.iterate.over([['a', '1'], ['set-cookie', 'b=2'], ['set-cookie', 'c=3']]);
})
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 allow iterating through multiple set-cookie headers with values()', ()=> {
let headers = new Headers([
['a', '1'],
['Set-Cookie', 'b=2']
]);
headers.append('Set-Cookie', 'c=3');
expect(headers.values()).to.be.iterable
.and.to.iterate.over(['1', 'b=2', 'c=3']);
})
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);
});
it('should allow HTTP2 pseudo-headers', () => {
let headers = new Headers({':authority': 'something'});
headers.append(":method", "something else")
const result = [];
for (const pair of headers) {
result.push(pair);
}
expect(result).to.deep.equal([[':authority', 'something'], [':method', 'something else']]);
})
it('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;
});
it('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);
});
it('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(util.format(headers)).to.equal("{ a: [ '1', '3' ], b: '2', host: 'thehost' }");
});
it('should have the correct prototype chain', () => {
const headers = new Headers();
expect(headers).to.be.instanceOf(Headers);
expect(Object.getPrototypeOf(headers)).to.equal(Headers.prototype);
});
it('should have the correct prototype chain when extended', () => {
class MyHeaders extends Headers {}
const headers = new MyHeaders();
expect(headers).to.be.instanceOf(MyHeaders);
expect(headers).to.be.instanceOf(Headers);
expect(Object.getPrototypeOf(headers)).to.equal(MyHeaders.prototype);
});
it('should call the method of the subclass', () => {
class MyHeaders extends Headers {
append(_name, _value) {
return 'subclass method called';
}
}
const headers = new MyHeaders();
const result = headers.append('Content-Type', 'application/json');
expect(result).to.equal('subclass method called');
});
});