Merge pull request #2957 from FrancocDev/develop

Allow jsonPath to be used along expectedValue and validate the response.
This commit is contained in:
Alexander Holliday authored and GitHub committed 2025-09-18 09:47:53 -07:00
commit 0e6e54a124
3 files changed
+38 -10

No files matched your search

+1 -1
View File
@@ -1119,4 +1119,4 @@
"packetsSent": "แพ็กเก็ตที่ส่ง", "packetsSent": "แพ็กเก็ตที่ส่ง",
"rate": "อัตรา", "rate": "อัตรา",
"selectInterface": "เลือกอินเทอร์เฟซ" "selectInterface": "เลือกอินเทอร์เฟซ"
} }
+2
View File
@@ -149,10 +149,12 @@
"statusPageByTeamId": "Got status pages by team id successfully", "statusPageByTeamId": "Got status pages by team id successfully",
"httpNetworkError": "Network error", "httpNetworkError": "Network error",
"httpNotJson": "Response data is not json", "httpNotJson": "Response data is not json",
"httpExpectedValueFail": "Expected value did not match",
"httpJsonPathError": "Failed to parse json data", "httpJsonPathError": "Failed to parse json data",
"httpEmptyResult": "Result is empty", "httpEmptyResult": "Result is empty",
"httpMatchSuccess": "Response data match successfully", "httpMatchSuccess": "Response data match successfully",
"httpMatchFail": "Failed to match response data", "httpMatchFail": "Failed to match response data",
"httpJsonPathFail": "JSONPath did not match expected value",
"webhookSendSuccess": "Webhook notification sent successfully", "webhookSendSuccess": "Webhook notification sent successfully",
"telegramRequiresBotTokenAndChatId": "Telegram notifications require both botToken and chatId", "telegramRequiresBotTokenAndChatId": "Telegram notifications require both botToken and chatId",
"webhookUrlRequired": "Webhook URL is required", "webhookUrlRequired": "Webhook URL is required",
@@ -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;
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;
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 { } catch {
httpResponse.status = false; httpResponse.status = false;
httpResponse.message = this.stringService.httpJsonPathError; httpResponse.message = this.stringService.httpJsonPathError;