Allow ROOT_URL to not contain http://

This commit is contained in:
Avital Oliver
2013-04-08 14:47:25 -07:00
parent 977d34c1f3
commit af89ff5000
2 changed files with 59 additions and 53 deletions

View File

@@ -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 += '/';

View File

@@ -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');
});
});