Skip to content

Commit

Permalink
feat: pre-processing of test requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Varun0157 committed Jun 16, 2024
1 parent 42a8004 commit ebdffad
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
58 changes: 58 additions & 0 deletions tests/callRequests.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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<void> {
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);
}
9 changes: 9 additions & 0 deletions tests/getResponses.ts
Original file line number Diff line number Diff line change
@@ -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<void> {

}
26 changes: 26 additions & 0 deletions tests/utils/bundleUtils.ts
Original file line number Diff line number Diff line change
@@ -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");
}
}
21 changes: 21 additions & 0 deletions tests/utils/requestUtils.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
25 changes: 25 additions & 0 deletions tests/utils/varUtils.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit ebdffad

Please sign in to comment.