mirror of
https://github.com/bluewave-labs/Checkmate.git
synced 2026-01-07 10:19:40 -06:00
Merge pull request #2957 from FrancocDev/develop
Allow jsonPath to be used along expectedValue and validate the response.
This commit is contained in:
@@ -1119,4 +1119,4 @@
|
||||
"packetsSent": "แพ็กเก็ตที่ส่ง",
|
||||
"rate": "อัตรา",
|
||||
"selectInterface": "เลือกอินเทอร์เฟซ"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,10 +149,12 @@
|
||||
"statusPageByTeamId": "Got status pages by team id successfully",
|
||||
"httpNetworkError": "Network error",
|
||||
"httpNotJson": "Response data is not json",
|
||||
"httpExpectedValueFail": "Expected value did not match",
|
||||
"httpJsonPathError": "Failed to parse json data",
|
||||
"httpEmptyResult": "Result is empty",
|
||||
"httpMatchSuccess": "Response data match successfully",
|
||||
"httpMatchFail": "Failed to match response data",
|
||||
"httpJsonPathFail": "JSONPath did not match expected value",
|
||||
"webhookSendSuccess": "Webhook notification sent successfully",
|
||||
"telegramRequiresBotTokenAndChatId": "Telegram notifications require both botToken and chatId",
|
||||
"webhookUrlRequired": "Webhook URL is required",
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import { match } from "assert";
|
||||
|
||||
const SERVICE_NAME = "NetworkService";
|
||||
|
||||
class NetworkService {
|
||||
@@ -107,7 +105,7 @@ class NetworkService {
|
||||
}
|
||||
|
||||
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 = {
|
||||
monitorId: _id,
|
||||
teamId: teamId,
|
||||
@@ -156,33 +154,61 @@ class NetworkService {
|
||||
return httpResponse;
|
||||
}
|
||||
|
||||
if (expectedValue) {
|
||||
if (expectedValue && !jsonPath) {
|
||||
let ok = false;
|
||||
if (matchMethod === "equal") ok = payload === expectedValue;
|
||||
if (matchMethod === "include") ok = payload.includes(expectedValue);
|
||||
if (matchMethod === "regex") ok = new RegExp(expectedValue).test(payload);
|
||||
if (matchMethod === "include" && typeof payload === "string") ok = payload.includes(expectedValue);
|
||||
if (matchMethod === "regex" && typeof payload === "string") ok = new RegExp(expectedValue).test(payload);
|
||||
|
||||
if (ok === true) {
|
||||
return httpResponse;
|
||||
} else {
|
||||
httpResponse.code = 500;
|
||||
httpResponse.status = false;
|
||||
httpResponse.message = this.stringService.httpExpectedValueFail;
|
||||
return httpResponse;
|
||||
}
|
||||
}
|
||||
|
||||
if (jsonPath) {
|
||||
const contentType = response.headers["content-type"];
|
||||
|
||||
const isJson = contentType?.includes("application/json");
|
||||
if (!isJson) {
|
||||
httpResponse.status = false;
|
||||
httpResponse.message = this.stringService.httpNotJson;
|
||||
return httpResponse;
|
||||
}
|
||||
|
||||
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;
|
||||
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;
|
||||
httpResponse.extracted = extracted;
|
||||
return httpResponse;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
httpResponse.status = false;
|
||||
httpResponse.message = this.stringService.httpJsonPathError;
|
||||
|
||||
Reference in New Issue
Block a user