Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port ignored in response redirect [5.X] #1363

Merged
merged 3 commits into from Jun 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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