diff --git a/packages/meteor/url_common.js b/packages/meteor/url_common.js index a33fb77512..a1f44173f4 100644 --- a/packages/meteor/url_common.js +++ b/packages/meteor/url_common.js @@ -11,6 +11,9 @@ Meteor.absoluteUrl = function (path, options) { if (!url) throw new Error("Must pass options.rootUrl or set ROOT_URL in the server environment"); + if (!/^http[s]?:\/\//i.test(url)) // url starts with 'http://' or 'https://' + url = 'http://' + url; // we will later fix to https if options.secure is set + if (!/\/$/.test(url)) // url ends with '/' url += '/'; diff --git a/packages/meteor/url_tests.js b/packages/meteor/url_tests.js index 922fe045f8..002c5d4f4f 100644 --- a/packages/meteor/url_tests.js +++ b/packages/meteor/url_tests.js @@ -1,62 +1,65 @@ Tinytest.add("absolute-url - basics", function(test) { - test.equal(Meteor.absoluteUrl({rootUrl: 'http://asdf.com'}), - 'http://asdf.com/'); - test.equal(Meteor.absoluteUrl(undefined, {rootUrl: 'http://asdf.com'}), - 'http://asdf.com/'); - test.equal(Meteor.absoluteUrl(undefined, {rootUrl: 'http://asdf.com/'}), - 'http://asdf.com/'); + _.each(['', 'http://'], function (prefix) { - test.equal(Meteor.absoluteUrl('foo', {rootUrl: 'http://asdf.com/'}), - 'http://asdf.com/foo'); - test.equal(Meteor.absoluteUrl('/foo', {rootUrl: 'http://asdf.com'}), - 'http://asdf.com//foo'); - test.equal(Meteor.absoluteUrl('#foo', {rootUrl: 'http://asdf.com'}), - 'http://asdf.com/#foo'); + test.equal(Meteor.absoluteUrl({rootUrl: prefix + 'asdf.com'}), + 'http://asdf.com/'); + test.equal(Meteor.absoluteUrl(undefined, {rootUrl: prefix + 'asdf.com'}), + 'http://asdf.com/'); + test.equal(Meteor.absoluteUrl(undefined, {rootUrl: prefix + 'asdf.com/'}), + 'http://asdf.com/'); - test.equal(Meteor.absoluteUrl('foo', {rootUrl: 'http://asdf.com', - secure: true}), - 'https://asdf.com/foo'); - test.equal(Meteor.absoluteUrl('foo', {rootUrl: 'https://asdf.com', - secure: true}), - 'https://asdf.com/foo'); - test.equal(Meteor.absoluteUrl('foo', {rootUrl: 'https://asdf.com', - secure: false}), - 'https://asdf.com/foo'); + test.equal(Meteor.absoluteUrl('foo', {rootUrl: prefix + 'asdf.com/'}), + 'http://asdf.com/foo'); + test.equal(Meteor.absoluteUrl('/foo', {rootUrl: prefix + 'asdf.com'}), + 'http://asdf.com//foo'); + test.equal(Meteor.absoluteUrl('#foo', {rootUrl: prefix + 'asdf.com'}), + 'http://asdf.com/#foo'); - test.equal(Meteor.absoluteUrl('foo', {rootUrl: 'http://localhost', - secure: true}), - 'http://localhost/foo'); - test.equal(Meteor.absoluteUrl('foo', {rootUrl: 'http://localhost:3000', - secure: true}), - 'http://localhost:3000/foo'); - test.equal(Meteor.absoluteUrl('foo', {rootUrl: 'https://localhost:3000', - secure: true}), - 'https://localhost:3000/foo'); - test.equal(Meteor.absoluteUrl('foo', {rootUrl: 'http://127.0.0.1:3000', - secure: true}), - 'http://127.0.0.1:3000/foo'); + test.equal(Meteor.absoluteUrl('foo', {rootUrl: prefix + 'asdf.com', + secure: true}), + 'https://asdf.com/foo'); + test.equal(Meteor.absoluteUrl('foo', {rootUrl: 'https://asdf.com', + secure: true}), + 'https://asdf.com/foo'); + test.equal(Meteor.absoluteUrl('foo', {rootUrl: 'https://asdf.com', + secure: false}), + 'https://asdf.com/foo'); - // test replaceLocalhost - test.equal(Meteor.absoluteUrl('foo', {rootUrl: 'http://localhost:3000', - replaceLocalhost: true}), - 'http://127.0.0.1:3000/foo'); - test.equal(Meteor.absoluteUrl('foo', {rootUrl: 'http://localhost', - replaceLocalhost: true}), - 'http://127.0.0.1/foo'); - test.equal(Meteor.absoluteUrl('foo', {rootUrl: 'http://127.0.0.1:3000', - replaceLocalhost: true}), - 'http://127.0.0.1:3000/foo'); - test.equal(Meteor.absoluteUrl('foo', {rootUrl: 'http://127.0.0.1', - replaceLocalhost: true}), - 'http://127.0.0.1/foo'); - // don't replace just any localhost - test.equal(Meteor.absoluteUrl('foo', {rootUrl: 'http://foo.com/localhost', - replaceLocalhost: true}), - 'http://foo.com/localhost/foo'); - test.equal(Meteor.absoluteUrl('foo', {rootUrl: 'http://foo.localhost.com', - replaceLocalhost: true}), - 'http://foo.localhost.com/foo'); + test.equal(Meteor.absoluteUrl('foo', {rootUrl: prefix + 'localhost', + secure: true}), + 'http://localhost/foo'); + test.equal(Meteor.absoluteUrl('foo', {rootUrl: prefix + 'localhost:3000', + secure: true}), + 'http://localhost:3000/foo'); + test.equal(Meteor.absoluteUrl('foo', {rootUrl: 'https://localhost:3000', + secure: true}), + 'https://localhost:3000/foo'); + test.equal(Meteor.absoluteUrl('foo', {rootUrl: prefix + '127.0.0.1:3000', + secure: true}), + 'http://127.0.0.1:3000/foo'); + + // test replaceLocalhost + test.equal(Meteor.absoluteUrl('foo', {rootUrl: prefix + 'localhost:3000', + replaceLocalhost: true}), + 'http://127.0.0.1:3000/foo'); + test.equal(Meteor.absoluteUrl('foo', {rootUrl: prefix + 'localhost', + replaceLocalhost: true}), + 'http://127.0.0.1/foo'); + test.equal(Meteor.absoluteUrl('foo', {rootUrl: prefix + '127.0.0.1:3000', + replaceLocalhost: true}), + 'http://127.0.0.1:3000/foo'); + test.equal(Meteor.absoluteUrl('foo', {rootUrl: prefix + '127.0.0.1', + replaceLocalhost: true}), + 'http://127.0.0.1/foo'); + // don't replace just any localhost + test.equal(Meteor.absoluteUrl('foo', {rootUrl: prefix + 'foo.com/localhost', + replaceLocalhost: true}), + 'http://foo.com/localhost/foo'); + test.equal(Meteor.absoluteUrl('foo', {rootUrl: prefix + 'foo.localhost.com', + replaceLocalhost: true}), + 'http://foo.localhost.com/foo'); + }); });