Navigation Menu

Skip to content

Commit

Permalink
fix: Honor port for redirect (#1363)
Browse files Browse the repository at this point in the history
* Allow for custom port on options object in redirect
  • Loading branch information
aguilarcarlos authored and William Blankenship committed Jun 19, 2017
1 parent 96f934f commit 61c0cb5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/response.js
Expand Up @@ -640,6 +640,11 @@ Response.prototype.redirect = function redirect(arg1, arg2, arg3) {
finalUri.pathname = opt.pathname;
}

// then set port
if (opt.port) {
finalUri.port = opt.port;
}

// then add query params
if (opt.query) {
if (opt.overrideQuery === true) {
Expand Down
56 changes: 56 additions & 0 deletions test/response.test.js
Expand Up @@ -287,6 +287,62 @@ test('redirect using options.url', function (t) {
});


test('redirect using opts.port', function (t) {
SERVER.get('/9', function (req, res, next) {
res.redirect({
port: 3000
}, next);
});

CLIENT.get(join(LOCALHOST, '/9'), function (err, _, res) {
t.ifError(err);
t.equal(res.statusCode, 302);
var parsedUrl = url.parse(res.headers.location, true);
t.equal(parsedUrl.port, 3000);
t.end();
});
});


test('redirect using external url and custom port', function (t) {
SERVER.get('/9', function (req, res, next) {
res.redirect({
hostname: 'www.foo.com',
pathname: '/99',
port: 3000
}, next);
});

CLIENT.get(join(LOCALHOST, '/9'), function (err, _, res) {
t.ifError(err);
t.equal(res.statusCode, 302);
var parsedUrl = url.parse(res.headers.location, true);
t.equal(parsedUrl.port, 3000);
t.equal(parsedUrl.hostname, 'www.foo.com');
t.equal(parsedUrl.pathname, '/99');
t.end();
});
});

test('redirect using default hostname with custom port', function (t) {
SERVER.get('/9', function (req, res, next) {
res.redirect({
pathname: '/99',
port: 3000
}, next);
});

CLIENT.get(join(LOCALHOST, '/9'), function (err, _, res) {
t.ifError(err);
t.equal(res.statusCode, 302);
var parsedUrl = url.parse(res.headers.location, true);
t.equal(parsedUrl.port, 3000);
t.equal(parsedUrl.pathname, '/99');
t.equal(res.headers.location, 'http://127.0.0.1:3000/99');
t.end();
});
});

// jscs:disable maximumLineLength
test('redirect should cause InternalError when invoked without next', function (t) {

Expand Down

0 comments on commit 61c0cb5

Please sign in to comment.