From 2b05540a20443daa329f4375076e0cc564e8ce34 Mon Sep 17 00:00:00 2001 From: Trevor Robinson Date: Fri, 29 Mar 2019 16:22:16 -0700 Subject: [PATCH] Construct urls using `url.format()` Also fix a test that wouldn't work --- lib/test.js | 9 ++++++++- test/supertest.js | 11 +++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/test.js b/lib/test.js index f87a3273..334075ba 100644 --- a/lib/test.js +++ b/lib/test.js @@ -4,6 +4,7 @@ var request = require('superagent'); var util = require('util'); +var url = require('url'); var http = require('http'); var https = require('https'); var assert = require('assert'); @@ -59,7 +60,13 @@ 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 url.format({ + protocol, + hostname: host || '127.0.0.1', + port, + pathname: path + }); }; /** diff --git a/test/supertest.js b/test/supertest.js index dfa90a7c..e87eb65a 100644 --- a/test/supertest.js +++ b/test/supertest.js @@ -1,6 +1,7 @@ const request = require('..'); const https = require('https'); const fs = require('fs'); +const os = require('os'); const path = require('path'); const should = require('should'); const express = require('express'); @@ -876,18 +877,16 @@ describe('agent.host(host)', function () { it('should set request hostname', function (done) { const app = express(); const agent = request.agent(app); + const localhostyName = os.hostname().toLowerCase(); app.get('/', function (req, res) { - res.send(); + res.send(req.hostname); }); agent - .host('something.test') + .host(localhostyName) .get('/') - .end(function (err, res) { - err.hostname.should.equal('something.test'); - done(); - }); + .expect(localhostyName, done); }); });