diff --git a/src/protocols/http.js b/src/protocols/http.js index f01b3403a..1aa056010 100644 --- a/src/protocols/http.js +++ b/src/protocols/http.js @@ -17,7 +17,9 @@ class HttpWrapper extends KuzzleAbstractProtocol { if (options.http && options.http.customRoutes) { for (const controller in options.http.customRoutes) { if (options.http.customRoutes.hasOwnProperty(controller)) { - this.http.routes[controller] = Object.assign(this.http.routes[controller] || {}, options.http.customRoutes[controller]); + this.http.routes[controller] = Object.assign( + this.http.routes[controller] || {}, + options.http.customRoutes[controller]); } } } @@ -130,6 +132,15 @@ class HttpWrapper extends KuzzleAbstractProtocol { queryString.push(...value.map(v => `${key}=${v}`)); } + else if (typeof value === 'boolean') { + // In Kuzzle, an optional boolean option is set to true if present in + // the querystring, and false if absent. + // As there is no boolean type in querystrings, encoding a boolean + // option "foo=false" in it will make Kuzzle consider it as truthy. + if (value === true) { + queryString.push(key); + } + } else { queryString.push(`${key}=${value}`); } diff --git a/test/protocol/http.test.js b/test/protocol/http.test.js index 072ed6e9a..00680e515 100644 --- a/test/protocol/http.test.js +++ b/test/protocol/http.test.js @@ -387,6 +387,58 @@ describe('HTTP networking module', () => { protocol.send(data); }); + it('should properly encode arrays into the querystring', done => { + const data = { + requestId: 'requestId', + action: 'bar', + controller: 'foo', + foo: ['bar', 'baz', 'qux'], + qux: 123 + }; + + protocol.on('requestId', () => { + try { + should(protocol._sendHttpRequest).be.calledOnce(); + should(protocol._sendHttpRequest.firstCall.args[0]).be.equal('VERB'); + should(protocol._sendHttpRequest.firstCall.args[1]) + .be.equal('/foo/bar?foo=bar&foo=baz&foo=qux&qux=123'); + } + catch (error) { + return done(error); + } + + done(); + }); + + protocol.send(data); + }); + + it('should properly encode boolean flags in the querystring', done => { + const data = { + requestId: 'requestId', + action: 'bar', + controller: 'foo', + foo: false, + bar: true, + qux: 123 + }; + + protocol.on('requestId', () => { + try { + should(protocol._sendHttpRequest).be.calledOnce(); + should(protocol._sendHttpRequest.firstCall.args[0]).be.equal('VERB'); + should(protocol._sendHttpRequest.firstCall.args[1]) + .be.equal('/foo/bar?bar&qux=123'); + } + catch (error) { + return done(error); + } + + done(); + }); + + protocol.send(data); + }); }); describe('#sendHttpRequest NodeJS', () => {