From 6f75b0189de9b2e45b88dae535b01703de585ce2 Mon Sep 17 00:00:00 2001 From: Benn Bollay Date: Wed, 27 May 2020 10:30:28 -0700 Subject: [PATCH 1/2] Support port overrides in connect() Allow for .connect({'*': {host: 'hostname', port: 12345}}) overrides to change the destination port of a request. --- docs/index.md | 1 + src/node/index.js | 20 ++++++++++++++++---- test/node/redirects.js | 18 ++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/docs/index.md b/docs/index.md index 01a61b624..90d7d0356 100644 --- a/docs/index.md +++ b/docs/index.md @@ -692,6 +692,7 @@ Because the request may be redirected, it's possible to specify multiple hostnam .connect({ "redir.example.com": "127.0.0.1", // redir.example.com:555 will use 127.0.0.1:555 "www.example.com": false, // don't override this one; use DNS as normal + "mapped.example.com": { host: "127.0.0.1", port: 8080}, // mapped.example.com:* will use 127.0.0.1:8080 "*": "proxy.example.com", // all other requests will go to this host }); diff --git a/src/node/index.js b/src/node/index.js index 7c3e0cd9b..62b3c13c1 100644 --- a/src/node/index.js +++ b/src/node/index.js @@ -726,13 +726,25 @@ Request.prototype.request = function() { this.set('host', url.host); } + let newHost; + let newPort; + + if (typeof match === 'object') { + newHost = match.host; + newPort = match.port; + } else { + newHost = match; + newPort = url.port; + } + // wrap [ipv6] - url.host = /:/.test(match) ? `[${match}]` : match; - if (url.port) { - url.host += `:${url.port}`; + url.host = /:/.test(newHost) ? `[${newHost}]` : newHost; + if (newPort) { + url.host += `:${newPort}`; + url.port = newPort; } - url.hostname = match; + url.hostname = newHost; } } diff --git a/test/node/redirects.js b/test/node/redirects.js index d510fd7ed..d3faa34a2 100644 --- a/test/node/redirects.js +++ b/test/node/redirects.js @@ -103,6 +103,24 @@ describe('request', () => { }); }); + it('should follow Location with IP:port override', () => { + const redirects = []; + const url = URL.parse(base); + return request + .get(`http://redir.example.com:9999${url.pathname}`) + .connect({ + '*': { host: url.hostname, port: url.port || 80 } + }) + .on('redirect', res => { + redirects.push(res.headers.location); + }) + .then(res => { + const arr = ['/movies', '/movies/all', '/movies/all/0']; + redirects.should.eql(arr); + res.text.should.equal('first movie page'); + }); + }); + it('should not follow on HEAD by default', () => { const redirects = []; From 046accb972b3d657e044773dd11fb7e8776765a5 Mon Sep 17 00:00:00 2001 From: Benn Bollay Date: Wed, 27 May 2020 12:59:11 -0700 Subject: [PATCH 2/2] Increment version to 5.2.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0b710235a..c4c749086 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "superagent", "description": "elegant & feature rich browser / node HTTP with a fluent API", - "version": "5.2.2", + "version": "5.2.3", "author": "TJ Holowaychuk ", "browser": { "./src/node/index.js": "./src/client.js",