-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-http-response-multiheaders.js
77 lines (72 loc) · 2.07 KB
/
test-http-response-multiheaders.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
'use strict';
const common = require('../common');
const http2 = require('http2');
const assert = require('assert');
const Countdown = require('../common/countdown');
// Test that certain response header fields do not repeat.
// 'content-length' should also be in this list but it is
// handled differently because multiple content-lengths are
// an error (see test-http-response-multi-content-length.js).
const norepeat = [
'content-type',
'user-agent',
'referer',
'host',
'authorization',
'proxy-authorization',
'if-modified-since',
'if-unmodified-since',
'from',
'location',
'max-forwards',
'retry-after',
'etag',
'last-modified',
'server',
'age',
'expires'
];
const runCount = 2;
const server = http2.createServer(function(req, res) {
const num = req.headers['x-num'];
if (num === '1') {
for (const name of norepeat) {
res.setHeader(name, ['A', 'B']);
}
res.setHeader('X-A', ['A', 'B']);
} else if (num === '2') {
const headers = {};
for (const name of norepeat) {
headers[name] = ['A', 'B'];
}
headers['X-A'] = ['A', 'B'];
res.writeHead(200, headers);
}
res.end('ok');
});
server.listen(0, common.mustCall(function() {
const countdown = new Countdown(runCount, () => {
client.close();
server.close();
});
const client = http2.connect('http://localhost:' + this.address().port);
for (let n = 1; n <= runCount; n++) {
// This runs twice, the first time, the server will use
// setHeader, the second time it uses writeHead. The
// result on the client side should be the same in
// either case -- only the first instance of the header
// value should be reported for the header fields listed
// in the norepeat array.
const req = client.request({ 'x-num': n });
req.end();
req.on('response', common.mustCall((res) => {
countdown.dec();
for (const name of norepeat) {
assert.strictEqual(res.headers[name], 'A');
}
assert.strictEqual(res.headers['x-a'], 'A, B');
req.resume();
req.on('end', () => req.close());
}));
}
}));