Files
scadaproj/ZB.MOM.WW.Overview/tests/ZB.MOM.WW.Overview.Tests/ProbeRedirectTests.cs
T

62 lines
2.1 KiB
C#

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;
}
}