node/test/parallel/test-http-host-headers.js
Adrian Estrada f4fd073f0f test: improve code in test-http-host-headers
* use common.fail to handle errors
* remove console.log
* use arrow functions

PR-URL: https://github.com/nodejs/node/pull/10830
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-01-16 11:54:26 -08:00

76 lines
1.8 KiB
JavaScript

'use strict';
const common = require('../common');
const http = require('http');
const assert = require('assert');
const httpServer = http.createServer(reqHandler);
function reqHandler(req, res) {
if (req.url === '/setHostFalse5') {
assert.strictEqual(req.headers.host, undefined);
} else {
assert.strictEqual(req.headers.host, `localhost:${this.address().port}`,
'Wrong host header for req[' + req.url + ']: ' +
req.headers.host);
}
res.writeHead(200, {});
res.end('ok');
}
testHttp();
function testHttp() {
let counter = 0;
function cb(res) {
counter--;
if (counter === 0) {
httpServer.close();
}
res.resume();
}
httpServer.listen(0, (er) => {
assert.ifError(er);
http.get({
method: 'GET',
path: '/' + (counter++),
host: 'localhost',
port: httpServer.address().port,
rejectUnauthorized: false
}, cb).on('error', common.fail);
http.request({
method: 'GET',
path: '/' + (counter++),
host: 'localhost',
port: httpServer.address().port,
rejectUnauthorized: false
}, cb).on('error', common.fail).end();
http.request({
method: 'POST',
path: '/' + (counter++),
host: 'localhost',
port: httpServer.address().port,
rejectUnauthorized: false
}, cb).on('error', common.fail).end();
http.request({
method: 'PUT',
path: '/' + (counter++),
host: 'localhost',
port: httpServer.address().port,
rejectUnauthorized: false
}, cb).on('error', common.fail).end();
http.request({
method: 'DELETE',
path: '/' + (counter++),
host: 'localhost',
port: httpServer.address().port,
rejectUnauthorized: false
}, cb).on('error', common.fail).end();
});
}