diff --git a/index.js b/index.js index fee3691..48305bb 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,7 @@ /** * if VCAP_SERVICES exists then it returns the credentials - * for the first service that stars with 'name' or {} otherwise + * for the last service that stars with 'name' or {} otherwise * @param String name, service name * @return {Object} the service credentials or {} if * name is not found in VCAP_SERVICES @@ -12,10 +12,10 @@ module.exports.getCredentials = function(name) { var services = JSON.parse(process.env.VCAP_SERVICES); for (var service_name in services) { if (service_name.indexOf(name) === 0) { - var service = services[service_name][0]; + var service = services[service_name][services[service_name].length - 1]; return service.credentials || {}; } } } return {}; -}; \ No newline at end of file +}; diff --git a/test/test.parse.js b/test/test.parse.js index 4af09f9..d0afa40 100644 --- a/test/test.parse.js +++ b/test/test.parse.js @@ -23,6 +23,8 @@ describe('vcap_services', function() { // set VCAP_SERVICES to a default value process.env.VCAP_SERVICES = JSON.stringify({ personality_insights: [{ + plan: 'beta' + },{ credentials: credentials, label: 'personality_insights', name: 'personality-insights-service',