Add the replaceLocalhost option to Meteor.absoluteUrl

This commit is contained in:
Avital Oliver
2012-09-20 15:07:52 -07:00
parent 4b8a688000
commit b0d181dbcc
4 changed files with 28 additions and 2 deletions

View File

@@ -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`\""

View File

@@ -16,7 +16,6 @@ and removed with:
$ meteor remove <package_name>
{{> pkg_absolute_url}}
{{> pkg_amplify}}
{{> pkg_backbone}}
{{> pkg_bootstrap}}

View File

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

View File

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