From 0028f6e878770b964b65854462d5fce924a94929 Mon Sep 17 00:00:00 2001 From: Florian Metz Date: Mon, 5 Feb 2024 18:42:55 +0100 Subject: [PATCH] feat(js-sandbox): expose `atob` & `btoa` functions for Node.js (#3724) Co-authored-by: jamesgeorge007 --- .../src/helpers/terndoc/ecma.json | 6 ++ .../testing/base64-helper-functions.spec.ts | 85 +++++++++++++++++++ .../src/pre-request/node-vm/index.ts | 2 + .../src/test-runner/node-vm/index.ts | 2 + 4 files changed, 95 insertions(+) create mode 100644 packages/hoppscotch-js-sandbox/src/__tests__/testing/base64-helper-functions.spec.ts diff --git a/packages/hoppscotch-common/src/helpers/terndoc/ecma.json b/packages/hoppscotch-common/src/helpers/terndoc/ecma.json index 9262de7d6..a916755ba 100644 --- a/packages/hoppscotch-common/src/helpers/terndoc/ecma.json +++ b/packages/hoppscotch-common/src/helpers/terndoc/ecma.json @@ -1256,5 +1256,11 @@ "!type": "fn(value: ?) -> bool" } } + }, + "btoa": { + "!type": "fn(data: string) -> string" + }, + "atob": { + "!type": "fn(data: string) -> string" } } diff --git a/packages/hoppscotch-js-sandbox/src/__tests__/testing/base64-helper-functions.spec.ts b/packages/hoppscotch-js-sandbox/src/__tests__/testing/base64-helper-functions.spec.ts new file mode 100644 index 000000000..8e54af0d9 --- /dev/null +++ b/packages/hoppscotch-js-sandbox/src/__tests__/testing/base64-helper-functions.spec.ts @@ -0,0 +1,85 @@ +import * as TE from "fp-ts/TaskEither" +import { pipe } from "fp-ts/function" + +import { runPreRequestScript } from "~/pre-request/node-vm" +import { runTestScript } from "~/test-runner/node-vm" +import { TestResponse, TestResult } from "~/types" + +describe("Base64 helper functions", () => { + const scriptExpectations = { + atob: { + script: `pw.env.set("atob", atob("SGVsbG8gV29ybGQ="))`, + environment: { + selected: [{ key: "atob", value: "Hello World" }], + }, + }, + btoa: { + script: `pw.env.set("btoa", btoa("Hello World"))`, + environment: { + selected: [{ key: "btoa", value: "SGVsbG8gV29ybGQ=" }], + }, + }, + } + + describe("Pre-request script", () => { + describe("atob", () => { + test("successfully decodes the input string", () => { + return expect( + runPreRequestScript(scriptExpectations.atob.script, { + global: [], + selected: [], + })() + ).resolves.toEqualRight( + expect.objectContaining(scriptExpectations.atob.environment) + ) + }) + }) + + describe("btoa", () => { + test("successfully encodes the input string", () => { + return expect( + runPreRequestScript(scriptExpectations.btoa.script, { + global: [], + selected: [], + })() + ).resolves.toEqualRight( + expect.objectContaining(scriptExpectations.btoa.environment) + ) + }) + }) + }) + + describe("Test script", () => { + const fakeResponse: TestResponse = { + status: 200, + body: "hoi", + headers: [], + } + + const func = (script: string, envs: TestResult["envs"]) => + pipe( + runTestScript(script, envs, fakeResponse), + TE.map((x) => x.envs) + ) + + describe("atob", () => { + test("successfully decodes the input string", () => { + return expect( + func(scriptExpectations.atob.script, { global: [], selected: [] })() + ).resolves.toEqualRight( + expect.objectContaining(scriptExpectations.atob.environment) + ) + }) + }) + + describe("btoa", () => { + test("successfully encodes the input string", () => { + return expect( + func(scriptExpectations.btoa.script, { global: [], selected: [] })() + ).resolves.toEqualRight( + expect.objectContaining(scriptExpectations.btoa.environment) + ) + }) + }) + }) +}) diff --git a/packages/hoppscotch-js-sandbox/src/pre-request/node-vm/index.ts b/packages/hoppscotch-js-sandbox/src/pre-request/node-vm/index.ts index f37b610cd..220a18d77 100644 --- a/packages/hoppscotch-js-sandbox/src/pre-request/node-vm/index.ts +++ b/packages/hoppscotch-js-sandbox/src/pre-request/node-vm/index.ts @@ -24,6 +24,8 @@ export const runPreRequestScript = ( // Expose pw to the context context.pw = pw + context.atob = atob + context.btoa = btoa // Run the pre-request script in the provided context runInContext(preRequestScript, context) diff --git a/packages/hoppscotch-js-sandbox/src/test-runner/node-vm/index.ts b/packages/hoppscotch-js-sandbox/src/test-runner/node-vm/index.ts index ce96acb13..89d5a8f68 100644 --- a/packages/hoppscotch-js-sandbox/src/test-runner/node-vm/index.ts +++ b/packages/hoppscotch-js-sandbox/src/test-runner/node-vm/index.ts @@ -43,6 +43,8 @@ const executeScriptInContext = ( // Expose pw to the context context.pw = { ...pw, response: responseObjHandle.right } + context.atob = atob + context.btoa = btoa // Run the test script in the provided context runInContext(testScript, context)