From 39210d9bc21b6777885a3f4f4f4de39227a17379 Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Sat, 11 May 2024 16:33:01 +0530 Subject: [PATCH 1/4] feat: running status, then header, then json, then direct body assertions --- src/mergeData.ts | 10 +++++----- src/runTests.ts | 26 +++++++++++++------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/mergeData.ts b/src/mergeData.ts index bdd614c..22fa6f0 100644 --- a/src/mergeData.ts +++ b/src/mergeData.ts @@ -51,7 +51,7 @@ function getMergedParams(commonParams: RawParams, requestParams: RawParams): Par function getMergedHeaders( commonHeaders: RawHeaders, - requestHeaders: RawHeaders, + requestHeaders: RawHeaders ): { [name: string]: string } { if (Array.isArray(commonHeaders)) { commonHeaders = getArrayHeadersAsObject(commonHeaders); @@ -79,7 +79,7 @@ function getMergedOptions(cOptions: RawOptions = {}, rOptions: RawOptions = {}): function getMergedSetVars( setvars: RawSetVars = {}, - captures: Captures = {}, + captures: Captures = {} ): { mergedVars: SetVar[]; hasJsonVars: boolean } { const mergedVars: SetVar[] = []; let hasJsonVars = false; @@ -152,7 +152,7 @@ function mergePrefixBasedTests(tests: RawTests) { function getMergedTests( cTests: RawTests = {}, - rTests: RawTests = {}, + rTests: RawTests = {} ): { mergedTests: Tests; hasJsonTests: boolean } { // Convert $. and h. at root level into headers and json keys mergePrefixBasedTests(cTests); @@ -209,11 +209,11 @@ export function getMergedData(commonData: Common, requestData: RawRequest): Requ const { mergedTests: tests, hasJsonTests: hasJsonTests } = getMergedTests( commonData?.tests, - requestData.tests, + requestData.tests ); const { mergedVars: setvars, hasJsonVars: hasJsonVars } = getMergedSetVars( requestData.setvars, - requestData.capture, + requestData.capture ); const mergedData: RequestSpec = { diff --git a/src/runTests.ts b/src/runTests.ts index e6d8c3f..f9f7006 100644 --- a/src/runTests.ts +++ b/src/runTests.ts @@ -8,11 +8,11 @@ export function runAllTests(tests: Tests, responseData: ResponseData): TestResul const results: TestResult[] = []; if (!tests) return results; - for (const spec in tests.json) { - const expected = tests.json[spec]; - const received = getValueForJSONTests(responseData.json, spec); - const jsonResults = runTest(spec, expected, received); - results.push(...jsonResults); + if (tests.status) { + const expected = tests.status; + const received = responseData.status; + const statusResults = runTest("status", expected, received); + results.push(...statusResults); } for (const spec in tests.headers) { @@ -22,6 +22,13 @@ export function runAllTests(tests: Tests, responseData: ResponseData): TestResul results.push(...headerResults); } + for (const spec in tests.json) { + const expected = tests.json[spec]; + const received = getValueForJSONTests(responseData.json, spec); + const jsonResults = runTest(spec, expected, received); + results.push(...jsonResults); + } + if (tests.body) { const expected = tests.body; const received = responseData.body; @@ -29,13 +36,6 @@ export function runAllTests(tests: Tests, responseData: ResponseData): TestResul results.push(...bodyResults); } - if (tests.status) { - const expected = tests.status; - const received = responseData.status; - const statusResults = runTest("status", expected, received); - results.push(...statusResults); - } - return results; } @@ -67,7 +67,7 @@ function getValueForJSONTests(responseContent: object, key: string): any { function runObjectTests( opVals: { [key: string]: any }, receivedObject: any, - spec: string, + spec: string ): TestResult[] { let results: TestResult[] = []; From 04a46db8f86b790bcdd7e0bc07bd8d92b2d4b502 Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Sat, 11 May 2024 17:42:10 +0530 Subject: [PATCH 2/4] feat: introduced stop on failure option --- src/mergeData.ts | 3 ++- src/models.ts | 2 ++ src/runTests.ts | 6 +++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/mergeData.ts b/src/mergeData.ts index 22fa6f0..5ef983e 100644 --- a/src/mergeData.ts +++ b/src/mergeData.ts @@ -73,8 +73,9 @@ function getMergedOptions(cOptions: RawOptions = {}, rOptions: RawOptions = {}): const keepRawJSON = options.keepRawJSON == true; const showHeaders = options.showHeaders == true; const rawParams = options.rawParams == true; + const stopOnFailure = options.stopOnFailure == true; - return { follow, verifySSL, keepRawJSON, showHeaders, rawParams }; + return { follow, verifySSL, keepRawJSON, showHeaders, rawParams, stopOnFailure }; } function getMergedSetVars( diff --git a/src/models.ts b/src/models.ts index 9a075cd..c56a0d3 100644 --- a/src/models.ts +++ b/src/models.ts @@ -16,6 +16,7 @@ export interface Options { keepRawJSON: boolean; showHeaders: boolean; rawParams: boolean; + stopOnFailure: boolean; } export type Assertion = number | boolean | string | null | { [op: string]: any }; @@ -50,6 +51,7 @@ export interface RawOptions { keepRawJSON?: boolean; showHeaders?: boolean; rawParams?: boolean; + stopOnFailure?: boolean; } export interface RawTests { diff --git a/src/runTests.ts b/src/runTests.ts index f9f7006..3434a09 100644 --- a/src/runTests.ts +++ b/src/runTests.ts @@ -4,7 +4,11 @@ import { getStringIfNotScalar } from "./utils/typeUtils"; import { Tests, ResponseData, TestResult, Assertion } from "./models"; -export function runAllTests(tests: Tests, responseData: ResponseData): TestResult[] { +export function runAllTests( + tests: Tests, + responseData: ResponseData, + stopOnFailure: boolean +): TestResult[] { const results: TestResult[] = []; if (!tests) return results; From e14a33beb2d2bf24d50123ee9d404f40adb2a587 Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Sat, 11 May 2024 17:45:28 +0530 Subject: [PATCH 3/4] feat: implemented stopping subsequent tests on status failure --- src/runTests.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/runTests.ts b/src/runTests.ts index 3434a09..609209b 100644 --- a/src/runTests.ts +++ b/src/runTests.ts @@ -12,12 +12,15 @@ export function runAllTests( const results: TestResult[] = []; if (!tests) return results; + let statusFail = false; if (tests.status) { const expected = tests.status; const received = responseData.status; const statusResults = runTest("status", expected, received); results.push(...statusResults); + statusFail = statusResults.some((r) => !r.pass); } + if (stopOnFailure && statusFail) return results; for (const spec in tests.headers) { const expected = tests.headers[spec]; From 12741162e91ab5a006d6bca63baf2d3577ab6aa1 Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Sat, 11 May 2024 17:51:52 +0530 Subject: [PATCH 4/4] reformat: formatting w prettier explicitly --- src/mergeData.ts | 10 +++++----- src/runTests.ts | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/mergeData.ts b/src/mergeData.ts index 5ef983e..6b0f120 100644 --- a/src/mergeData.ts +++ b/src/mergeData.ts @@ -51,7 +51,7 @@ function getMergedParams(commonParams: RawParams, requestParams: RawParams): Par function getMergedHeaders( commonHeaders: RawHeaders, - requestHeaders: RawHeaders + requestHeaders: RawHeaders, ): { [name: string]: string } { if (Array.isArray(commonHeaders)) { commonHeaders = getArrayHeadersAsObject(commonHeaders); @@ -80,7 +80,7 @@ function getMergedOptions(cOptions: RawOptions = {}, rOptions: RawOptions = {}): function getMergedSetVars( setvars: RawSetVars = {}, - captures: Captures = {} + captures: Captures = {}, ): { mergedVars: SetVar[]; hasJsonVars: boolean } { const mergedVars: SetVar[] = []; let hasJsonVars = false; @@ -153,7 +153,7 @@ function mergePrefixBasedTests(tests: RawTests) { function getMergedTests( cTests: RawTests = {}, - rTests: RawTests = {} + rTests: RawTests = {}, ): { mergedTests: Tests; hasJsonTests: boolean } { // Convert $. and h. at root level into headers and json keys mergePrefixBasedTests(cTests); @@ -210,11 +210,11 @@ export function getMergedData(commonData: Common, requestData: RawRequest): Requ const { mergedTests: tests, hasJsonTests: hasJsonTests } = getMergedTests( commonData?.tests, - requestData.tests + requestData.tests, ); const { mergedVars: setvars, hasJsonVars: hasJsonVars } = getMergedSetVars( requestData.setvars, - requestData.capture + requestData.capture, ); const mergedData: RequestSpec = { diff --git a/src/runTests.ts b/src/runTests.ts index 609209b..5a92d13 100644 --- a/src/runTests.ts +++ b/src/runTests.ts @@ -7,7 +7,7 @@ import { Tests, ResponseData, TestResult, Assertion } from "./models"; export function runAllTests( tests: Tests, responseData: ResponseData, - stopOnFailure: boolean + stopOnFailure: boolean, ): TestResult[] { const results: TestResult[] = []; if (!tests) return results; @@ -74,7 +74,7 @@ function getValueForJSONTests(responseContent: object, key: string): any { function runObjectTests( opVals: { [key: string]: any }, receivedObject: any, - spec: string + spec: string, ): TestResult[] { let results: TestResult[] = [];