From 00fc7b803858f41e9aeb2f457caf5860abc36801 Mon Sep 17 00:00:00 2001 From: Franco Carrara Date: Wed, 17 Sep 2025 23:23:16 -0300 Subject: [PATCH 1/3] Allow jsonPath to be used along expectedValue and validate the response. --- .../service/infrastructure/networkService.js | 44 +++++++++++++++---- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/server/src/service/infrastructure/networkService.js b/server/src/service/infrastructure/networkService.js index 50cf16b85..2b0b15059 100644 --- a/server/src/service/infrastructure/networkService.js +++ b/server/src/service/infrastructure/networkService.js @@ -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 || "Expected value did not match"; 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 || "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 { httpResponse.status = false; httpResponse.message = this.stringService.httpJsonPathError; From 4039517683ece57198882955a60baec5c238362b Mon Sep 17 00:00:00 2001 From: Franco Carrara Date: Wed, 17 Sep 2025 23:33:45 -0300 Subject: [PATCH 2/3] Add response messages to string service --- server/src/locales/en.json | 2 ++ server/src/service/infrastructure/networkService.js | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/server/src/locales/en.json b/server/src/locales/en.json index 22ab0d157..1345f84a8 100755 --- a/server/src/locales/en.json +++ b/server/src/locales/en.json @@ -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", diff --git a/server/src/service/infrastructure/networkService.js b/server/src/service/infrastructure/networkService.js index 2b0b15059..47ef6878f 100644 --- a/server/src/service/infrastructure/networkService.js +++ b/server/src/service/infrastructure/networkService.js @@ -165,7 +165,7 @@ class NetworkService { } else { httpResponse.code = 500; httpResponse.status = false; - httpResponse.message = this.stringService.httpExpectedValueFail || "Expected value did not match"; + httpResponse.message = this.stringService.httpExpectedValueFail; return httpResponse; } } @@ -192,7 +192,7 @@ class NetworkService { } else { httpResponse.status = false; httpResponse.code = 500; - httpResponse.message = this.stringService.httpJsonPathFail || "JSONPath did not match expected value"; + httpResponse.message = this.stringService.httpJsonPathFail; httpResponse.extracted = extracted; return httpResponse; } @@ -204,7 +204,7 @@ class NetworkService { } else { httpResponse.status = false; httpResponse.code = 500; - httpResponse.message = this.stringService.httpJsonPathFail || "JSONPath result was false"; + httpResponse.message = this.stringService.httpJsonPathFail; httpResponse.extracted = extracted; return httpResponse; } From c666cb3879e7cd6511e08753f41042918ecab6fb Mon Sep 17 00:00:00 2001 From: Franco Carrara Date: Wed, 17 Sep 2025 23:46:20 -0300 Subject: [PATCH 3/3] fix formatting issues --- client/src/locales/th.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/locales/th.json b/client/src/locales/th.json index 4e60d61e0..c3f571b6b 100644 --- a/client/src/locales/th.json +++ b/client/src/locales/th.json @@ -1119,4 +1119,4 @@ "packetsSent": "แพ็กเก็ตที่ส่ง", "rate": "อัตรา", "selectInterface": "เลือกอินเทอร์เฟซ" -} \ No newline at end of file +}