Skip to content

Commit a373130

Browse files
himself65nodejs-github-bot
authored andcommitted
test: refactor test-https-host-headers
Use `common.mustCall` and `util.debuglog`. Remove unnecessary functions PR-URL: #32805 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent aaf225a commit a373130

File tree

1 file changed

+96
-109
lines changed

1 file changed

+96
-109
lines changed

test/parallel/test-https-host-headers.js

+96-109
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ if (!common.hasCrypto)
77

88
const assert = require('assert');
99
const https = require('https');
10+
const debug = require('util').debuglog('test');
1011

11-
const options = {
12-
key: fixtures.readKey('agent1-key.pem'),
13-
cert: fixtures.readKey('agent1-cert.pem')
14-
};
15-
const httpsServer = https.createServer(options, reqHandler);
12+
let counter = 0;
1613

17-
function reqHandler(req, res) {
18-
console.log(`Got request: ${req.headers.host} ${req.url}`);
14+
const httpsServer = https.createServer({
15+
key: fixtures.readKey('agent1-key.pem'),
16+
cert: fixtures.readKey('agent1-cert.pem'),
17+
}, common.mustCall(function(req, res) {
18+
debug(`Got request: ${req.headers.host} ${req.url}`);
1919
if (req.url.startsWith('/setHostFalse')) {
2020
assert.strictEqual(req.headers.host, undefined);
2121
} else {
@@ -25,106 +25,93 @@ function reqHandler(req, res) {
2525
}
2626
res.writeHead(200, {});
2727
res.end('ok');
28-
}
29-
30-
function thrower(er) {
31-
throw er;
32-
}
33-
34-
testHttps();
35-
36-
function testHttps() {
37-
38-
let counter = 0;
39-
40-
function cb(res) {
41-
counter--;
42-
console.log(`back from https request. counter = ${counter}`);
43-
if (counter === 0) {
44-
httpsServer.close();
45-
console.log('ok');
46-
}
47-
res.resume();
28+
}, 9)).listen(0, common.mustCall(function(err) {
29+
debug(`test https server listening on port ${this.address().port}`);
30+
assert.ifError(err);
31+
https.get({
32+
method: 'GET',
33+
path: `/${counter++}`,
34+
host: 'localhost',
35+
port: this.address().port,
36+
rejectUnauthorized: false,
37+
}, cb).on('error', common.mustNotCall());
38+
39+
https.request({
40+
method: 'GET',
41+
path: `/${counter++}`,
42+
host: 'localhost',
43+
port: this.address().port,
44+
rejectUnauthorized: false,
45+
}, cb).on('error', common.mustNotCall()).end();
46+
47+
https.request({
48+
method: 'POST',
49+
path: `/${counter++}`,
50+
host: 'localhost',
51+
port: this.address().port,
52+
rejectUnauthorized: false,
53+
}, cb).on('error', common.mustNotCall()).end();
54+
55+
https.request({
56+
method: 'PUT',
57+
path: `/${counter++}`,
58+
host: 'localhost',
59+
port: this.address().port,
60+
rejectUnauthorized: false,
61+
}, cb).on('error', common.mustNotCall()).end();
62+
63+
https.request({
64+
method: 'DELETE',
65+
path: `/${counter++}`,
66+
host: 'localhost',
67+
port: this.address().port,
68+
rejectUnauthorized: false,
69+
}, cb).on('error', common.mustNotCall()).end();
70+
71+
https.get({
72+
method: 'GET',
73+
path: `/setHostFalse${counter++}`,
74+
host: 'localhost',
75+
setHost: false,
76+
port: this.address().port,
77+
rejectUnauthorized: false,
78+
}, cb).on('error', common.mustNotCall());
79+
80+
https.request({
81+
method: 'GET',
82+
path: `/${counter++}`,
83+
host: 'localhost',
84+
setHost: true,
85+
// agent: false,
86+
port: this.address().port,
87+
rejectUnauthorized: false,
88+
}, cb).on('error', common.mustNotCall()).end();
89+
90+
https.get({
91+
method: 'GET',
92+
path: `/setHostFalse${counter++}`,
93+
host: 'localhost',
94+
setHost: 0,
95+
port: this.address().port,
96+
rejectUnauthorized: false,
97+
}, cb).on('error', common.mustNotCall());
98+
99+
https.get({
100+
method: 'GET',
101+
path: `/setHostFalse${counter++}`,
102+
host: 'localhost',
103+
setHost: null,
104+
port: this.address().port,
105+
rejectUnauthorized: false,
106+
}, cb).on('error', common.mustNotCall());
107+
}));
108+
109+
const cb = common.mustCall(function(res) {
110+
counter--;
111+
debug(`back from https request. counter = ${counter}`);
112+
if (counter === 0) {
113+
httpsServer.close();
114+
debug('ok');
48115
}
49-
50-
httpsServer.listen(0, function(er) {
51-
console.log(`test https server listening on port ${this.address().port}`);
52-
assert.ifError(er);
53-
https.get({
54-
method: 'GET',
55-
path: `/${counter++}`,
56-
host: 'localhost',
57-
port: this.address().port,
58-
rejectUnauthorized: false
59-
}, cb).on('error', thrower);
60-
61-
https.request({
62-
method: 'GET',
63-
path: `/${counter++}`,
64-
host: 'localhost',
65-
port: this.address().port,
66-
rejectUnauthorized: false
67-
}, cb).on('error', thrower).end();
68-
69-
https.request({
70-
method: 'POST',
71-
path: `/${counter++}`,
72-
host: 'localhost',
73-
port: this.address().port,
74-
rejectUnauthorized: false
75-
}, cb).on('error', thrower).end();
76-
77-
https.request({
78-
method: 'PUT',
79-
path: `/${counter++}`,
80-
host: 'localhost',
81-
port: this.address().port,
82-
rejectUnauthorized: false
83-
}, cb).on('error', thrower).end();
84-
85-
https.request({
86-
method: 'DELETE',
87-
path: `/${counter++}`,
88-
host: 'localhost',
89-
port: this.address().port,
90-
rejectUnauthorized: false
91-
}, cb).on('error', thrower).end();
92-
93-
https.get({
94-
method: 'GET',
95-
path: `/setHostFalse${counter++}`,
96-
host: 'localhost',
97-
setHost: false,
98-
port: this.address().port,
99-
rejectUnauthorized: false
100-
}, cb).on('error', thrower);
101-
102-
https.request({
103-
method: 'GET',
104-
path: `/${counter++}`,
105-
host: 'localhost',
106-
setHost: true,
107-
// agent: false,
108-
port: this.address().port,
109-
rejectUnauthorized: false
110-
}, cb).on('error', thrower).end();
111-
112-
https.get({
113-
method: 'GET',
114-
path: `/setHostFalse${counter++}`,
115-
host: 'localhost',
116-
setHost: 0,
117-
port: this.address().port,
118-
rejectUnauthorized: false
119-
}, cb).on('error', thrower);
120-
121-
https.get({
122-
method: 'GET',
123-
path: `/setHostFalse${counter++}`,
124-
host: 'localhost',
125-
setHost: null,
126-
port: this.address().port,
127-
rejectUnauthorized: false
128-
}, cb).on('error', thrower);
129-
});
130-
}
116+
res.resume();
117+
}, 9);

0 commit comments

Comments
 (0)