From 674a48af62c3e81b884adf958e950bdc3f3a73c9 Mon Sep 17 00:00:00 2001 From: Patrick Mueller Date: Thu, 12 Jan 2017 23:01:49 -0500 Subject: [PATCH] don't parse url for getServiceURL unless required fixes https://github.com/cloudfoundry-community/node-cfenv/issues/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. --- lib-src/cfenv.coffee | 2 ++ lib/cfenv.js | 12 +++++++----- lib/server.js | 2 +- package.json | 6 +++--- tests/test-core.coffee | 12 +++++++++++- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/lib-src/cfenv.coffee b/lib-src/cfenv.coffee index d9e964e..c2f7d09 100644 --- a/lib-src/cfenv.coffee +++ b/lib-src/cfenv.coffee @@ -88,6 +88,8 @@ class AppEnv delete replacements.url + return url if _.isEmpty replacements + purl = URL.parse url for key, value of replacements diff --git a/lib/cfenv.js b/lib/cfenv.js index df54034..b7b3b57 100644 --- a/lib/cfenv.js +++ b/lib/cfenv.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.10.0 +// Generated by CoffeeScript 1.12.2 (function() { var AppEnv, URL, _, cfenv, fs, getApp, getBind, getName, getPort, getServices, getURLs, pkg, ports, throwError, yaml; @@ -25,7 +25,6 @@ AppEnv = (function() { function AppEnv(options) { - var error; if (options == null) { options = {}; } @@ -115,6 +114,9 @@ return null; } delete replacements.url; + if (_.isEmpty(replacements)) { + return url; + } purl = URL.parse(url); for (key in replacements) { value = replacements[key]; @@ -142,7 +144,7 @@ })(); getApp = function(appEnv, options) { - var e, envValue, error, locValue, ref, string; + var e, envValue, locValue, ref, string; string = process.env.VCAP_APPLICATION; envValue = {}; if (string != null) { @@ -164,7 +166,7 @@ }; getServices = function(appEnv, options) { - var e, envValue, error, locValue, ref, string; + var e, envValue, locValue, ref, string; string = process.env.VCAP_SERVICES; envValue = {}; if (string != null) { @@ -202,7 +204,7 @@ }; getName = function(appEnv, options) { - var error, pObject, pString, ref, val, yObject, yString; + var pObject, pString, ref, val, yObject, yString; if (options.name != null) { return options.name; } diff --git a/lib/server.js b/lib/server.js index 17b0812..cdd23d0 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.10.0 +// Generated by CoffeeScript 1.12.2 (function() { var JL, JS, cfenv, generateDump, http; diff --git a/package.json b/package.json index e75db88..b9b8411 100644 --- a/package.json +++ b/package.json @@ -11,13 +11,13 @@ "url": "https://github.com/cloudfoundry-community/node-cfenv.git" }, "dependencies": { - "js-yaml": "3.4.x", + "js-yaml": "3.7.x", "ports": "1.1.x", "underscore": "1.8.x" }, "devDependencies": { - "coffee-script": "1.10.x", - "mocha": "2.3.x", + "coffee-script": "1.12.x", + "mocha": "3.2.x", "expect.js": "0.3.x" } } diff --git a/tests/test-core.coffee b/tests/test-core.coffee index 43ea2fb..6c69b53 100644 --- a/tests/test-core.coffee +++ b/tests/test-core.coffee @@ -190,6 +190,16 @@ describe "appEnv", -> expect(url).to.be "org-protocol://org-host:666/org-path" + #------------------------------------------- + testURL = "mongodb://user:pass@example.com:18394,example.com:18394/admin" + vcap = getVCAPServicesWithCreds "service-a", + uri: testURL + + appEnv = cfenv.getAppEnv {vcap} + url = appEnv.getServiceURL "service-a" + + expect(url).to.be testURL + #----------------------------------------------------------------------------- it "local - getServiceCreds()", -> @@ -294,7 +304,7 @@ describe "appEnv", -> expect(appEnv.url).to.be "https://sample-uri.example.com" url = appEnv.getServiceURL "service-a" - expect(url).to.be "http://example.com/" + expect(url).to.be "http://example.com" #----------------------------------------------------------------------------- it "name - from option", ->