Skip to content

Commit

Permalink
Merge branch 'main' of github.com:agrostar/zzapi
Browse files Browse the repository at this point in the history
  • Loading branch information
vasan-agrostar committed Jun 2, 2024
2 parents 54aafbc + 7dc4928 commit 3c539a6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/mergeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function getMergedSetVars(
* $. and $h. prefixes, as this is more convenient when specifying them.
* We merge these into tests.json and tests.headers respectively.
*/
function mergePrefixBasedTests(tests: RawTests) {
export function mergePrefixBasedTests(tests: RawTests) {
if (!tests.json) tests.json = {};
if (!tests.headers) tests.headers = {};
for (const key of Object.keys(tests)) {
Expand Down
28 changes: 25 additions & 3 deletions src/runTests.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import jp from "jsonpath";

import { getStringIfNotScalar } from "./utils/typeUtils";
import { getStringIfNotScalar, isDict } from "./utils/typeUtils";

import { Tests, ResponseData, TestResult, Assertion } from "./models";
import { mergePrefixBasedTests } from "./mergeData";

export function runAllTests(
tests: Tests,
Expand Down Expand Up @@ -47,8 +48,8 @@ export function runAllTests(

function runTest(spec: string, expected: Assertion, received: any): TestResult[] {
let results: TestResult[] = [];
// typeof null is also 'object'
if (typeof expected !== "object" || expected == null) {
// typeof null is 'object', so we include it here
expected = getStringIfNotScalar(expected);
received = getStringIfNotScalar(received);
const pass = expected === received;
Expand Down Expand Up @@ -80,6 +81,7 @@ function runObjectTests(
for (const op in opVals) {
let expected = getStringIfNotScalar(opVals[op]);
let received = getStringIfNotScalar(receivedObject);

let pass = false;
let message = "";
if (op === "$eq") {
Expand Down Expand Up @@ -134,8 +136,28 @@ function runObjectTests(
pass = typeof received === "string" && received.endsWith(expected);
} else if (op === "$co") {
pass = typeof received === "string" && received.includes(expected);
} else if (op == "$options") {
} else if (op === "$options") {
continue; // do nothing. $regex will address it.
} else if (op === "$tests") {
const recursiveTests = opVals[op];

if (!isDict(recursiveTests)) {
pass = false;
message = "recursive tests must be dicts";
} else {
mergePrefixBasedTests(recursiveTests);
const receivedObj: ResponseData = {
executionTime: 0,
body: "",
rawHeaders: "",
headers: {},
json: receivedObject,
};

const res = runAllTests(recursiveTests, receivedObj, false);
results.push(...res);
continue;
}
} else {
results.push({
pass: false,
Expand Down

0 comments on commit 3c539a6

Please sign in to comment.