This commit is contained in:
AJ ONeal 2016-11-21 15:45:05 -07:00
parent b3abd291e7
commit 699a97a902
2 changed files with 81 additions and 0 deletions

View File

@ -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
;

77
test.js Normal file
View File

@ -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();