From 736f83a70c9aaab78bd6d6d984fec8f2eeffb53b Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Tue, 7 Nov 2023 20:36:34 +0530 Subject: [PATCH] fix: header inspector cookie inspection will not trigger if current interceptor supports cookies --- .../__tests__/header.inspector.spec.ts | 44 +++++++++++++++++++ .../inspection/inspectors/header.inspector.ts | 7 ++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/packages/hoppscotch-common/src/services/inspection/inspectors/__tests__/header.inspector.spec.ts b/packages/hoppscotch-common/src/services/inspection/inspectors/__tests__/header.inspector.spec.ts index cd4c50e2d..db05fe573 100644 --- a/packages/hoppscotch-common/src/services/inspection/inspectors/__tests__/header.inspector.spec.ts +++ b/packages/hoppscotch-common/src/services/inspection/inspectors/__tests__/header.inspector.spec.ts @@ -4,6 +4,7 @@ import { HeaderInspectorService } from "../header.inspector" import { InspectionService } from "../../index" import { getDefaultRESTRequest } from "~/helpers/rest/default" import { ref } from "vue" +import { InterceptorService } from "~/services/interceptor.service" vi.mock("~/modules/i18n", () => ({ __esModule: true, @@ -58,5 +59,48 @@ describe("HeaderInspectorService", () => { expect(result.value).toHaveLength(0) }) + + it("should return an empty array when headers contain cookies but interceptor supports cookies", () => { + const container = new TestContainer() + + container.bindMock(InterceptorService, { + currentInterceptor: ref({ supportsCookies: true }) as any, + }) + + const headerInspector = container.bind(HeaderInspectorService) + + const req = ref({ + ...getDefaultRESTRequest(), + endpoint: "http://example.com/api/data", + headers: [{ key: "Cookie", value: "some-cookie", active: true }], + }) + + const result = headerInspector.getInspections(req) + + expect(result.value).toHaveLength(0) + }) + + it("should return an inspector result when headers contain cookies and the current interceptor doesn't support cookies", () => { + const container = new TestContainer() + + container.bindMock(InterceptorService, { + currentInterceptor: ref({ supportsCookies: false }) as any, + }) + + const headerInspector = container.bind(HeaderInspectorService) + + const req = ref({ + ...getDefaultRESTRequest(), + endpoint: "http://example.com/api/data", + headers: [{ key: "Cookie", value: "some-cookie", active: true }], + }) + + const result = headerInspector.getInspections(req) + + expect(result.value).not.toHaveLength(0) + expect(result.value).toContainEqual( + expect.objectContaining({ id: "header", isApplicable: true }) + ) + }) }) }) diff --git a/packages/hoppscotch-common/src/services/inspection/inspectors/header.inspector.ts b/packages/hoppscotch-common/src/services/inspection/inspectors/header.inspector.ts index 5ac612b50..3f8b893f3 100644 --- a/packages/hoppscotch-common/src/services/inspection/inspectors/header.inspector.ts +++ b/packages/hoppscotch-common/src/services/inspection/inspectors/header.inspector.ts @@ -4,6 +4,7 @@ import { getI18n } from "~/modules/i18n" import { HoppRESTRequest } from "@hoppscotch/data" import { Ref, computed, markRaw } from "vue" import IconAlertTriangle from "~icons/lucide/alert-triangle" +import { InterceptorService } from "~/services/interceptor.service" /** * This inspector is responsible for inspecting the header of a request. @@ -19,6 +20,7 @@ export class HeaderInspectorService extends Service implements Inspector { public readonly inspectorID = "header" private readonly inspection = this.bind(InspectionService) + private readonly interceptorService = this.bind(InterceptorService) constructor() { super() @@ -42,7 +44,10 @@ export class HeaderInspectorService extends Service implements Inspector { const isContainCookies = headerKeys.includes("Cookie") - if (isContainCookies) { + if ( + isContainCookies && + !this.interceptorService.currentInterceptor.value?.supportsCookies + ) { headerKeys.forEach((headerKey, index) => { if (this.cookiesCheck(headerKey)) { results.push({