nodejs/test/parallel/test-http-host-headers.js
cjihrig 7dd82dd1c3 test: add common.mustNotCall()
This commit adds a mustNotCall() helper for testing. This provides
an alternative to using common.fail() as a callback, or creating
a callback function for the sole purpose of calling common.fail().

PR-URL: https://github.com/nodejs/node/pull/11152
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
2017-02-06 14:07:55 -05: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.mustNotCall());
http.request({
method: 'GET',
path: '/' + (counter++),
host: 'localhost',
port: httpServer.address().port,
rejectUnauthorized: false
}, cb).on('error', common.mustNotCall()).end();
http.request({
method: 'POST',
path: '/' + (counter++),
host: 'localhost',
port: httpServer.address().port,
rejectUnauthorized: false
}, cb).on('error', common.mustNotCall()).end();
http.request({
method: 'PUT',
path: '/' + (counter++),
host: 'localhost',
port: httpServer.address().port,
rejectUnauthorized: false
}, cb).on('error', common.mustNotCall()).end();
http.request({
method: 'DELETE',
path: '/' + (counter++),
host: 'localhost',
port: httpServer.address().port,
rejectUnauthorized: false
}, cb).on('error', common.mustNotCall()).end();
});
}