From b0d181dbcc1e22ec9b425af2366fa6bf7f37cd2b Mon Sep 17 00:00:00 2001 From: Avital Oliver Date: Thu, 20 Sep 2012 15:07:52 -0700 Subject: [PATCH] Add the replaceLocalhost option to Meteor.absoluteUrl --- docs/client/api.js | 5 ++++- docs/client/packages.html | 1 - packages/meteor/url_common.js | 3 +++ packages/meteor/url_tests.js | 21 +++++++++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/docs/client/api.js b/docs/client/api.js index c6ef8d2940..e638a8b62b 100644 --- a/docs/client/api.js +++ b/docs/client/api.js @@ -25,7 +25,7 @@ Template.api.startup = { }; Template.api.absoluteUrl = { - id: "meteor_absoluteUrl", + id: "meteor_absoluteurl", name: "Meteor.absoluteUrl([path], [options])", locus: "Anywhere", descr: ["Generate an absolute URL pointing to the application. The server " @@ -44,6 +44,9 @@ Template.api.absoluteUrl = { type: "Boolean", descr: "Create an HTTPS URL." }, + {name: "replaceLocalhost", + type: "Boolean", + descr: "Replace localhost with 127.0.0.1. Useful for services that don't recognize localhost as a domain name."}, {name: "rootUrl", type: "String", descr: "Override the default ROOT_URL from the server environment. For example: \"`http://foo.example.com`\"" diff --git a/docs/client/packages.html b/docs/client/packages.html index 51eddc0373..1c12157077 100644 --- a/docs/client/packages.html +++ b/docs/client/packages.html @@ -16,7 +16,6 @@ and removed with: $ meteor remove -{{> pkg_absolute_url}} {{> pkg_amplify}} {{> pkg_backbone}} {{> pkg_bootstrap}} diff --git a/packages/meteor/url_common.js b/packages/meteor/url_common.js index 4c65f71170..c6ac9b2770 100644 --- a/packages/meteor/url_common.js +++ b/packages/meteor/url_common.js @@ -27,6 +27,9 @@ !/http:\/\/127\.0\.0\.1[:\/]/.test(url)) // or 127.0.0.1 url = url.replace(/^http:/, 'https:'); + if (options.replaceLocalhost) + url = url.replace(/^http:\/\/localhost([:\/].*)/, 'http://127.0.0.1$1'); + return url; }; diff --git a/packages/meteor/url_tests.js b/packages/meteor/url_tests.js index 6548dfc7ab..922fe045f8 100644 --- a/packages/meteor/url_tests.js +++ b/packages/meteor/url_tests.js @@ -36,6 +36,27 @@ Tinytest.add("absolute-url - basics", function(test) { test.equal(Meteor.absoluteUrl('foo', {rootUrl: 'http://127.0.0.1:3000', secure: true}), 'http://127.0.0.1:3000/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'); });