diff --git a/lib/agent.js b/lib/agent.js index 21ee6211..c6ee7869 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -54,6 +54,9 @@ methods.forEach(function(method) { req.ca(this._ca); req.cert(this._cert); req.key(this._key); + if (this._host) { + req.set('host', this._host); + } req.on('response', this._saveCookies.bind(this)); req.on('redirect', this._saveCookies.bind(this)); diff --git a/lib/test.js b/lib/test.js index 3f534a5c..0dbb3e17 100644 --- a/lib/test.js +++ b/lib/test.js @@ -27,7 +27,7 @@ module.exports = Test; * @api public */ -function Test(app, method, path, host) { +function Test(app, method, path) { Request.call(this, method.toUpperCase(), path); this.redirects(0); this.buffer(); @@ -35,7 +35,7 @@ function Test(app, method, path, host) { this._asserts = []; this.url = typeof app === 'string' ? app + path - : this.serverAddress(app, path, host); + : this.serverAddress(app, path); } /** @@ -53,7 +53,7 @@ Object.setPrototypeOf(Test.prototype, Request.prototype); * @api private */ -Test.prototype.serverAddress = function(app, path, host) { +Test.prototype.serverAddress = function(app, path) { var addr = app.address(); var port; var protocol; @@ -61,7 +61,7 @@ Test.prototype.serverAddress = function(app, path, host) { if (!addr) this._server = app.listen(0); port = app.address().port; protocol = app instanceof https.Server ? 'https' : 'http'; - return protocol + '://' + (host || '127.0.0.1') + ':' + port + path; + return protocol + '://127.0.0.1:' + port + path; }; /** diff --git a/test/supertest.js b/test/supertest.js index a9ed21c6..215f2be2 100644 --- a/test/supertest.js +++ b/test/supertest.js @@ -880,14 +880,15 @@ describe('agent.host(host)', function () { const agent = request.agent(app); app.get('/', function (req, res) { - res.send(); + res.send({ hostname: req.hostname }); }); agent .host('something.test') .get('/') .end(function (err, res) { - err.hostname.should.equal('something.test'); + if (err) return done(err); + res.body.hostname.should.equal('something.test'); done(); }); });