feat(js-sandbox): expose atob & btoa functions for Node.js (#3724)
Co-authored-by: jamesgeorge007 <jamesgeorge998001@gmail.com>
This commit is contained in:
@@ -1256,5 +1256,11 @@
|
||||
"!type": "fn(value: ?) -> bool"
|
||||
}
|
||||
}
|
||||
},
|
||||
"btoa": {
|
||||
"!type": "fn(data: string) -> string"
|
||||
},
|
||||
"atob": {
|
||||
"!type": "fn(data: string) -> string"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user