Skip to content

Commit e814df2

Browse files
Allow to call custom GET HTTP method with index and collection args (#716)
Intend to fix #713 by adding a edge case handling in the request payload forging loop that allow user to call a custom HTTP GET route that expects to receive queries named index and/or collection.
1 parent 536f1ee commit e814df2

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/protocols/Http.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,14 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
274274
payload.headers.authorization = "Bearer " + value;
275275
} else if (key === "volatile") {
276276
payload.headers["x-kuzzle-volatile"] = JSON.stringify(value);
277+
} else if (key === "index" || key === "collection") {
278+
// If we're calling a non-native route that answer to a GET request
279+
// we need to add the index and collection (if provided) to the query string
280+
if (!staticHttpRoutes[request.controller] && method === "GET") {
281+
queryArgs[key] = value;
282+
} else {
283+
payload[key] = value;
284+
}
277285
} else if (Object.prototype.hasOwnProperty.call(payload, key)) {
278286
payload[key] = value;
279287
} else if (value !== undefined && value !== null) {

test/protocol/Http.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,33 @@ describe("HTTP networking module", () => {
639639

640640
protocol.send(data);
641641
});
642+
643+
it("should add index and collection to the query args if they are defined when using a custom GET route", (done) => {
644+
const data = {
645+
requestId: "requestId",
646+
controller: "foo",
647+
action: "bar",
648+
index: "index",
649+
collection: "collection",
650+
};
651+
652+
protocol._routes = {
653+
foo: { bar: { verb: "GET", url: "/foo/bar" } },
654+
};
655+
656+
protocol.on("requestId", () => {
657+
should(protocol._sendHttpRequest)
658+
.be.calledOnce()
659+
.and.be.calledWithMatch({
660+
method: "GET",
661+
path: "/foo/bar?index=index&collection=collection",
662+
});
663+
664+
done();
665+
});
666+
667+
protocol.send(data);
668+
});
642669
});
643670

644671
describe("#sendHttpRequest NodeJS", () => {

0 commit comments

Comments
 (0)