Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/protocols/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
}
Expand Down Expand Up @@ -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);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpicking: do we really need the comment block and the nested if?

else if (typeof value === 'boolean' && value) {
  // foo=false => foo is truthy
  queryString.push(key);
}

Copy link
Contributor Author

@scottinet scottinet Jul 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need the nested if because, otherwise, a falsey boolean value will be processed by the final else condition just below, and added to the querystring anyway

}
else {
queryString.push(`${key}=${value}`);
}
Expand Down
52 changes: 52 additions & 0 deletions test/protocol/http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down