Skip to content

Commit 94cf06c

Browse files
author
Patrick Mueller
committed
don't parse url for getServiceURL unless required
fixes #21 Turns out the following is not always true: ```js url.format(url.parse(SomeURL)) == SomeURL ``` At least when the protocol isn't `http:` or `https:`. cfenv expects this to be true in `getServiceURL()`, when using the replacements argument. It seems to break for some example mongodb urls, like the one added to the test in this commit. Fix for now is that if there aren't any replacement values, then return early with the currently calculated URL. So it would still break if you passed one of these not-parsed-correctly URLs and replacements, but the odds of that seem pretty slim. Also noticed that `url.format()`` must be standardizing URLs to return them with a trailing `/` if there was no other path. So now you don't always get that trailing `/`. Seems survivable.
1 parent b3eeec9 commit 94cf06c

File tree

5 files changed

+24
-10
lines changed

5 files changed

+24
-10
lines changed

lib-src/cfenv.coffee

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ class AppEnv
8888

8989
delete replacements.url
9090

91+
return url if _.isEmpty replacements
92+
9193
purl = URL.parse url
9294

9395
for key, value of replacements

lib/cfenv.js

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/server.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
"url": "https://github.com/cloudfoundry-community/node-cfenv.git"
1212
},
1313
"dependencies": {
14-
"js-yaml": "3.4.x",
14+
"js-yaml": "3.7.x",
1515
"ports": "1.1.x",
1616
"underscore": "1.8.x"
1717
},
1818
"devDependencies": {
19-
"coffee-script": "1.10.x",
20-
"mocha": "2.3.x",
19+
"coffee-script": "1.12.x",
20+
"mocha": "3.2.x",
2121
"expect.js": "0.3.x"
2222
}
2323
}

tests/test-core.coffee

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,16 @@ describe "appEnv", ->
190190

191191
expect(url).to.be "org-protocol://org-host:666/org-path"
192192

193+
#-------------------------------------------
194+
testURL = "mongodb://user:[email protected]:18394,example.com:18394/admin"
195+
vcap = getVCAPServicesWithCreds "service-a",
196+
uri: testURL
197+
198+
appEnv = cfenv.getAppEnv {vcap}
199+
url = appEnv.getServiceURL "service-a"
200+
201+
expect(url).to.be testURL
202+
193203
#-----------------------------------------------------------------------------
194204
it "local - getServiceCreds()", ->
195205

@@ -294,7 +304,7 @@ describe "appEnv", ->
294304
expect(appEnv.url).to.be "https://sample-uri.example.com"
295305

296306
url = appEnv.getServiceURL "service-a"
297-
expect(url).to.be "http://example.com/"
307+
expect(url).to.be "http://example.com"
298308

299309
#-----------------------------------------------------------------------------
300310
it "name - from option", ->

0 commit comments

Comments
 (0)