-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Expand file tree
/
Copy pathtest-http-write-information.js
More file actions
94 lines (82 loc) Β· 2.92 KB
/
test-http-write-information.js
File metadata and controls
94 lines (82 loc) Β· 2.92 KB
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
'use strict';
const common = require('../common');
const assert = require('node:assert');
const http = require('node:http');
// Happy flow: arbitrary 1xx status with custom headers, observed by the
// client's 'information' event.
{
const server = http.createServer(common.mustCall((req, res) => {
res.writeInformation(110, { 'X-Progress': '50%', 'X-Stage': 'reading' });
res.writeInformation(199, [['X-Custom', 'one'], ['X-Custom-2', 'two']]);
res.end('done');
}));
server.listen(0, common.mustCall(() => {
const req = http.request({ port: server.address().port });
const seen = [];
req.on('information', (res) => {
seen.push({
statusCode: res.statusCode,
headers: res.headers,
});
});
req.on('response', common.mustCall((res) => {
assert.strictEqual(res.statusCode, 200);
assert.strictEqual(seen.length, 2);
assert.strictEqual(seen[0].statusCode, 110);
assert.strictEqual(seen[0].headers['x-progress'], '50%');
assert.strictEqual(seen[0].headers['x-stage'], 'reading');
assert.strictEqual(seen[1].statusCode, 199);
assert.strictEqual(seen[1].headers['x-custom'], 'one');
assert.strictEqual(seen[1].headers['x-custom-2'], 'two');
res.resume();
res.on('end', common.mustCall(() => server.close()));
}));
req.end();
}));
}
// Headers argument is optional / nullable.
{
const server = http.createServer(common.mustCall((req, res) => {
res.writeInformation(150);
res.writeInformation(151, null);
res.end();
}));
server.listen(0, common.mustCall(() => {
const req = http.request({ port: server.address().port });
let count = 0;
req.on('information', () => count++);
req.on('response', common.mustCall((res) => {
assert.strictEqual(count, 2);
res.resume();
res.on('end', common.mustCall(() => server.close()));
}));
req.end();
}));
}
// Error cases.
{
const server = http.createServer(common.mustCall((req, res) => {
assert.throws(() => res.writeInformation(101),
{ code: 'ERR_HTTP_INVALID_STATUS_CODE' });
assert.throws(() => res.writeInformation(99),
{ code: 'ERR_OUT_OF_RANGE' });
assert.throws(() => res.writeInformation(200),
{ code: 'ERR_OUT_OF_RANGE' });
assert.throws(() => res.writeInformation('100'),
{ code: 'ERR_INVALID_ARG_TYPE' });
assert.throws(() => res.writeInformation(150, { 'X-Bad\n': 'v' }),
{ code: 'ERR_INVALID_HTTP_TOKEN' });
res.writeHead(200);
assert.throws(() => res.writeInformation(150),
{ code: 'ERR_HTTP_HEADERS_SENT' });
res.end();
}));
server.listen(0, common.mustCall(() => {
const req = http.request({ port: server.address().port });
req.on('response', common.mustCall((res) => {
res.resume();
res.on('end', common.mustCall(() => server.close()));
}));
req.end();
}));
}