Skip to content

Commit de056d2

Browse files
author
Stefan Lau
committedSep 25, 2018
Set host header instead of host in url
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.
1 parent e910e85 commit de056d2

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed
 

‎lib/agent.js

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ methods.forEach(function(method) {
5353
req.ca(this._ca);
5454
req.cert(this._cert);
5555
req.key(this._key);
56+
if (this._host) {
57+
req.set('host', this._host);
58+
}
5659

5760
req.on('response', this._saveCookies.bind(this));
5861
req.on('redirect', this._saveCookies.bind(this));

‎lib/test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ module.exports = Test;
2525
* @api public
2626
*/
2727

28-
function Test(app, method, path, host) {
28+
function Test(app, method, path) {
2929
Request.call(this, method.toUpperCase(), path);
3030
this.redirects(0);
3131
this.buffer();
3232
this.app = app;
3333
this._asserts = [];
3434
this.url = typeof app === 'string'
3535
? app + path
36-
: this.serverAddress(app, path, host);
36+
: this.serverAddress(app, path);
3737
}
3838

3939
/**
@@ -51,15 +51,15 @@ Object.setPrototypeOf(Test.prototype, Request.prototype);
5151
* @api private
5252
*/
5353

54-
Test.prototype.serverAddress = function(app, path, host) {
54+
Test.prototype.serverAddress = function(app, path) {
5555
var addr = app.address();
5656
var port;
5757
var protocol;
5858

5959
if (!addr) this._server = app.listen(0);
6060
port = app.address().port;
6161
protocol = app instanceof https.Server ? 'https' : 'http';
62-
return protocol + '://' + (host || '127.0.0.1') + ':' + port + path;
62+
return protocol + '://127.0.0.1:' + port + path;
6363
};
6464

6565
/**

‎test/supertest.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -866,14 +866,15 @@ describe('agent.host(host)', function () {
866866
const agent = request.agent(app);
867867

868868
app.get('/', function (req, res) {
869-
res.send();
869+
res.send({ hostname: req.hostname });
870870
});
871871

872872
agent
873873
.host('something.test')
874874
.get('/')
875875
.end(function (err, res) {
876-
err.hostname.should.equal('something.test');
876+
if (err) return done(err);
877+
res.body.hostname.should.equal('something.test');
877878
done();
878879
});
879880
});

0 commit comments

Comments
 (0)
Please sign in to comment.