nodejs/test/parallel/test-http-host-headers.js

76 lines
1.8 KiB
JavaScript
Raw Normal View History

'use strict';
const common = require('../common');
const http = require('http');
const assert = require('assert');
const httpServer = http.createServer(reqHandler);
function reqHandler(req, res) {
2011-08-01 21:22:30 -07:00
if (req.url === '/setHostFalse5') {
assert.strictEqual(req.headers.host, undefined);
2011-08-01 21:22:30 -07:00
} else {
assert.strictEqual(req.headers.host, `localhost:${this.address().port}`,
'Wrong host header for req[' + req.url + ']: ' +
2011-08-01 21:22:30 -07:00
req.headers.host);
}
res.writeHead(200, {});
res.end('ok');
}
testHttp();
function testHttp() {
let counter = 0;
2012-12-13 07:47:33 -08:00
function cb(res) {
counter--;
if (counter === 0) {
httpServer.close();
}
2012-12-13 07:47:33 -08:00
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();
});
}