Skip to content

Commit e6b6ce4

Browse files
cjihrigitaloacasas
authored andcommitted
test: refactor test-http-mutable-headers.js
PR-URL: #10664 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 8262d49 commit e6b6ce4

File tree

1 file changed

+45
-52
lines changed

1 file changed

+45
-52
lines changed
+45-52
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44
const http = require('http');
55

@@ -11,55 +11,63 @@ const http = require('http');
1111
// <ClientRequest>.method
1212
// <ClientRequest>.path
1313

14-
var testsComplete = 0;
15-
var test = 'headers';
16-
var content = 'hello world\n';
17-
var cookies = [
14+
let test = 'headers';
15+
const content = 'hello world\n';
16+
const cookies = [
1817
'session_token=; path=/; expires=Sun, 15-Sep-2030 13:48:52 GMT',
1918
'prefers_open_id=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT'
2019
];
2120

22-
var s = http.createServer(function(req, res) {
21+
const s = http.createServer(common.mustCall((req, res) => {
2322
switch (test) {
2423
case 'headers':
25-
assert.throws(function() { res.setHeader(); });
26-
assert.throws(function() { res.setHeader('someHeader'); });
27-
assert.throws(function() { res.getHeader(); });
28-
assert.throws(function() { res.removeHeader(); });
24+
assert.throws(() => {
25+
res.setHeader();
26+
}, /^TypeError: Header name must be a valid HTTP Token \["undefined"\]$/);
27+
assert.throws(() => {
28+
res.setHeader('someHeader');
29+
}, /^Error: "value" required in setHeader\("someHeader", value\)$/);
30+
assert.throws(() => {
31+
res.getHeader();
32+
}, /^Error: "name" argument is required for getHeader\(name\)$/);
33+
assert.throws(() => {
34+
res.removeHeader();
35+
}, /^Error: "name" argument is required for removeHeader\(name\)$/);
2936

3037
res.setHeader('x-test-header', 'testing');
3138
res.setHeader('X-TEST-HEADER2', 'testing');
3239
res.setHeader('set-cookie', cookies);
3340
res.setHeader('x-test-array-header', [1, 2, 3]);
3441

35-
var val1 = res.getHeader('x-test-header');
36-
var val2 = res.getHeader('x-test-header2');
37-
assert.equal(val1, 'testing');
38-
assert.equal(val2, 'testing');
42+
assert.strictEqual(res.getHeader('x-test-header'), 'testing');
43+
assert.strictEqual(res.getHeader('x-test-header2'), 'testing');
3944

4045
res.removeHeader('x-test-header2');
4146
break;
4247

4348
case 'contentLength':
4449
res.setHeader('content-length', content.length);
45-
assert.equal(content.length, res.getHeader('Content-Length'));
50+
assert.strictEqual(res.getHeader('Content-Length'), content.length);
4651
break;
4752

4853
case 'transferEncoding':
4954
res.setHeader('transfer-encoding', 'chunked');
50-
assert.equal(res.getHeader('Transfer-Encoding'), 'chunked');
55+
assert.strictEqual(res.getHeader('Transfer-Encoding'), 'chunked');
5156
break;
5257

5358
case 'writeHead':
5459
res.statusCode = 404;
5560
res.setHeader('x-foo', 'keyboard cat');
5661
res.writeHead(200, { 'x-foo': 'bar', 'x-bar': 'baz' });
5762
break;
63+
64+
default:
65+
common.fail('Unknown test');
5866
}
5967

6068
res.statusCode = 201;
6169
res.end(content);
62-
});
70+
}, 4));
6371

6472
s.listen(0, nextTest);
6573

@@ -69,64 +77,49 @@ function nextTest() {
6977
return s.close();
7078
}
7179

72-
var bufferedResponse = '';
73-
74-
http.get({ port: s.address().port }, function(response) {
75-
console.log('TEST: ' + test);
76-
console.log('STATUS: ' + response.statusCode);
77-
console.log('HEADERS: ');
78-
console.dir(response.headers);
80+
let bufferedResponse = '';
7981

82+
http.get({ port: s.address().port }, common.mustCall((response) => {
8083
switch (test) {
8184
case 'headers':
82-
assert.equal(response.statusCode, 201);
83-
assert.equal(response.headers['x-test-header'],
84-
'testing');
85-
assert.equal(response.headers['x-test-array-header'],
86-
[1, 2, 3].join(', '));
87-
assert.deepStrictEqual(cookies,
88-
response.headers['set-cookie']);
89-
assert.equal(response.headers['x-test-header2'] !== undefined, false);
90-
// Make the next request
85+
assert.strictEqual(response.statusCode, 201);
86+
assert.strictEqual(response.headers['x-test-header'], 'testing');
87+
assert.strictEqual(response.headers['x-test-array-header'],
88+
[1, 2, 3].join(', '));
89+
assert.deepStrictEqual(cookies, response.headers['set-cookie']);
90+
assert.strictEqual(response.headers['x-test-header2'], undefined);
9191
test = 'contentLength';
92-
console.log('foobar');
9392
break;
9493

9594
case 'contentLength':
96-
assert.equal(response.headers['content-length'], content.length);
95+
assert.strictEqual(+response.headers['content-length'], content.length);
9796
test = 'transferEncoding';
9897
break;
9998

10099
case 'transferEncoding':
101-
assert.equal(response.headers['transfer-encoding'], 'chunked');
100+
assert.strictEqual(response.headers['transfer-encoding'], 'chunked');
102101
test = 'writeHead';
103102
break;
104103

105104
case 'writeHead':
106-
assert.equal(response.headers['x-foo'], 'bar');
107-
assert.equal(response.headers['x-bar'], 'baz');
108-
assert.equal(200, response.statusCode);
105+
assert.strictEqual(response.headers['x-foo'], 'bar');
106+
assert.strictEqual(response.headers['x-bar'], 'baz');
107+
assert.strictEqual(response.statusCode, 200);
109108
test = 'end';
110109
break;
111110

112111
default:
113-
throw new Error('?');
112+
common.fail('Unknown test');
114113
}
115114

116115
response.setEncoding('utf8');
117-
response.on('data', function(s) {
116+
response.on('data', (s) => {
118117
bufferedResponse += s;
119118
});
120119

121-
response.on('end', function() {
122-
assert.equal(content, bufferedResponse);
123-
testsComplete++;
124-
nextTest();
125-
});
126-
});
120+
response.on('end', common.mustCall(() => {
121+
assert.strictEqual(bufferedResponse, content);
122+
common.mustCall(nextTest)();
123+
}));
124+
}));
127125
}
128-
129-
130-
process.on('exit', function() {
131-
assert.equal(4, testsComplete);
132-
});

0 commit comments

Comments
 (0)