From 7610d11de5a05ddfe672101de7232a57d6ffde11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8C=BF=E4=BA=BA=E6=98=93?= Date: Wed, 12 Feb 2025 16:00:41 +0800 Subject: [PATCH] =?UTF-8?q?WebHook=E6=94=AF=E6=8C=81=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E4=BB=A4=E7=89=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Stardust.Server/Services/MonitorService.cs | 25 +++++++++++++---- .../Services/MonitorServiceTests.cs | 28 +++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 Stardust.ServerTests/Services/MonitorServiceTests.cs diff --git a/Stardust.Server/Services/MonitorService.cs b/Stardust.Server/Services/MonitorService.cs index 512ef3c..12ae4dc 100644 --- a/Stardust.Server/Services/MonitorService.cs +++ b/Stardust.Server/Services/MonitorService.cs @@ -25,15 +25,16 @@ public void WebHook(AppTracer app, TraceModel model) actor.Tell(model); } - class WebHookActor : Actor + public class WebHookActor : Actor { public AppTracer App { get; set; } private ApiHttpClient _client; private String _server; private String _action; + private String _token; - private ApiHttpClient GetClient() + public ApiHttpClient GetClient() { var addr = App.WebHook; if (addr.IsNullOrEmpty()) return null; @@ -44,13 +45,27 @@ private ApiHttpClient GetClient() } if (addr.IsNullOrEmpty()) return null; + _server = addr; - _client = new ApiHttpClient(addr); + var p = addr.IndexOf("#token=", StringComparison.OrdinalIgnoreCase); + if (p > 0) + { + _token = addr[(p + 7)..]; + addr = addr[..p]; + } - _server = addr; + //p = addr.IndexOf('?'); + //if (p > 0) + //{ + // _action = addr[(p + 1)..]; + // addr = addr[..p]; + //} var uri = new Uri(addr); - _action = uri.AbsolutePath; + _action = uri.PathAndQuery; + + _client = new ApiHttpClient($"{uri.Scheme}://{uri.Authority}"); + _client.Token = _token; return _client; } diff --git a/Stardust.ServerTests/Services/MonitorServiceTests.cs b/Stardust.ServerTests/Services/MonitorServiceTests.cs new file mode 100644 index 0000000..755b6c2 --- /dev/null +++ b/Stardust.ServerTests/Services/MonitorServiceTests.cs @@ -0,0 +1,28 @@ +using System; +using NewLife.Reflection; +using Stardust.Data.Monitors; +using Xunit; +using static Stardust.Server.Services.MonitorService; + +namespace Stardust.ServerTests.Services; + +public class MonitorServiceTests +{ + [Fact] + public void GetClientTest() + { + var actor = new WebHookActor(); + actor.App = new AppTracer { WebHook = "https://newlifex.com/monitor/push?id=1234#token=abcd" }; + + var uri = new Uri(actor.App.WebHook); + Assert.Equal("newlifex.com", uri.Host); + Assert.Equal("/monitor/push", uri.AbsolutePath); + Assert.Equal("?id=1234", uri.Query); + Assert.Equal("/monitor/push?id=1234", uri.PathAndQuery); + + var client = actor.GetClient(); + Assert.NotNull(client); + Assert.Equal("/monitor/push?id=1234", actor.GetValue("_action")); + Assert.Equal("abcd", actor.GetValue("_token")); + } +}