-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-http-response-multi-content-length.js
51 lines (46 loc) · 1.5 KB
/
test-http-response-multi-content-length.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
'use strict';
const common = require('../common');
const http2 = require('http2');
const assert = require('assert');
const Countdown = require('../common/countdown');
const MAX_COUNT = 2;
const server = http2.createServer((req, res) => {
const num = req.headers['x-num'];
// TODO(@jasnell) At some point this should be refactored as the API
// should not be allowing users to set multiple content-length values
// in the first place.
switch (num) {
case '1':
res.setHeader('content-length', [2, 1]);
break;
case '2':
res.writeHead(200, { 'content-length': [1, 2] });
break;
default:
assert.fail('should never get here');
}
res.end('ok');
});
const countdown = new Countdown(MAX_COUNT, () => {
client.close();
server.close();
});
let client;
server.listen(0, common.mustCall(() => {
client = http2.connect('http://localhost:' + server.address().port);
for (let n = 1; n <= MAX_COUNT; n++) {
// This runs twice, the first time, the server will use
// setHeader, the second time it uses writeHead. In either
// case, the error handler must be called because the client
// is not allowed to accept multiple content-length headers.
const req = client.request({ 'x-num': n });
req.end();
req.on('error', common.mustCall((err) => {
assert(/^Parse Error/.test(err.message));
assert.strictEqual(err.code, 'HPE_UNEXPECTED_CONTENT_LENGTH');
req.close();
countdown.dec();
}));
req.on('end', common.mustNotCall());
}
}));