From 88691f53cd259d7d7d27155768f590aa0f2aac55 Mon Sep 17 00:00:00 2001 From: Ritik Jain <60597329+re-Tick@users.noreply.github.com> Date: Tue, 14 Feb 2023 17:58:03 +0530 Subject: [PATCH] fix: stores correct http response for unique test-ids (#41) Signed-off-by: re-Tick --- src/client.ts | 26 ++++++++++++-------------- src/keploy.ts | 9 +++++++-- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/client.ts b/src/client.ts index 0f0760c..b41cb82 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,8 +1,8 @@ -import http, { Headers, OptionsOfJSONResponseBody, Response } from "got"; +import http, { Headers, Options } from "got"; export class Request { headers: Headers; - options: OptionsOfJSONResponseBody; + options: Options; constructor() { this.headers = { "User-Agent": "keploy-typescript-sdk" }; @@ -32,7 +32,7 @@ export class Request { url: requestUrl, method: "GET", headers: this.headers, - responseType: "json", + responseType: "buffer", searchParams, }; @@ -49,7 +49,7 @@ export class Request { url: requestUrl, method: "POST", headers: this.headers, - responseType: "json", + responseType: "buffer", searchParams, }; @@ -82,7 +82,7 @@ export class Request { url: requestUrl, method: "PUT", headers: this.headers, - responseType: "json", + responseType: "buffer", searchParams, }; @@ -99,7 +99,7 @@ export class Request { url: requestUrl, method: "PATCH", headers: this.headers, - responseType: "json", + responseType: "buffer", searchParams, }; @@ -136,14 +136,12 @@ export default class HttpClient { this.baseUrl = baseUrl; } - async makeHttpRequest(request: Request): Promise { + async makeHttpRequestRaw(request: Request) { const options = { ...request.raw(), prefixUrl: this.baseUrl }; - return http(options).json(); - } - - async makeHttpRequestRaw(request: Request): Promise> { - const options = { ...request.raw(), prefixUrl: this.baseUrl }; - const resp: Response = await http(options); - return resp; + try { + await http(options); + } catch (error) { + console.log(error); + } } } diff --git a/src/keploy.ts b/src/keploy.ts index 3a616bf..1de2cca 100644 --- a/src/keploy.ts +++ b/src/keploy.ts @@ -153,8 +153,13 @@ export default class Keploy { return this.responses[id]; } + // stores http response for unique test-ids to capture them at middleware layer putResp(id: ID, resp: HttpResponse) { - this.responses[id] = resp; + // put http response in map only once for unique test-ids. Since, finish event + // can trigger multiple time for redirect. + if (this.responses[id] === null || this.responses[id] === undefined) { + this.responses[id] = resp; + } } capture(req: TestCaseReq) { @@ -348,7 +353,7 @@ export default class Keploy { //@ts-ignore const requestUrl = `${tc.HttpReq?.URL.substr(1)}`; - await client.makeHttpRequestRaw( + await client.makeHttpRequestRaw( new Request() .setHttpHeader("KEPLOY_TEST_ID", tc.id) //@ts-ignore