From ed2a461dc5c111b7a4fb3cb224ba992f621e6e60 Mon Sep 17 00:00:00 2001 From: Gaurav K P Date: Mon, 4 Dec 2023 23:31:49 +0530 Subject: [PATCH] feat(common): display status text from the API response if available (#3466) Co-authored-by: jamesgeorge007 --- .../src/components/http/ResponseMeta.vue | 7 ++++++- packages/hoppscotch-common/src/helpers/network.ts | 2 +- .../src/helpers/types/HoppRESTResponse.ts | 3 ++- .../src/helpers/utils/statusCodes.ts | 13 ++++++++++++- .../inspectors/__tests__/response.inspector.spec.ts | 4 ++++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/hoppscotch-common/src/components/http/ResponseMeta.vue b/packages/hoppscotch-common/src/components/http/ResponseMeta.vue index 1217ae518..ef246088e 100644 --- a/packages/hoppscotch-common/src/components/http/ResponseMeta.vue +++ b/packages/hoppscotch-common/src/components/http/ResponseMeta.vue @@ -53,7 +53,12 @@ {{ t("response.status") }}: {{ `${response.statusCode}\xA0 • \xA0` - }}{{ getStatusCodeReasonPhrase(response.statusCode) }} + }}{{ + getStatusCodeReasonPhrase( + response.statusCode, + response.statusText + ) + }} {{ t("response.time") }}: diff --git a/packages/hoppscotch-common/src/helpers/network.ts b/packages/hoppscotch-common/src/helpers/network.ts index 33d2f8d2f..6f70f9395 100644 --- a/packages/hoppscotch-common/src/helpers/network.ts +++ b/packages/hoppscotch-common/src/helpers/network.ts @@ -29,10 +29,10 @@ function processResponse( const contentLength = res.headers["content-length"] ? parseInt(res.headers["content-length"]) : (res.data as ArrayBuffer).byteLength - return { type: successState, statusCode: res.status, + statusText: res.statusText, body: res.data, headers: Object.keys(res.headers).map((x) => ({ key: x, diff --git a/packages/hoppscotch-common/src/helpers/types/HoppRESTResponse.ts b/packages/hoppscotch-common/src/helpers/types/HoppRESTResponse.ts index 0bc894342..f5032d4d2 100644 --- a/packages/hoppscotch-common/src/helpers/types/HoppRESTResponse.ts +++ b/packages/hoppscotch-common/src/helpers/types/HoppRESTResponse.ts @@ -10,7 +10,7 @@ export type HoppRESTResponse = headers: HoppRESTResponseHeader[] body: ArrayBuffer statusCode: number - + statusText: string meta: { responseSize: number // in bytes responseDuration: number // in millis @@ -33,6 +33,7 @@ export type HoppRESTResponse = headers: HoppRESTResponseHeader[] body: ArrayBuffer statusCode: number + statusText: string meta: { responseSize: number // in bytes responseDuration: number // in millis diff --git a/packages/hoppscotch-common/src/helpers/utils/statusCodes.ts b/packages/hoppscotch-common/src/helpers/utils/statusCodes.ts index e5f8996fd..34f1e12d2 100644 --- a/packages/hoppscotch-common/src/helpers/utils/statusCodes.ts +++ b/packages/hoppscotch-common/src/helpers/utils/statusCodes.ts @@ -85,6 +85,17 @@ const statusCodes: { 599: "Network connect timeout error", // (Unknown) This status code is not specified in any RFCs, but is used by Microsoft Corp. HTTP proxies to signal a network connect timeout behind the proxy to a client in front of the proxy. } -export function getStatusCodeReasonPhrase(code: number): string { +export function getStatusCodeReasonPhrase( + code: number, + statusText?: string +): string { + // Return statusText if non-empty after trimming and add ellipsis if greater than 35 characters + const trimmedStatusText = statusText?.trim() + if (trimmedStatusText) { + return trimmedStatusText.length > 35 + ? `${trimmedStatusText.substring(0, 35)}...` + : trimmedStatusText + } + return statusCodes[code] ?? "Unknown" } diff --git a/packages/hoppscotch-common/src/services/inspection/inspectors/__tests__/response.inspector.spec.ts b/packages/hoppscotch-common/src/services/inspection/inspectors/__tests__/response.inspector.spec.ts index 0f69a0d20..7ce9de007 100644 --- a/packages/hoppscotch-common/src/services/inspection/inspectors/__tests__/response.inspector.spec.ts +++ b/packages/hoppscotch-common/src/services/inspection/inspectors/__tests__/response.inspector.spec.ts @@ -157,6 +157,7 @@ describe("ResponseInspectorService", () => { headers: [], meta: { responseDuration: 0, responseSize: 0 }, req: req.value, + statusText: "", }) const result = responseInspector.getInspections(req, res) @@ -183,6 +184,7 @@ describe("ResponseInspectorService", () => { headers: [], meta: { responseDuration: 0, responseSize: 0 }, req: req.value, + statusText: "", }) const result = responseInspector.getInspections(req, res) @@ -209,6 +211,7 @@ describe("ResponseInspectorService", () => { headers: [], meta: { responseDuration: 0, responseSize: 0 }, req: req.value, + statusText: "", }) const result = responseInspector.getInspections(req, res) @@ -235,6 +238,7 @@ describe("ResponseInspectorService", () => { headers: [], meta: { responseDuration: 0, responseSize: 0 }, req: req.value, + statusText: "", }) const result = responseInspector.getInspections(req, res)