feat(js-sandbox): expose atob & btoa functions for Node.js (#3724)
Co-authored-by: jamesgeorge007 <jamesgeorge998001@gmail.com>
This commit is contained in:
@@ -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)
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user