Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia
TypeScript apiapi-clientapi-restapi-testingdeveloper-toolsgraphqlhttphttp-clientpwarestrest-apispatestingtesting-toolstoolsvuevuejswebsocket
feat: move testing code to js-sandbox
Andrew Bastin9454d8c100bb
diff --git a/packages/hoppscotch-app/components/http/Request.vue b/packages/hoppscotch-app/components/http/Request.vueindex f4b3b14cc..31c9c6f01 100644--- a/packages/hoppscotch-app/components/http/Request.vue+++ b/packages/hoppscotch-app/components/http/Request.vue@@ -232,6 +232,7 @@<script setup lang="ts">import { computed, ref, useContext, watch } from "@nuxtjs/composition-api"+import { isRight } from "fp-ts/lib/Either"import {updateRESTResponse,restEndpoint$,@@ -303,25 +304,30 @@ watch(loading, () => {}})-const newSendRequest = () => {+const newSendRequest = async () => {loading.value = true- subscribeToStream(- runRESTRequest$(),- (responseState) => {- if (loading.value) {- // Check exists because, loading can be set to false- // when cancelled- updateRESTResponse(responseState)+ const streamResult = await runRESTRequest$()++ // TODO: What if stream fetching failed (script execution errors ?) (isLeft)+ if (isRight(streamResult)) {+ subscribeToStream(+ streamResult.right,+ (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 = () => {diff --git a/packages/hoppscotch-app/helpers/RequestRunner.ts b/packages/hoppscotch-app/helpers/RequestRunner.tsindex 1b7019fb6..d4450c8ed 100644--- a/packages/hoppscotch-app/helpers/RequestRunner.ts+++ b/packages/hoppscotch-app/helpers/RequestRunner.ts@@ -1,155 +1,80 @@import { Observable } from "rxjs"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 { HoppRESTResponse } from "./types/HoppRESTResponse"import { createRESTNetworkRequestStream } from "./network"-import runTestScriptWithVariables, {- transformResponseForTesting,-} from "./postwomanTesting"import { HoppTestData, HoppTestResult } from "./types/HoppTestResult"+import { isJSONContentType } from "./utils/contenttypes"import { getRESTRequest, setRESTTestResults } from "~/newstore/RESTSession"-/**- * Runs a REST network request along with all the- * other side processes (like running test scripts)- */-export function runRESTRequest$(): Observable<HoppRESTResponse> {- const envs = getEnvironmentVariablesFromScript(- getRESTRequest().preRequestScript+const getTestableBody = (res: HoppRESTResponse & { type: "success" }) => {+ const contentTypeHeader = res.headers.find(+ (h) => h.value.toLowerCase() === "content-type")- const effectiveRequest = getEffectiveRESTRequest(getRESTRequest(), {- name: "Env",- variables: Object.keys(envs).map((key) => {- return {- key,- value: envs[key],- }- }),- })-- const stream = createRESTNetworkRequestStream(effectiveRequest)-- // Run Test Script when request ran successfully- 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+ const rawBody = new TextDecoder("utf-8").decode(res.body)- setRESTTestResults(translateToNewTestResults(testReport))-- subscription.unsubscribe()- })+ if (!contentTypeHeader || !isJSONContentType(contentTypeHeader.value))+ return rawBody- return stream+ return JSON.parse(rawBody)}-function isTestPass(x: any): x is {- result: "PASS"- styles: { icon: "check"; class: "success-response" }-} {- return x.result !== undefined && x.result === "PASS"-}--function isTestFail(x: any): x is {- result: "FAIL"- message: string- styles: { icon: "close"; class: "cl-error-response" }-} {- return x.result !== undefined && x.result === "FAIL"-}+export const runRESTRequest$: TaskEither<+ string,+ Observable<HoppRESTResponse>+> = pipe(+ getFinalEnvsFromPreRequest(),+ chain((envs) => {+ console.log(envs)+ const effectiveRequest = getEffectiveRESTRequest(getRESTRequest(), {+ name: "Env",+ variables: envs,+ })-function isStartBlock(- x: any-): x is { startBlock: string; styles: { icon: ""; class: "" } } {- return x.startBlock !== undefined-}+ const stream = createRESTNetworkRequestStream(effectiveRequest)-function isEndBlock(- x: any-): x is { endBlock: true; styles: { icon: ""; class: "" } } {- return x.endBlock !== undefined-}+ // Run Test Script when request ran successfully+ const subscription = stream+ .pipe(filter((res) => res.type === "success"))+ .subscribe(async (res) => {+ if (res.type === "success") {+ const runResult = await runTestScript(res.req.testScript, {+ status: res.statusCode,+ body: getTestableBody(res),+ headers: res.headers,+ })()-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: [],- },- ]+ // TODO: Handle script executation fails (isLeft)+ if (isRight(runResult)) {+ setRESTTestResults(translateToSandboxTestResults(runResult.right))+ }- 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- 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,+ subscription.unsubscribe()+ }})- } 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- if (testsStack.length !== 1) throw new Error("Invalid test result structure")+ return right(stream)+ })+)+function translateToSandboxTestResults(+ testDesc: TestDescriptor+): HoppTestResult {+ const translateChildTests = (child: TestDescriptor): HoppTestData => {+ return {+ description: child.descriptor,+ expectResults: child.expectResults,+ tests: child.children.map(translateChildTests),+ }+ }return {- expectResults: testsStack[0].expectResults,- tests: testsStack[0].tests,+ expectResults: testDesc.expectResults,+ tests: testDesc.children.map(translateChildTests),}}diff --git a/packages/hoppscotch-app/helpers/postwomanTesting.ts b/packages/hoppscotch-app/helpers/postwomanTesting.tsdeleted file mode 100644index 4057e905a..000000000--- a/packages/hoppscotch-app/helpers/postwomanTesting.ts+++ /dev/null@@ -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,- }-}diff --git a/packages/hoppscotch-app/helpers/preRequest.ts b/packages/hoppscotch-app/helpers/preRequest.tsindex 48ce28636..b5e53731b 100644--- a/packages/hoppscotch-app/helpers/preRequest.ts+++ b/packages/hoppscotch-app/helpers/preRequest.ts@@ -1,42 +1,31 @@+import { runPreRequestScript } from "@hoppscotch/js-sandbox"import {getCurrentEnvironment,getGlobalVariables,} from "~/newstore/environments"+import { getRESTRequest } from "~/newstore/RESTSession"-export default function getEnvironmentVariablesFromScript(script: string) {- const _variables: Record<string, string> = {}+export const getCombinedEnvVariables = () => {+ 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) {- _variables[variable.key] = variable.value- }-- const globalEnv = getGlobalVariables()-- if (globalEnv) {- for (const variable of globalEnv) {- _variables[variable.key] = variable.value+ if (index === -1) {+ variables.push({+ key: variable.key,+ value: variable.value,+ })+ } else {+ variables[index].value = variable.value}}- try {- // 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+ return variables}++export const getFinalEnvsFromPreRequest = () =>+ runPreRequestScript(+ getRESTRequest().preRequestScript,+ getCombinedEnvVariables()+ )