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
|
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const h2 = require('http2');
// Http2ServerResponse should support checking and reading custom headers
const server = h2.createServer();
server.listen(0, common.mustCall(function() {
const port = server.address().port;
server.once('request', common.mustCall(function(request, response) {
const real = 'foo-bar';
const fake = 'bar-foo';
const denormalised = ` ${real.toUpperCase()}\n\t`;
const expectedValue = 'abc123';
response.setHeader(real, expectedValue);
assert.strictEqual(response.hasHeader(real), true);
assert.strictEqual(response.hasHeader(fake), false);
assert.strictEqual(response.hasHeader(denormalised), true);
assert.strictEqual(response.getHeader(real), expectedValue);
assert.strictEqual(response.getHeader(denormalised), expectedValue);
assert.strictEqual(response.getHeader(fake), undefined);
response.removeHeader(fake);
assert.strictEqual(response.hasHeader(fake), false);
response.setHeader(real, expectedValue);
assert.strictEqual(response.getHeader(real), expectedValue);
assert.strictEqual(response.hasHeader(real), true);
response.removeHeader(real);
assert.strictEqual(response.hasHeader(real), false);
response.setHeader(denormalised, expectedValue);
assert.strictEqual(response.getHeader(denormalised), expectedValue);
assert.strictEqual(response.hasHeader(denormalised), true);
response.removeHeader(denormalised);
assert.strictEqual(response.hasHeader(denormalised), false);
common.expectsError(
() => response.hasHeader(),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "name" argument must be of type string'
}
);
common.expectsError(
() => response.getHeader(),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "name" argument must be of type string'
}
);
common.expectsError(
() => response.removeHeader(),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "name" argument must be of type string'
}
);
[
':status',
':method',
':path',
':authority',
':scheme'
].forEach((header) => assert.throws(
() => response.setHeader(header, 'foobar'),
common.expectsError({
code: 'ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED',
type: Error,
message: 'Cannot set HTTP/2 pseudo-headers'
})
));
assert.throws(function() {
response.setHeader(real, null);
}, common.expectsError({
code: 'ERR_HTTP2_INVALID_HEADER_VALUE',
type: TypeError,
message: 'Value must not be undefined or null'
}));
assert.throws(function() {
response.setHeader(real, undefined);
}, common.expectsError({
code: 'ERR_HTTP2_INVALID_HEADER_VALUE',
type: TypeError,
message: 'Value must not be undefined or null'
}));
common.expectsError(
() => response.setHeader(), // header name undefined
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "name" argument must be of type string'
}
);
common.expectsError(
() => response.setHeader(''),
{
code: 'ERR_INVALID_HTTP_TOKEN',
type: TypeError,
message: 'Header name must be a valid HTTP token [""]'
}
);
response.setHeader(real, expectedValue);
const expectedHeaderNames = [real];
assert.deepStrictEqual(response.getHeaderNames(), expectedHeaderNames);
const expectedHeaders = { [real]: expectedValue };
assert.deepStrictEqual(response.getHeaders(), expectedHeaders);
response.getHeaders()[fake] = fake;
assert.strictEqual(response.hasHeader(fake), false);
assert.strictEqual(response.sendDate, true);
response.sendDate = false;
assert.strictEqual(response.sendDate, false);
response.on('finish', common.mustCall(function() {
assert.strictEqual(response.headersSent, true);
common.expectsError(
() => response.setHeader(real, expectedValue),
{
code: 'ERR_HTTP2_HEADERS_SENT',
type: Error,
message: 'Response has already been initiated.'
}
);
common.expectsError(
() => response.removeHeader(real, expectedValue),
{
code: 'ERR_HTTP2_HEADERS_SENT',
type: Error,
message: 'Response has already been initiated.'
}
);
process.nextTick(() => {
common.expectsError(
() => response.setHeader(real, expectedValue),
{
code: 'ERR_HTTP2_HEADERS_SENT',
type: Error,
message: 'Response has already been initiated.'
}
);
common.expectsError(
() => response.removeHeader(real, expectedValue),
{
code: 'ERR_HTTP2_HEADERS_SENT',
type: Error,
message: 'Response has already been initiated.'
}
);
assert.strictEqual(response.headersSent, true);
server.close();
});
}));
response.end();
}));
const url = `http://localhost:${port}`;
const client = h2.connect(url, common.mustCall(function() {
const headers = {
':path': '/',
':method': 'GET',
':scheme': 'http',
':authority': `localhost:${port}`
};
const request = client.request(headers);
request.on('end', common.mustCall(function() {
client.destroy();
}));
request.end();
request.resume();
}));
}));
|