fix(overview): P3 review fixes 003-005 - no redirects on probes, per-target publish so a hung instance cannot stale the rest, probe docs (scadaproj#48)
Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
using System.Net;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ZB.MOM.WW.Overview.Polling;
|
||||
|
||||
namespace ZB.MOM.WW.Overview.Tests;
|
||||
|
||||
/// <summary>Overview-003: the pooled probe client must not follow redirects.</summary>
|
||||
public sealed class ProbeRedirectTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task ActiveProbe_AuthRedirectToLoginPage_IsNotReportedAsActive()
|
||||
{
|
||||
var port = FreePort();
|
||||
var prefix = $"http://127.0.0.1:{port}/";
|
||||
using var listener = new HttpListener();
|
||||
listener.Prefixes.Add(prefix);
|
||||
listener.Start();
|
||||
var server = Task.Run(async () =>
|
||||
{
|
||||
while (listener.IsListening)
|
||||
{
|
||||
HttpListenerContext ctx;
|
||||
try { ctx = await listener.GetContextAsync(); } catch { return; }
|
||||
if (ctx.Request.Url!.AbsolutePath == "/login")
|
||||
{
|
||||
ctx.Response.StatusCode = 200;
|
||||
ctx.Response.ContentType = "text/html";
|
||||
await ctx.Response.OutputStream.WriteAsync("<html>login</html>"u8.ToArray());
|
||||
}
|
||||
else
|
||||
{
|
||||
ctx.Response.StatusCode = 302;
|
||||
ctx.Response.RedirectLocation = "/login";
|
||||
}
|
||||
ctx.Response.Close();
|
||||
}
|
||||
});
|
||||
|
||||
var services = new ServiceCollection();
|
||||
services.AddLogging();
|
||||
services.AddOverviewPolling();
|
||||
await using var provider = services.BuildServiceProvider();
|
||||
var client = provider.GetRequiredService<IHttpClientFactory>().CreateClient(OverviewPollerService.HttpClientName);
|
||||
|
||||
var probe = await new ZbHealthReportClient(client)
|
||||
.ProbeStatusAsync($"{prefix}health/active", TimeSpan.FromSeconds(5));
|
||||
|
||||
listener.Stop();
|
||||
Assert.Equal(302, probe.StatusCode);
|
||||
Assert.Equal(ActiveState.Unknown, StatusDerivation.ObserveActive(true, probe));
|
||||
}
|
||||
|
||||
private static int FreePort()
|
||||
{
|
||||
var l = new System.Net.Sockets.TcpListener(IPAddress.Loopback, 0);
|
||||
l.Start();
|
||||
var port = ((IPEndPoint)l.LocalEndpoint).Port;
|
||||
l.Stop();
|
||||
return port;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user