Skip to content

Commit

Permalink
Set host header instead of host in url
Browse files Browse the repository at this point in the history
Setting the host in the url might result in the request going
to a completely different server (wherever the hostname resolves
to), not necessarily the server under test.
  • Loading branch information
Stefan Lau committed Sep 25, 2018
1 parent e910e85 commit de056d2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/agent.js
Expand Up @@ -53,6 +53,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));
Expand Down
8 changes: 4 additions & 4 deletions lib/test.js
Expand Up @@ -25,15 +25,15 @@ 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();
this.app = app;
this._asserts = [];
this.url = typeof app === 'string'
? app + path
: this.serverAddress(app, path, host);
: this.serverAddress(app, path);
}

/**
Expand All @@ -51,15 +51,15 @@ 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;

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;
};

/**
Expand Down
5 changes: 3 additions & 2 deletions test/supertest.js
Expand Up @@ -866,14 +866,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();
});
});
Expand Down

0 comments on commit de056d2

Please sign in to comment.