feat: move testing code to js-sandbox
This commit is contained in:
@@ -232,6 +232,7 @@
|
|||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { computed, ref, useContext, watch } from "@nuxtjs/composition-api"
|
import { computed, ref, useContext, watch } from "@nuxtjs/composition-api"
|
||||||
|
import { isRight } from "fp-ts/lib/Either"
|
||||||
import {
|
import {
|
||||||
updateRESTResponse,
|
updateRESTResponse,
|
||||||
restEndpoint$,
|
restEndpoint$,
|
||||||
@@ -303,25 +304,30 @@ watch(loading, () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const newSendRequest = () => {
|
const newSendRequest = async () => {
|
||||||
loading.value = true
|
loading.value = true
|
||||||
|
|
||||||
subscribeToStream(
|
const streamResult = await runRESTRequest$()
|
||||||
runRESTRequest$(),
|
|
||||||
(responseState) => {
|
// TODO: What if stream fetching failed (script execution errors ?) (isLeft)
|
||||||
if (loading.value) {
|
if (isRight(streamResult)) {
|
||||||
// Check exists because, loading can be set to false
|
subscribeToStream(
|
||||||
// when cancelled
|
streamResult.right,
|
||||||
updateRESTResponse(responseState)
|
(responseState) => {
|
||||||
|
if (loading.value) {
|
||||||
|
// Check exists because, loading can be set to false
|
||||||
|
// when cancelled
|
||||||
|
updateRESTResponse(responseState)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
() => {
|
||||||
|
loading.value = false
|
||||||
|
},
|
||||||
|
() => {
|
||||||
|
loading.value = false
|
||||||
}
|
}
|
||||||
},
|
)
|
||||||
() => {
|
}
|
||||||
loading.value = false
|
|
||||||
},
|
|
||||||
() => {
|
|
||||||
loading.value = false
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const cancelRequest = () => {
|
const cancelRequest = () => {
|
||||||
|
|||||||
@@ -1,155 +1,80 @@
|
|||||||
import { Observable } from "rxjs"
|
import { Observable } from "rxjs"
|
||||||
import { filter } from "rxjs/operators"
|
import { filter } from "rxjs/operators"
|
||||||
import getEnvironmentVariablesFromScript from "./preRequest"
|
import { chain, right, TaskEither } from "fp-ts/lib/TaskEither"
|
||||||
|
import { pipe } from "fp-ts/lib/function"
|
||||||
|
import { runTestScript, TestDescriptor } from "@hoppscotch/js-sandbox"
|
||||||
|
import { isRight } from "fp-ts/lib/Either"
|
||||||
|
import { getFinalEnvsFromPreRequest } from "./preRequest"
|
||||||
import { getEffectiveRESTRequest } from "./utils/EffectiveURL"
|
import { getEffectiveRESTRequest } from "./utils/EffectiveURL"
|
||||||
import { HoppRESTResponse } from "./types/HoppRESTResponse"
|
import { HoppRESTResponse } from "./types/HoppRESTResponse"
|
||||||
import { createRESTNetworkRequestStream } from "./network"
|
import { createRESTNetworkRequestStream } from "./network"
|
||||||
import runTestScriptWithVariables, {
|
|
||||||
transformResponseForTesting,
|
|
||||||
} from "./postwomanTesting"
|
|
||||||
import { HoppTestData, HoppTestResult } from "./types/HoppTestResult"
|
import { HoppTestData, HoppTestResult } from "./types/HoppTestResult"
|
||||||
|
import { isJSONContentType } from "./utils/contenttypes"
|
||||||
import { getRESTRequest, setRESTTestResults } from "~/newstore/RESTSession"
|
import { getRESTRequest, setRESTTestResults } from "~/newstore/RESTSession"
|
||||||
|
|
||||||
/**
|
const getTestableBody = (res: HoppRESTResponse & { type: "success" }) => {
|
||||||
* Runs a REST network request along with all the
|
const contentTypeHeader = res.headers.find(
|
||||||
* other side processes (like running test scripts)
|
(h) => h.value.toLowerCase() === "content-type"
|
||||||
*/
|
|
||||||
export function runRESTRequest$(): Observable<HoppRESTResponse> {
|
|
||||||
const envs = getEnvironmentVariablesFromScript(
|
|
||||||
getRESTRequest().preRequestScript
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const effectiveRequest = getEffectiveRESTRequest(getRESTRequest(), {
|
const rawBody = new TextDecoder("utf-8").decode(res.body)
|
||||||
name: "Env",
|
|
||||||
variables: Object.keys(envs).map((key) => {
|
|
||||||
return {
|
|
||||||
key,
|
|
||||||
value: envs[key],
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
|
|
||||||
const stream = createRESTNetworkRequestStream(effectiveRequest)
|
if (!contentTypeHeader || !isJSONContentType(contentTypeHeader.value))
|
||||||
|
return rawBody
|
||||||
|
|
||||||
// Run Test Script when request ran successfully
|
return JSON.parse(rawBody)
|
||||||
const subscription = stream
|
}
|
||||||
.pipe(filter((res) => res.type === "success"))
|
|
||||||
.subscribe((res) => {
|
|
||||||
const testReport: {
|
|
||||||
report: "" // ¯\_(ツ)_/¯
|
|
||||||
testResults: Array<
|
|
||||||
| {
|
|
||||||
result: "FAIL"
|
|
||||||
message: string
|
|
||||||
styles: { icon: "close"; class: "cl-error-response" }
|
|
||||||
}
|
|
||||||
| {
|
|
||||||
result: "PASS"
|
|
||||||
message: string
|
|
||||||
styles: { icon: "check"; class: "success-response" }
|
|
||||||
}
|
|
||||||
| { startBlock: string; styles: { icon: ""; class: "" } }
|
|
||||||
| { endBlock: true; styles: { icon: ""; class: "" } }
|
|
||||||
>
|
|
||||||
errors: [] // ¯\_(ツ)_/¯
|
|
||||||
} = runTestScriptWithVariables(effectiveRequest.testScript, {
|
|
||||||
response: transformResponseForTesting(res),
|
|
||||||
}) as any
|
|
||||||
|
|
||||||
setRESTTestResults(translateToNewTestResults(testReport))
|
export const runRESTRequest$: TaskEither<
|
||||||
|
string,
|
||||||
subscription.unsubscribe()
|
Observable<HoppRESTResponse>
|
||||||
|
> = pipe(
|
||||||
|
getFinalEnvsFromPreRequest(),
|
||||||
|
chain((envs) => {
|
||||||
|
console.log(envs)
|
||||||
|
const effectiveRequest = getEffectiveRESTRequest(getRESTRequest(), {
|
||||||
|
name: "Env",
|
||||||
|
variables: envs,
|
||||||
})
|
})
|
||||||
|
|
||||||
return stream
|
const stream = createRESTNetworkRequestStream(effectiveRequest)
|
||||||
}
|
|
||||||
|
|
||||||
function isTestPass(x: any): x is {
|
// Run Test Script when request ran successfully
|
||||||
result: "PASS"
|
const subscription = stream
|
||||||
styles: { icon: "check"; class: "success-response" }
|
.pipe(filter((res) => res.type === "success"))
|
||||||
} {
|
.subscribe(async (res) => {
|
||||||
return x.result !== undefined && x.result === "PASS"
|
if (res.type === "success") {
|
||||||
}
|
const runResult = await runTestScript(res.req.testScript, {
|
||||||
|
status: res.statusCode,
|
||||||
|
body: getTestableBody(res),
|
||||||
|
headers: res.headers,
|
||||||
|
})()
|
||||||
|
|
||||||
function isTestFail(x: any): x is {
|
// TODO: Handle script executation fails (isLeft)
|
||||||
result: "FAIL"
|
if (isRight(runResult)) {
|
||||||
message: string
|
setRESTTestResults(translateToSandboxTestResults(runResult.right))
|
||||||
styles: { icon: "close"; class: "cl-error-response" }
|
}
|
||||||
} {
|
|
||||||
return x.result !== undefined && x.result === "FAIL"
|
|
||||||
}
|
|
||||||
|
|
||||||
function isStartBlock(
|
subscription.unsubscribe()
|
||||||
x: any
|
}
|
||||||
): x is { startBlock: string; styles: { icon: ""; class: "" } } {
|
|
||||||
return x.startBlock !== undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
function isEndBlock(
|
|
||||||
x: any
|
|
||||||
): x is { endBlock: true; styles: { icon: ""; class: "" } } {
|
|
||||||
return x.endBlock !== undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
function translateToNewTestResults(testReport: {
|
|
||||||
report: "" // ¯\_(ツ)_/¯
|
|
||||||
testResults: Array<
|
|
||||||
| {
|
|
||||||
result: "FAIL"
|
|
||||||
message: string
|
|
||||||
styles: { icon: "close"; class: "cl-error-response" }
|
|
||||||
}
|
|
||||||
| {
|
|
||||||
result: "PASS"
|
|
||||||
message: string
|
|
||||||
styles: { icon: "check"; class: "success-response" }
|
|
||||||
}
|
|
||||||
| { startBlock: string; styles: { icon: ""; class: "" } }
|
|
||||||
| { endBlock: true; styles: { icon: ""; class: "" } }
|
|
||||||
>
|
|
||||||
errors: [] // ¯\_(ツ)_/¯
|
|
||||||
}): HoppTestResult {
|
|
||||||
// Build a stack of test data which we eventually build up based on the results
|
|
||||||
const testsStack: HoppTestData[] = [
|
|
||||||
{
|
|
||||||
description: "root",
|
|
||||||
tests: [],
|
|
||||||
expectResults: [],
|
|
||||||
},
|
|
||||||
]
|
|
||||||
|
|
||||||
testReport.testResults.forEach((result) => {
|
|
||||||
// This is a test block start, push an empty test to the stack
|
|
||||||
if (isStartBlock(result)) {
|
|
||||||
testsStack.push({
|
|
||||||
description: result.startBlock,
|
|
||||||
tests: [],
|
|
||||||
expectResults: [],
|
|
||||||
})
|
})
|
||||||
} else if (isEndBlock(result)) {
|
|
||||||
// End of the block, pop the stack and add it as a child to the current stack top
|
return right(stream)
|
||||||
const testData = testsStack.pop()!
|
|
||||||
testsStack[testsStack.length - 1].tests.push(testData)
|
|
||||||
} else if (isTestPass(result)) {
|
|
||||||
// A normal PASS expectation
|
|
||||||
testsStack[testsStack.length - 1].expectResults.push({
|
|
||||||
status: "pass",
|
|
||||||
message: result.message,
|
|
||||||
})
|
|
||||||
} else if (isTestFail(result)) {
|
|
||||||
// A normal FAIL expectation
|
|
||||||
testsStack[testsStack.length - 1].expectResults.push({
|
|
||||||
status: "fail",
|
|
||||||
message: result.message,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
)
|
||||||
|
|
||||||
// We should end up with only the root stack entry
|
function translateToSandboxTestResults(
|
||||||
if (testsStack.length !== 1) throw new Error("Invalid test result structure")
|
testDesc: TestDescriptor
|
||||||
|
): HoppTestResult {
|
||||||
|
const translateChildTests = (child: TestDescriptor): HoppTestData => {
|
||||||
|
return {
|
||||||
|
description: child.descriptor,
|
||||||
|
expectResults: child.expectResults,
|
||||||
|
tests: child.children.map(translateChildTests),
|
||||||
|
}
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
expectResults: testsStack[0].expectResults,
|
expectResults: testDesc.expectResults,
|
||||||
tests: testsStack[0].tests,
|
tests: testDesc.children.map(translateChildTests),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,321 +0,0 @@
|
|||||||
import { HoppRESTResponse } from "./types/HoppRESTResponse"
|
|
||||||
|
|
||||||
const styles = {
|
|
||||||
PASS: { icon: "check", class: "success-response" },
|
|
||||||
FAIL: { icon: "close", class: "cl-error-response" },
|
|
||||||
ERROR: { icon: "close", class: "cl-error-response" },
|
|
||||||
}
|
|
||||||
|
|
||||||
type TestScriptResponse = {
|
|
||||||
body: any
|
|
||||||
headers: any[]
|
|
||||||
status: number
|
|
||||||
|
|
||||||
__newRes: HoppRESTResponse
|
|
||||||
}
|
|
||||||
|
|
||||||
type TestScriptVariables = {
|
|
||||||
response: TestScriptResponse
|
|
||||||
}
|
|
||||||
|
|
||||||
type TestReportStartBlock = {
|
|
||||||
startBlock: string
|
|
||||||
}
|
|
||||||
|
|
||||||
type TestReportEndBlock = {
|
|
||||||
endBlock: true
|
|
||||||
}
|
|
||||||
|
|
||||||
type TestReportEntry = {
|
|
||||||
result: "PASS" | "FAIL" | "ERROR"
|
|
||||||
message: string
|
|
||||||
styles: {
|
|
||||||
icon: string
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type TestReport = TestReportStartBlock | TestReportEntry | TestReportEndBlock
|
|
||||||
|
|
||||||
export default function runTestScriptWithVariables(
|
|
||||||
script: string,
|
|
||||||
variables?: TestScriptVariables
|
|
||||||
) {
|
|
||||||
const pw = {
|
|
||||||
_errors: [],
|
|
||||||
_testReports: [] as TestReport[],
|
|
||||||
_report: "",
|
|
||||||
expect(value: any) {
|
|
||||||
try {
|
|
||||||
return expect(value, this._testReports)
|
|
||||||
} catch (e: any) {
|
|
||||||
pw._testReports.push({
|
|
||||||
result: "ERROR",
|
|
||||||
message: `${e}`,
|
|
||||||
styles: styles.ERROR,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
|
||||||
test: (descriptor: string, func: () => void) =>
|
|
||||||
test(descriptor, func, pw._testReports),
|
|
||||||
// globals that the script is allowed to have access to.
|
|
||||||
}
|
|
||||||
Object.assign(pw, variables)
|
|
||||||
|
|
||||||
// run pre-request script within this function so that it has access to the pw object.
|
|
||||||
// eslint-disable-next-line no-new-func
|
|
||||||
new Function("pw", script)(pw)
|
|
||||||
|
|
||||||
return {
|
|
||||||
report: pw._report,
|
|
||||||
errors: pw._errors,
|
|
||||||
testResults: pw._testReports,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function test(
|
|
||||||
descriptor: string,
|
|
||||||
func: () => void,
|
|
||||||
_testReports: TestReport[]
|
|
||||||
) {
|
|
||||||
_testReports.push({ startBlock: descriptor })
|
|
||||||
try {
|
|
||||||
func()
|
|
||||||
} catch (e: any) {
|
|
||||||
_testReports.push({ result: "ERROR", message: e, styles: styles.ERROR })
|
|
||||||
}
|
|
||||||
_testReports.push({ endBlock: true })
|
|
||||||
|
|
||||||
// TODO: Organize and generate text report of each {descriptor: true} section in testReports.
|
|
||||||
// add checkmark or x depending on if each testReport is pass=true or pass=false
|
|
||||||
}
|
|
||||||
|
|
||||||
function expect(expectValue: any, _testReports: TestReport[]) {
|
|
||||||
return new Expectation(expectValue, null, _testReports)
|
|
||||||
}
|
|
||||||
|
|
||||||
class Expectation {
|
|
||||||
private expectValue: any
|
|
||||||
private not: true | Expectation
|
|
||||||
private _testReports: TestReport[]
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
expectValue: any,
|
|
||||||
_not: boolean | null,
|
|
||||||
_testReports: TestReport[]
|
|
||||||
) {
|
|
||||||
this.expectValue = expectValue
|
|
||||||
this.not = _not || new Expectation(this.expectValue, true, _testReports)
|
|
||||||
this._testReports = _testReports // this values is used within Test.it, which wraps Expectation and passes _testReports value.
|
|
||||||
}
|
|
||||||
|
|
||||||
private _satisfies(expectValue: any, targetValue?: any): boolean {
|
|
||||||
// Used for testing if two values match the expectation, which could be === OR !==, depending on if not
|
|
||||||
// was used. Expectation#_satisfies prevents the need to have an if(this.not) branch in every test method.
|
|
||||||
// Signature is _satisfies([expectValue,] targetValue): if only one argument is given, it is assumed the targetValue, and expectValue is set to this.expectValue
|
|
||||||
if (!targetValue) {
|
|
||||||
targetValue = expectValue
|
|
||||||
expectValue = this.expectValue
|
|
||||||
}
|
|
||||||
if (this.not === true) {
|
|
||||||
// test the inverse. this.not is always truthly, but an Expectation that is inverted will always be strictly `true`
|
|
||||||
return expectValue !== targetValue
|
|
||||||
} else {
|
|
||||||
return expectValue === targetValue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_fmtNot(message: string) {
|
|
||||||
// given a string with "(not)" in it, replaces with "not" or "", depending if the expectation is expecting the positive or inverse (this._not)
|
|
||||||
if (this.not === true) {
|
|
||||||
return message.replace("(not)", "not ")
|
|
||||||
} else {
|
|
||||||
return message.replace("(not)", "")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_fail(message: string) {
|
|
||||||
return this._testReports.push({
|
|
||||||
result: "FAIL",
|
|
||||||
message,
|
|
||||||
styles: styles.FAIL,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
_pass(message: string) {
|
|
||||||
return this._testReports.push({
|
|
||||||
result: "PASS",
|
|
||||||
message,
|
|
||||||
styles: styles.PASS,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// TEST METHODS DEFINED BELOW
|
|
||||||
// these are the usual methods that would follow expect(...)
|
|
||||||
toBe(value: any) {
|
|
||||||
return this._satisfies(value)
|
|
||||||
? this._pass(
|
|
||||||
this._fmtNot(`${this.expectValue} do (not)match with ${value}`)
|
|
||||||
)
|
|
||||||
: this._fail(
|
|
||||||
this._fmtNot(`Expected ${this.expectValue} (not)to be ${value}`)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
toHaveProperty(value: string) {
|
|
||||||
return this._satisfies(
|
|
||||||
Object.prototype.hasOwnProperty.call(this.expectValue, value),
|
|
||||||
true
|
|
||||||
)
|
|
||||||
? this._pass(
|
|
||||||
this._fmtNot(`${this.expectValue} do (not)have property ${value}`)
|
|
||||||
)
|
|
||||||
: this._fail(
|
|
||||||
this._fmtNot(
|
|
||||||
`Expected object ${this.expectValue} to (not)have property ${value}`
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
toBeLevel2xx() {
|
|
||||||
const code = parseInt(this.expectValue, 10)
|
|
||||||
if (Number.isNaN(code)) {
|
|
||||||
return this._fail(
|
|
||||||
`Expected 200-level status but could not parse value ${this.expectValue}`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return this._satisfies(code >= 200 && code < 300, true)
|
|
||||||
? this._pass(
|
|
||||||
this._fmtNot(`${this.expectValue} is (not)a 200-level status`)
|
|
||||||
)
|
|
||||||
: this._fail(
|
|
||||||
this._fmtNot(
|
|
||||||
`Expected ${this.expectValue} to (not)be 200-level status`
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
toBeLevel3xx() {
|
|
||||||
const code = parseInt(this.expectValue, 10)
|
|
||||||
if (Number.isNaN(code)) {
|
|
||||||
return this._fail(
|
|
||||||
`Expected 300-level status but could not parse value ${this.expectValue}`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return this._satisfies(code >= 300 && code < 400, true)
|
|
||||||
? this._pass(
|
|
||||||
this._fmtNot(`${this.expectValue} is (not)a 300-level status`)
|
|
||||||
)
|
|
||||||
: this._fail(
|
|
||||||
this._fmtNot(
|
|
||||||
`Expected ${this.expectValue} to (not)be 300-level status`
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
toBeLevel4xx() {
|
|
||||||
const code = parseInt(this.expectValue, 10)
|
|
||||||
if (Number.isNaN(code)) {
|
|
||||||
return this._fail(
|
|
||||||
`Expected 400-level status but could not parse value ${this.expectValue}`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return this._satisfies(code >= 400 && code < 500, true)
|
|
||||||
? this._pass(
|
|
||||||
this._fmtNot(`${this.expectValue} is (not)a 400-level status`)
|
|
||||||
)
|
|
||||||
: this._fail(
|
|
||||||
this._fmtNot(
|
|
||||||
`Expected ${this.expectValue} to (not)be 400-level status`
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
toBeLevel5xx() {
|
|
||||||
const code = parseInt(this.expectValue, 10)
|
|
||||||
if (Number.isNaN(code)) {
|
|
||||||
return this._fail(
|
|
||||||
`Expected 500-level status but could not parse value ${this.expectValue}`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return this._satisfies(code >= 500 && code < 600, true)
|
|
||||||
? this._pass(
|
|
||||||
this._fmtNot(`${this.expectValue} is (not)a 500-level status`)
|
|
||||||
)
|
|
||||||
: this._fail(
|
|
||||||
this._fmtNot(
|
|
||||||
`Expected ${this.expectValue} to (not)be 500-level status`
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
toHaveLength(expectedLength: number) {
|
|
||||||
const actualLength = this.expectValue.length
|
|
||||||
return this._satisfies(actualLength, expectedLength)
|
|
||||||
? this._pass(
|
|
||||||
this._fmtNot(
|
|
||||||
`Length expectation of (not)being ${expectedLength} is kept`
|
|
||||||
)
|
|
||||||
)
|
|
||||||
: this._fail(
|
|
||||||
this._fmtNot(
|
|
||||||
`Expected length to be ${expectedLength} but actual length was ${actualLength}`
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
toBeType(expectedType: string) {
|
|
||||||
const actualType = typeof this.expectValue
|
|
||||||
if (
|
|
||||||
![
|
|
||||||
"string",
|
|
||||||
"boolean",
|
|
||||||
"number",
|
|
||||||
"object",
|
|
||||||
"undefined",
|
|
||||||
"bigint",
|
|
||||||
"symbol",
|
|
||||||
"function",
|
|
||||||
].includes(expectedType)
|
|
||||||
) {
|
|
||||||
return this._fail(
|
|
||||||
this._fmtNot(
|
|
||||||
`Argument for toBeType should be "string", "boolean", "number", "object", "undefined", "bigint", "symbol" or "function"`
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return this._satisfies(actualType, expectedType)
|
|
||||||
? this._pass(this._fmtNot(`The type is (not)"${expectedType}"`))
|
|
||||||
: this._fail(
|
|
||||||
this._fmtNot(
|
|
||||||
`Expected type to be "${expectedType}" but actual type was "${actualType}"`
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function transformResponseForTesting(
|
|
||||||
response: HoppRESTResponse
|
|
||||||
): TestScriptResponse {
|
|
||||||
if (response.type === "loading") {
|
|
||||||
throw new Error("Cannot transform loading responses")
|
|
||||||
}
|
|
||||||
|
|
||||||
if (response.type === "network_fail") {
|
|
||||||
throw new Error("Cannot transform failed responses")
|
|
||||||
}
|
|
||||||
|
|
||||||
let body: any = new TextDecoder("utf-8").decode(response.body)
|
|
||||||
|
|
||||||
// Try parsing to JSON
|
|
||||||
try {
|
|
||||||
body = JSON.parse(body)
|
|
||||||
} catch (_) {}
|
|
||||||
|
|
||||||
return {
|
|
||||||
body,
|
|
||||||
headers: response.headers,
|
|
||||||
status: response.statusCode,
|
|
||||||
|
|
||||||
__newRes: response,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,42 +1,31 @@
|
|||||||
|
import { runPreRequestScript } from "@hoppscotch/js-sandbox"
|
||||||
import {
|
import {
|
||||||
getCurrentEnvironment,
|
getCurrentEnvironment,
|
||||||
getGlobalVariables,
|
getGlobalVariables,
|
||||||
} from "~/newstore/environments"
|
} from "~/newstore/environments"
|
||||||
|
import { getRESTRequest } from "~/newstore/RESTSession"
|
||||||
|
|
||||||
export default function getEnvironmentVariablesFromScript(script: string) {
|
export const getCombinedEnvVariables = () => {
|
||||||
const _variables: Record<string, string> = {}
|
const variables: { key: string; value: string }[] = [...getGlobalVariables()]
|
||||||
|
|
||||||
const currentEnv = getCurrentEnvironment()
|
for (const variable of getCurrentEnvironment().variables) {
|
||||||
|
const index = variables.findIndex((v) => variable.key === v.key)
|
||||||
|
|
||||||
for (const variable of currentEnv.variables) {
|
if (index === -1) {
|
||||||
_variables[variable.key] = variable.value
|
variables.push({
|
||||||
}
|
key: variable.key,
|
||||||
|
value: variable.value,
|
||||||
const globalEnv = getGlobalVariables()
|
})
|
||||||
|
} else {
|
||||||
if (globalEnv) {
|
variables[index].value = variable.value
|
||||||
for (const variable of globalEnv) {
|
|
||||||
_variables[variable.key] = variable.value
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
return variables
|
||||||
// the pw object is the proxy by which pre-request scripts can pass variables to the request.
|
|
||||||
// for security and control purposes, this is the only way a pre-request script should modify variables.
|
|
||||||
const pw = {
|
|
||||||
environment: {
|
|
||||||
set: (key: string, value: string) => (_variables[key] = value),
|
|
||||||
},
|
|
||||||
env: {
|
|
||||||
set: (key: string, value: string) => (_variables[key] = value),
|
|
||||||
},
|
|
||||||
// globals that the script is allowed to have access to.
|
|
||||||
}
|
|
||||||
|
|
||||||
// run pre-request script within this function so that it has access to the pw object.
|
|
||||||
// eslint-disable-next-line no-new-func
|
|
||||||
new Function("pw", script)(pw)
|
|
||||||
} catch (_e) {}
|
|
||||||
|
|
||||||
return _variables
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const getFinalEnvsFromPreRequest = () =>
|
||||||
|
runPreRequestScript(
|
||||||
|
getRESTRequest().preRequestScript,
|
||||||
|
getCombinedEnvVariables()
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user