-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: pre-processing of test requests
- Loading branch information
Showing
5 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |