diff --git a/client/src/href-builder.js b/client/src/href-builder.js index bac133946..7a4ab6f8b 100644 --- a/client/src/href-builder.js +++ b/client/src/href-builder.js @@ -4,6 +4,7 @@ var querystring = require('querystring'); var format = require('stringformat'); var url = require('url'); var settings = require('./settings'); +var mergeObjects = require('./utils/merge-objects'); module.exports = function(config){ return { @@ -52,7 +53,11 @@ module.exports = function(config){ prepareServerGet: function(baseUrl, component, options) { var urlPath = component.name + (component.version ? '/' + component.version : ''); - var qs = options.parameters ? ('/?' + querystring.stringify(options.parameters)) : ''; + + var qs = ''; + if (component.parameters || options.parameters) { + qs = '/?' + querystring.stringify(mergeObjects(component.parameters, options.parameters)); + } return url.resolve(baseUrl, urlPath + qs); } diff --git a/client/src/utils/merge-objects.js b/client/src/utils/merge-objects.js new file mode 100644 index 000000000..748887737 --- /dev/null +++ b/client/src/utils/merge-objects.js @@ -0,0 +1,17 @@ +'use strict'; + +function addProperties(source, destination) { + for (var key in source) { + if (source.hasOwnProperty(key) && !destination[key]) { + destination[key] = source[key]; + } + } +} + +// info: use Object.assign() w/ ES6 +module.exports = function (obj1, obj2) { + var obj3 = {}; + addProperties(obj1, obj3); + addProperties(obj2, obj3); + return obj3; +}; diff --git a/test/unit/client-href-builder.js b/test/unit/client-href-builder.js index a529a5622..d01b1aebc 100644 --- a/test/unit/client-href-builder.js +++ b/test/unit/client-href-builder.js @@ -59,5 +59,27 @@ describe('client : href-builder :', () => { .to.equal('http://localhost:3030/hello-world/1.0.0/?p1=v1&p2=v%202'); }); }); + + describe('when there are parameters in both "options" and "component"', () => { + it('it should return a valid request for the component with the parameters set as URL query params', () => { + let options = { parameters: { p1: 'v1', p2: 'v 2' } }; + let component = { name: 'hello-world', version: '1.0.0', parameters: { message: 'hello' } }; + let hrefBuilder = new hrefBuilderPrototype({}); + + expect(hrefBuilder.prepareServerGet('http://localhost:3030', component, options)) + .to.equal('http://localhost:3030/hello-world/1.0.0/?message=hello&p1=v1&p2=v%202'); + }); + }); + + describe('when there are common parameters in both "options" and "component"', () => { + it('it should return a valid request for the component with the parameters set as URL query params', () => { + let options = { parameters: { p1: 'v1', p2: 'v 2' } }; + let component = { name: 'hello-world', version: '1.0.0', parameters: { message: 'hello', p1: 'v3' } }; + let hrefBuilder = new hrefBuilderPrototype({}); + + expect(hrefBuilder.prepareServerGet('http://localhost:3030', component, options)) + .to.equal('http://localhost:3030/hello-world/1.0.0/?message=hello&p1=v3&p2=v%202'); + }); + }); }); }); diff --git a/test/unit/client-merge-objects.js b/test/unit/client-merge-objects.js new file mode 100644 index 000000000..2ee51fa10 --- /dev/null +++ b/test/unit/client-merge-objects.js @@ -0,0 +1,24 @@ +'use strict'; + +const expect = require('chai').expect; +const mergeObjects = require('../../client/src/utils/merge-objects'); + +describe('client : merge-objects :', () => { + const scenarios = [ + { describe: 'obj1 is empty and obj2 is empty', obj1: {}, obj2: {}, obj3: {} }, + { describe: 'obj1 is not empty and obj2 is empty', obj1: { p1: 'aaa' }, obj2: {}, obj3: { p1: 'aaa' } }, + { describe: 'obj1 is empty and obj2 is not empty', obj1: {}, obj2: { p2: 'bbb' }, obj3: { p2: 'bbb' } }, + { describe: 'obj1 is not empty and obj2 is not empty', obj1: { p1: 'aaa' }, obj2: { p2: 'bbb' }, obj3: { p1: 'aaa', p2: 'bbb' } }, + { describe: 'obj1 is undefined and obj2 is undefined', obj1: undefined, obj2: undefined, obj3: {} }, + { describe: 'obj1 is null and obj2 is null', obj1: null, obj2: null, obj3: {} }, + { describe: 'obj1 and obj2 have a property in common', obj1: { a: 1, b: 2 }, obj2: { b: 3, c: 4 }, obj3: { a: 1, b: 2, c: 4 } } + ]; + + scenarios.forEach((scenario) => { + describe(`when ${scenario.describe}`, () => { + it(`then obj3 is equal to ${JSON.stringify(scenario.obj3)}`, () => { + expect(mergeObjects(scenario.obj1, scenario.obj2)).to.deep.equal(scenario.obj3); + }); + }); + }); +});