Skip to content

Commit 76f0556

Browse files
edsadrMylesBorins
authored andcommitted
test: improve code in test-http-host-headers
* use common.fail to handle errors * remove console.log * use arrow functions PR-URL: #10830 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent c740cb6 commit 76f0556

File tree

1 file changed

+13
-28
lines changed

1 file changed

+13
-28
lines changed
Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const http = require('http');
44
const assert = require('assert');
55
const httpServer = http.createServer(reqHandler);
66

77
function reqHandler(req, res) {
8-
console.log('Got request: ' + req.headers.host + ' ' + req.url);
98
if (req.url === '/setHostFalse5') {
109
assert.equal(req.headers.host, undefined);
1110
} else {
@@ -14,14 +13,9 @@ function reqHandler(req, res) {
1413
req.headers.host);
1514
}
1615
res.writeHead(200, {});
17-
//process.nextTick(function() { res.end('ok'); });
1816
res.end('ok');
1917
}
2018

21-
function thrower(er) {
22-
throw er;
23-
}
24-
2519
testHttp();
2620

2721
function testHttp() {
@@ -30,61 +24,52 @@ function testHttp() {
3024

3125
function cb(res) {
3226
counter--;
33-
console.log('back from http request. counter = ' + counter);
3427
if (counter === 0) {
3528
httpServer.close();
3629
}
3730
res.resume();
3831
}
3932

40-
httpServer.listen(0, function(er) {
41-
console.error(`test http server listening on ${this.address().port}`);
42-
43-
if (er) throw er;
44-
33+
httpServer.listen(0, (er) => {
34+
assert.ifError(er);
4535
http.get({
4636
method: 'GET',
4737
path: '/' + (counter++),
4838
host: 'localhost',
49-
//agent: false,
50-
port: this.address().port,
39+
port: httpServer.address().port,
5140
rejectUnauthorized: false
52-
}, cb).on('error', thrower);
41+
}, cb).on('error', common.fail);
5342

5443
http.request({
5544
method: 'GET',
5645
path: '/' + (counter++),
5746
host: 'localhost',
58-
//agent: false,
59-
port: this.address().port,
47+
port: httpServer.address().port,
6048
rejectUnauthorized: false
61-
}, cb).on('error', thrower).end();
49+
}, cb).on('error', common.fail).end();
6250

6351
http.request({
6452
method: 'POST',
6553
path: '/' + (counter++),
6654
host: 'localhost',
67-
//agent: false,
68-
port: this.address().port,
55+
port: httpServer.address().port,
6956
rejectUnauthorized: false
70-
}, cb).on('error', thrower).end();
57+
}, cb).on('error', common.fail).end();
7158

7259
http.request({
7360
method: 'PUT',
7461
path: '/' + (counter++),
7562
host: 'localhost',
76-
//agent: false,
77-
port: this.address().port,
63+
port: httpServer.address().port,
7864
rejectUnauthorized: false
79-
}, cb).on('error', thrower).end();
65+
}, cb).on('error', common.fail).end();
8066

8167
http.request({
8268
method: 'DELETE',
8369
path: '/' + (counter++),
8470
host: 'localhost',
85-
//agent: false,
86-
port: this.address().port,
71+
port: httpServer.address().port,
8772
rejectUnauthorized: false
88-
}, cb).on('error', thrower).end();
73+
}, cb).on('error', common.fail).end();
8974
});
9075
}

0 commit comments

Comments
 (0)