import { Environment, HoppRESTRequest } from "@hoppscotch/data"; import { EffectiveHoppRESTRequest } from "../../../interfaces/request"; import { HoppCLIError } from "../../../types/errors"; import { getEffectiveRESTRequest } from "../../../utils/pre-request"; import "@relmify/jest-fp-ts"; const DEFAULT_ENV = { name: "name", variables: [ { key: "HEADER", value: "parsed_header", }, { key: "PARAM", value: "parsed_param" }, { key: "TOKEN", value: "parsed_token" }, { key: "BODY_PROP", value: "parsed_body_prop" }, { key: "ENDPOINT", value: "https://parsed-endpoint.com" }, ], }; const DEFAULT_REQUEST = { v: "1", name: "name", method: "GET", endpoint: "https://example.com", params: [], headers: [], preRequestScript: "", testScript: "", auth: { authActive: false, authType: "none", }, body: { contentType: null, body: null, }, }; describe("getEffectiveRESTRequest", () => { let SAMPLE_REQUEST = Object.assign({}, DEFAULT_REQUEST); beforeEach(() => { SAMPLE_REQUEST = Object.assign({}, DEFAULT_REQUEST); }); test("Endpoint, headers and params with unavailable ENV.", () => { SAMPLE_REQUEST.headers = [ { key: "HEADER", value: "<>", active: true, }, ]; SAMPLE_REQUEST.params = [ { key: "PARAM", value: "<>", active: true, }, ]; SAMPLE_REQUEST.endpoint = "<>"; expect( getEffectiveRESTRequest(SAMPLE_REQUEST, DEFAULT_ENV) ).toSubsetEqualRight({ effectiveFinalHeaders: [{ active: true, key: "HEADER", value: "" }], effectiveFinalParams: [{ active: true, key: "PARAM", value: "" }], effectiveFinalURL: "", }); }); test("Auth with unavailable ENV.", () => { SAMPLE_REQUEST.auth = { authActive: true, authType: "bearer", token: "<>", }; expect( getEffectiveRESTRequest(SAMPLE_REQUEST, DEFAULT_ENV) ).toSubsetEqualRight({ effectiveFinalHeaders: [ { active: true, key: "Authorization", value: "Bearer " }, ], }); }); test("Body with unavailable ENV.", () => { SAMPLE_REQUEST.body = { contentType: "text/plain", body: "<>", }; expect( getEffectiveRESTRequest(SAMPLE_REQUEST, DEFAULT_ENV) ).toSubsetEqualLeft({ code: "PARSING_ERROR", }); }); test("Request meta-data with available ENVs.", () => { SAMPLE_REQUEST.headers = [ { key: "HEADER", value: "<
>", active: true, }, ]; SAMPLE_REQUEST.params = [ { key: "PARAM", value: "<>", active: true, }, ]; SAMPLE_REQUEST.endpoint = "<>"; SAMPLE_REQUEST.auth = { authActive: true, authType: "bearer", token: "<>", }; SAMPLE_REQUEST.body = { contentType: "text/plain", body: "<>", }; const vars = DEFAULT_ENV.variables; expect( getEffectiveRESTRequest(SAMPLE_REQUEST, DEFAULT_ENV) ).toSubsetEqualRight({ effectiveFinalHeaders: [ { active: true, key: "HEADER", value: vars[0].value }, { active: true, key: "Authorization", value: `Bearer ${vars[2].value}`, }, { active: true, key: "content-type", value: "text/plain" }, ], effectiveFinalParams: [ { active: true, key: "PARAM", value: vars[1].value }, ], effectiveFinalURL: vars[4].value, effectiveFinalBody: vars[3].value, }); }); });