Allow jsonPath to be used along expectedValue and validate the response.
This commit is contained in:
1 parent
81d283ec8d
commit
00fc7b8038
1 file changed
+35
-9
@@ -1,5 +1,3 @@
|
|||||||
import { match } from "assert";
|
|
||||||
|
|
||||||
const SERVICE_NAME = "NetworkService";
|
const SERVICE_NAME = "NetworkService";
|
||||||
|
|
||||||
class NetworkService {
|
class NetworkService {
|
||||||
@@ -107,7 +105,7 @@ class NetworkService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async requestHttp(monitor) {
|
async requestHttp(monitor) {
|
||||||
const { url, secret, _id, name, teamId, type, ignoreTlsErrors, jsonPath, matchMethod, expectedValue } = monitor;
|
const { url, secret, _id, teamId, type, ignoreTlsErrors, jsonPath, matchMethod, expectedValue } = monitor;
|
||||||
const httpResponse = {
|
const httpResponse = {
|
||||||
monitorId: _id,
|
monitorId: _id,
|
||||||
teamId: teamId,
|
teamId: teamId,
|
||||||
@@ -156,33 +154,61 @@ class NetworkService {
|
|||||||
return httpResponse;
|
return httpResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (expectedValue) {
|
if (expectedValue && !jsonPath) {
|
||||||
let ok = false;
|
let ok = false;
|
||||||
if (matchMethod === "equal") ok = payload === expectedValue;
|
if (matchMethod === "equal") ok = payload === expectedValue;
|
||||||
if (matchMethod === "include") ok = payload.includes(expectedValue);
|
if (matchMethod === "include" && typeof payload === "string") ok = payload.includes(expectedValue);
|
||||||
if (matchMethod === "regex") ok = new RegExp(expectedValue).test(payload);
|
if (matchMethod === "regex" && typeof payload === "string") ok = new RegExp(expectedValue).test(payload);
|
||||||
|
|
||||||
if (ok === true) {
|
if (ok === true) {
|
||||||
return httpResponse;
|
return httpResponse;
|
||||||
} else {
|
} else {
|
||||||
httpResponse.code = 500;
|
httpResponse.code = 500;
|
||||||
httpResponse.status = false;
|
httpResponse.status = false;
|
||||||
|
httpResponse.message = this.stringService.httpExpectedValueFail || "Expected value did not match";
|
||||||
return httpResponse;
|
return httpResponse;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (jsonPath) {
|
if (jsonPath) {
|
||||||
const contentType = response.headers["content-type"];
|
const contentType = response.headers["content-type"];
|
||||||
|
|
||||||
const isJson = contentType?.includes("application/json");
|
const isJson = contentType?.includes("application/json");
|
||||||
if (!isJson) {
|
if (!isJson) {
|
||||||
httpResponse.status = false;
|
httpResponse.status = false;
|
||||||
httpResponse.message = this.stringService.httpNotJson;
|
httpResponse.message = this.stringService.httpNotJson;
|
||||||
return httpResponse;
|
return httpResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.jmespath.search(payload, jsonPath);
|
const extracted = this.jmespath.search(payload, jsonPath);
|
||||||
|
if (expectedValue) {
|
||||||
|
let ok = false;
|
||||||
|
if (matchMethod === "equal") ok = extracted === expectedValue;
|
||||||
|
if (matchMethod === "include" && typeof extracted === "string") ok = extracted.includes(expectedValue);
|
||||||
|
if (matchMethod === "regex" && typeof extracted === "string") ok = new RegExp(expectedValue).test(extracted);
|
||||||
|
|
||||||
|
if (ok) {
|
||||||
|
httpResponse.extracted = extracted;
|
||||||
|
return httpResponse;
|
||||||
|
} else {
|
||||||
|
httpResponse.status = false;
|
||||||
|
httpResponse.code = 500;
|
||||||
|
httpResponse.message = this.stringService.httpJsonPathFail || "JSONPath did not match expected value";
|
||||||
|
httpResponse.extracted = extracted;
|
||||||
|
return httpResponse;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const isFalsey = extracted === false || extracted === "false" || extracted === undefined || extracted === null;
|
||||||
|
if (!isFalsey) {
|
||||||
|
httpResponse.extracted = extracted;
|
||||||
|
return httpResponse;
|
||||||
|
} else {
|
||||||
|
httpResponse.status = false;
|
||||||
|
httpResponse.code = 500;
|
||||||
|
httpResponse.message = this.stringService.httpJsonPathFail || "JSONPath result was false";
|
||||||
|
httpResponse.extracted = extracted;
|
||||||
|
return httpResponse;
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
httpResponse.status = false;
|
httpResponse.status = false;
|
||||||
httpResponse.message = this.stringService.httpJsonPathError;
|
httpResponse.message = this.stringService.httpJsonPathError;
|
||||||
|
|||||||
Reference in new issue
Block a user