using System.Net; using Microsoft.Extensions.DependencyInjection; using ZB.MOM.WW.Overview.Polling; namespace ZB.MOM.WW.Overview.Tests; /// Overview-003: the pooled probe client must not follow redirects. 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("login"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().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; } }