From ebdffad85a54a5981797cb227d93add3d2ed69c1 Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Mon, 17 Jun 2024 01:27:27 +0530 Subject: [PATCH] feat: pre-processing of test requests --- tests/callRequests.ts | 58 +++++++++++++++++++++++++++++++++++++ tests/getResponses.ts | 9 ++++++ tests/utils/bundleUtils.ts | 26 +++++++++++++++++ tests/utils/requestUtils.ts | 21 ++++++++++++++ tests/utils/varUtils.ts | 25 ++++++++++++++++ 5 files changed, 139 insertions(+) create mode 100644 tests/callRequests.ts create mode 100644 tests/getResponses.ts create mode 100644 tests/utils/bundleUtils.ts create mode 100644 tests/utils/requestUtils.ts create mode 100644 tests/utils/varUtils.ts diff --git a/tests/callRequests.ts b/tests/callRequests.ts new file mode 100644 index 0000000..f53d5d4 --- /dev/null +++ b/tests/callRequests.ts @@ -0,0 +1,58 @@ +import * as path from "path"; + +import { RequestSpec, Variables } from "../src/index"; +import { getAllRequestSpecs, getRequestSpec } from "../src/index"; +import { loadVariables } from "../src/index"; + +import { RawRequest } from "./utils/requestUtils"; +import { getVarFileContents } from "./utils/varUtils"; + +import { runRequestTests } from "./getResponses"; + +async function runRequestSpecs( + requests: { [name: string]: RequestSpec }, + rawRequest: RawRequest, +): Promise { + for (const name in requests) { + const request = requests[name]; + + const autoHeaders: { [key: string]: string } = { "user-agent": "zzAPI-cli/" + CLI_VERSION }; + if (request.httpRequest.body && typeof request.httpRequest.body == "object") + autoHeaders["content-type"] = "application/json"; + + request.httpRequest.headers = Object.assign(autoHeaders, request.httpRequest.headers); + } + + await runRequestTests(requests, rawRequest); +} + +export async function callRequests(request: RawRequest): Promise { + try { + // load the variables + const env = request.envName; + const loadedVariables: Variables = loadVariables( + env, + request.bundle.bundleContents, + getVarFileContents(path.dirname(request.bundle.bundlePath)), + ); + if (env && Object.keys(loadedVariables).length < 1) + console.log(`warning: no variables added from env: ${env}`); + request.variables.setLoadedVariables(loadedVariables); + } catch (err: any) { + throw err; + } + + // create the request specs + const name = request.requestName, + content = request.bundle.bundleContents; + + let allRequests: { [name: string]: RequestSpec }; + try { + allRequests = name ? { [name]: getRequestSpec(content, name) } : getAllRequestSpecs(content); + } catch (err: any) { + throw err; + } + + // finally, run the request specs + await runRequestSpecs(allRequests, request); +} diff --git a/tests/getResponses.ts b/tests/getResponses.ts new file mode 100644 index 0000000..224368f --- /dev/null +++ b/tests/getResponses.ts @@ -0,0 +1,9 @@ +import { RequestSpec, ResponseData } from "../src"; +import { RawRequest } from "./utils/requestUtils"; + +export async function runRequestTests( + requests: { [name: string]: RequestSpec }, + rawReq: RawRequest, +): Promise { + +} diff --git a/tests/utils/bundleUtils.ts b/tests/utils/bundleUtils.ts new file mode 100644 index 0000000..282e114 --- /dev/null +++ b/tests/utils/bundleUtils.ts @@ -0,0 +1,26 @@ +import path from "path"; +import * as fs from "fs"; + +export class Bundle { + public bundlePath: string = __dirname; + public bundleContents: string = ""; + + constructor(relPath: string) { + try { + this.setBundlePath(relPath); + this.readContents(); + } catch (e) { + throw e; + } + } + + setBundlePath(relPath: string) { + this.bundlePath = path.resolve(relPath); + if (!fs.existsSync(this.bundlePath)) throw `error: ${this.bundlePath} does not exist`; + if (!fs.lstatSync(this.bundlePath).isFile()) throw `error: ${this.bundlePath} is not a file`; + } + + readContents() { + this.bundleContents = fs.readFileSync(this.bundlePath, "utf-8"); + } +} diff --git a/tests/utils/requestUtils.ts b/tests/utils/requestUtils.ts new file mode 100644 index 0000000..9f52ac2 --- /dev/null +++ b/tests/utils/requestUtils.ts @@ -0,0 +1,21 @@ +import { VarStore } from "../../src/index"; + +import { Bundle } from "./bundleUtils"; + +export class RawRequest { + public requestName: string | undefined = undefined; + public envName: string | undefined = undefined; + public bundle: Bundle; + public variables: VarStore; + + constructor(relPath: string, envName: string | undefined, reqName?: string) { + try { + this.bundle = new Bundle(relPath); + this.requestName = reqName; + this.envName = envName; + this.variables = new VarStore(); + } catch (e) { + throw e; + } + } +} diff --git a/tests/utils/varUtils.ts b/tests/utils/varUtils.ts new file mode 100644 index 0000000..c81dab0 --- /dev/null +++ b/tests/utils/varUtils.ts @@ -0,0 +1,25 @@ +import * as fs from "fs"; +import path from "path"; + +const VARFILE_EXTENSION = ".zzv"; + +function getVarFilePaths(dirPath: string): string[] { + const dirContents = fs.readdirSync(dirPath, { recursive: false, encoding: "utf-8" }); + const varFiles = dirContents.filter((file) => path.extname(file) == VARFILE_EXTENSION); + const varFilePaths = varFiles.map((file) => path.join(dirPath, file)); + + return varFilePaths; +} + +export function getVarFileContents(dirPath: string): string[] { + if (!dirPath) return []; + + const varFilePaths = getVarFilePaths(dirPath); + const fileContents: string[] = []; + varFilePaths.forEach((varFilePath) => { + const fileData = fs.readFileSync(varFilePath, "utf-8"); + fileContents.push(fileData); + }); + + return fileContents; +}