2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2016-05-29 03:06:56 -04:00
|
|
|
require('../common');
|
2016-01-13 21:42:45 +01:00
|
|
|
const http = require('http');
|
|
|
|
const assert = require('assert');
|
|
|
|
const httpServer = http.createServer(reqHandler);
|
2015-03-04 12:11:21 +11:00
|
|
|
|
2011-02-23 14:46:35 -08:00
|
|
|
function reqHandler(req, res) {
|
|
|
|
console.log('Got request: ' + req.headers.host + ' ' + req.url);
|
2011-08-01 21:22:30 -07:00
|
|
|
if (req.url === '/setHostFalse5') {
|
|
|
|
assert.equal(req.headers.host, undefined);
|
|
|
|
} else {
|
2016-05-29 03:06:56 -04:00
|
|
|
assert.equal(req.headers.host, `localhost:${this.address().port}`,
|
2011-08-01 21:22:30 -07:00
|
|
|
'Wrong host header for req[' + req.url + ']: ' +
|
|
|
|
req.headers.host);
|
|
|
|
}
|
2011-02-23 14:46:35 -08:00
|
|
|
res.writeHead(200, {});
|
|
|
|
//process.nextTick(function() { res.end('ok'); });
|
|
|
|
res.end('ok');
|
|
|
|
}
|
|
|
|
|
|
|
|
function thrower(er) {
|
|
|
|
throw er;
|
|
|
|
}
|
|
|
|
|
|
|
|
testHttp();
|
|
|
|
|
|
|
|
function testHttp() {
|
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
let counter = 0;
|
2011-02-23 14:46:35 -08:00
|
|
|
|
2012-12-13 07:47:33 -08:00
|
|
|
function cb(res) {
|
2011-02-23 14:46:35 -08:00
|
|
|
counter--;
|
|
|
|
console.log('back from http request. counter = ' + counter);
|
|
|
|
if (counter === 0) {
|
|
|
|
httpServer.close();
|
|
|
|
}
|
2012-12-13 07:47:33 -08:00
|
|
|
res.resume();
|
2011-02-23 14:46:35 -08:00
|
|
|
}
|
|
|
|
|
2016-05-29 03:06:56 -04:00
|
|
|
httpServer.listen(0, function(er) {
|
|
|
|
console.error(`test http server listening on ${this.address().port}`);
|
2016-12-30 10:54:01 -05:00
|
|
|
assert.ifError(er);
|
2012-08-30 16:43:20 +02:00
|
|
|
http.get({
|
|
|
|
method: 'GET',
|
2011-02-23 14:46:35 -08:00
|
|
|
path: '/' + (counter++),
|
|
|
|
host: 'localhost',
|
|
|
|
//agent: false,
|
2016-05-29 03:06:56 -04:00
|
|
|
port: this.address().port,
|
2012-08-30 16:43:20 +02:00
|
|
|
rejectUnauthorized: false
|
|
|
|
}, cb).on('error', thrower);
|
2011-02-23 14:46:35 -08:00
|
|
|
|
2012-08-30 16:43:20 +02:00
|
|
|
http.request({
|
|
|
|
method: 'GET',
|
2011-02-23 14:46:35 -08:00
|
|
|
path: '/' + (counter++),
|
|
|
|
host: 'localhost',
|
|
|
|
//agent: false,
|
2016-05-29 03:06:56 -04:00
|
|
|
port: this.address().port,
|
2012-08-30 16:43:20 +02:00
|
|
|
rejectUnauthorized: false
|
|
|
|
}, cb).on('error', thrower).end();
|
2011-02-23 14:46:35 -08:00
|
|
|
|
2012-08-30 16:43:20 +02:00
|
|
|
http.request({
|
|
|
|
method: 'POST',
|
2011-02-23 14:46:35 -08:00
|
|
|
path: '/' + (counter++),
|
|
|
|
host: 'localhost',
|
|
|
|
//agent: false,
|
2016-05-29 03:06:56 -04:00
|
|
|
port: this.address().port,
|
2012-08-30 16:43:20 +02:00
|
|
|
rejectUnauthorized: false
|
|
|
|
}, cb).on('error', thrower).end();
|
2011-02-23 14:46:35 -08:00
|
|
|
|
2012-08-30 16:43:20 +02:00
|
|
|
http.request({
|
|
|
|
method: 'PUT',
|
2011-02-23 14:46:35 -08:00
|
|
|
path: '/' + (counter++),
|
|
|
|
host: 'localhost',
|
|
|
|
//agent: false,
|
2016-05-29 03:06:56 -04:00
|
|
|
port: this.address().port,
|
2012-08-30 16:43:20 +02:00
|
|
|
rejectUnauthorized: false
|
|
|
|
}, cb).on('error', thrower).end();
|
2011-02-23 14:46:35 -08:00
|
|
|
|
2012-08-30 16:43:20 +02:00
|
|
|
http.request({
|
|
|
|
method: 'DELETE',
|
2011-02-23 14:46:35 -08:00
|
|
|
path: '/' + (counter++),
|
|
|
|
host: 'localhost',
|
|
|
|
//agent: false,
|
2016-05-29 03:06:56 -04:00
|
|
|
port: this.address().port,
|
2012-08-30 16:43:20 +02:00
|
|
|
rejectUnauthorized: false
|
|
|
|
}, cb).on('error', thrower).end();
|
2011-02-23 14:46:35 -08:00
|
|
|
});
|
|
|
|
}
|