From 2686d505230faf7f060e2c97bb51baeb0986c4ac Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 9 Sep 2022 17:18:19 +0100 Subject: [PATCH] Fixing test cases. --- .../server/src/api/routes/tests/query.spec.js | 6 ++--- packages/server/src/integrations/rest.ts | 4 +++- .../src/integrations/tests/rest.spec.js | 24 ++++++++++++++++--- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/packages/server/src/api/routes/tests/query.spec.js b/packages/server/src/api/routes/tests/query.spec.js index 45f69de5e9..4253b6884b 100644 --- a/packages/server/src/api/routes/tests/query.spec.js +++ b/packages/server/src/api/routes/tests/query.spec.js @@ -311,7 +311,7 @@ describe("/queries", () => { "url": "string", "value": "string" }) - expect(res.body.rows[0].url).toContain("doctype html") + expect(res.body.rows[0].url).toContain("doctype%20html") }) it("check that it automatically retries on fail with cached dynamics", async () => { @@ -396,7 +396,7 @@ describe("/queries", () => { "queryHdr": userDetails.firstName, "secondHdr" : "1234" }) - expect(res.body.rows[0].url).toEqual("http://www.google.com?email=" + userDetails.email) + expect(res.body.rows[0].url).toEqual("http://www.google.com?email=" + userDetails.email.replace("@", "%40")) }) it("should bind the current user to query parameters", async () => { @@ -413,7 +413,7 @@ describe("/queries", () => { "testParam" : "1234" }) - expect(res.body.rows[0].url).toEqual("http://www.google.com?test=" + userDetails.email + + expect(res.body.rows[0].url).toEqual("http://www.google.com?test=" + userDetails.email.replace("@", "%40") + "&testName=" + userDetails.firstName + "&testParam=1234") }) diff --git a/packages/server/src/integrations/rest.ts b/packages/server/src/integrations/rest.ts index 59eb1e9941..1148eb0784 100644 --- a/packages/server/src/integrations/rest.ts +++ b/packages/server/src/integrations/rest.ts @@ -14,6 +14,7 @@ import { BearerAuthConfig, } from "../definitions/datasource" import { get } from "lodash" +import qs from "querystring" const BodyTypes = { NONE: "none", @@ -215,7 +216,8 @@ module RestModule { } } - const main = `${path}?${encodeURIComponent(queryString)}` + // make sure the query string is fully encoded + const main = `${path}?${qs.encode(qs.decode(queryString))}` let complete = main if (this.config.url && !main.startsWith("http")) { complete = !this.config.url ? main : `${this.config.url}/${main}` diff --git a/packages/server/src/integrations/tests/rest.spec.js b/packages/server/src/integrations/tests/rest.spec.js index 0bb1e3a75d..56e5c33042 100644 --- a/packages/server/src/integrations/tests/rest.spec.js +++ b/packages/server/src/integrations/tests/rest.spec.js @@ -50,7 +50,7 @@ describe("REST Integration", () => { name: "test", }), } - const response = await config.integration.create(query) + await config.integration.create(query) expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, { method: "POST", body: '{"name":"test"}', @@ -295,7 +295,7 @@ describe("REST Integration", () => { } await config.integration.read(query) expect(fetch).toHaveBeenCalledWith( - `${BASE_URL}/api?${pageParam}=${pageValue}&${sizeParam}=${sizeValue}&`, + `${BASE_URL}/api?${pageParam}=${pageValue}&${sizeParam}=${sizeValue}`, { headers: {}, method: "GET", @@ -420,7 +420,7 @@ describe("REST Integration", () => { } const res = await config.integration.read(query) expect(fetch).toHaveBeenCalledWith( - `${BASE_URL}/api?${pageParam}=${pageValue}&${sizeParam}=${sizeValue}&`, + `${BASE_URL}/api?${pageParam}=${pageValue}&${sizeParam}=${sizeValue}`, { headers: {}, method: "GET", @@ -528,5 +528,23 @@ describe("REST Integration", () => { expect(sentData.get(sizeParam)).toEqual(sizeValue.toString()) expect(res.pagination.cursor).toEqual(123) }) + + it("should encode query string correctly", async () => { + const query = { + path: "api", + queryString: "test=1 2", + headers: HEADERS, + bodyType: "json", + requestBody: JSON.stringify({ + name: "test", + }), + } + await config.integration.create(query) + expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1%202`, { + method: "POST", + body: '{"name":"test"}', + headers: HEADERS, + }) + }) }) })