From 699a97a902516c3d4f00ee166087a4738c1ce62d Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 21 Nov 2016 15:45:05 -0700 Subject: [PATCH] fix #2 --- index.js | 4 +++ test.js | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 test.js diff --git a/index.js b/index.js index fc604db..b9a4182 100644 --- a/index.js +++ b/index.js @@ -26,6 +26,10 @@ module.exports = function (opts) { var url = req.url; var host = req.headers.host || ''; + if (!/:\d+/.test(host) && 443 !== opts.port) { + // we are using standard port 80, but we aren't using standard port 443 + host += ':80'; + } var newLocation = 'https://' + host.replace(/:\d+/, ':' + opts.port) + url ; diff --git a/test.js b/test.js new file mode 100644 index 0000000..6ad687a --- /dev/null +++ b/test.js @@ -0,0 +1,77 @@ +'use strict'; + +var opts = { port: 0 }; +var redirect = require('./')(opts); + +function r80to443() { + // using port 80 and 443 + opts.port = 443; + var req = { connection: {}, headers: { host: 'example.com' }, url: '/path/to/somewhere.html' }; + var res = { + setHeader: function () {} + , end: function (body) { + if (!/:/.test(body)) { + throw new Error("test didn't pass with port 80 redirecting to 443"); + } + console.log('PASS: 80 to 443'); + } + }; + var next = function () {}; + redirect(req, res, next); +} + +function r80to8443() { + // using port 80, but not 443 + opts.port = 8443; + var req = { connection: {}, headers: { host: 'example.com' }, url: '/path/to/somewhere.html' }; + var res = { + setHeader: function () {} + , end: function (body) { + if (!/:8443/.test(body)) { + throw new Error("test didn't pass with port 80 redirecting to non-443"); + } + console.log('PASS: 80 to 8443'); + } + }; + var next = function () {}; + redirect(req, res, next); +} + +function r4080to8443() { + // using port 80 and 443 + opts.port = 8443; + var req = { connection: {}, headers: { host: 'example.com:4080' }, url: '/path/to/somewhere.html' }; + var res = { + setHeader: function () {} + , end: function (body) { + if (!/:8443/.test(body)) { + throw new Error("test didn't pass with port 4080 redirecting to 8443"); + } + console.log('PASS: 4080 to 8443'); + } + }; + var next = function () {}; + redirect(req, res, next); +} + +function r4080to443() { + // using port 80 and 443 + opts.port = 8443; + var req = { connection: {}, headers: { host: 'example.com:4080' }, url: '/path/to/somewhere.html' }; + var res = { + setHeader: function () {} + , end: function (body) { + if (/:/.test(body)) { + throw new Error("test didn't pass with port 4080 redirecting to 8443"); + } + console.log('PASS: 4080 to 8443'); + } + }; + var next = function () {}; + redirect(req, res, next); +} + +r80to443(); +r80to8443(); +r4080to8443(); +//r4080to443();