Files
Flypaper/test/Flypaper.Tests/Http/TargetNormalizationTests.cs

51 lines
1.8 KiB
C#

using System.Net;
using Flypaper.Core;
using Flypaper.Core.Abstractions;
using Flypaper.Core.Models;
using Flypaper.Http;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Shouldly;
namespace Flypaper.Tests.Http;
public sealed class TargetNormalizationTests
{
[Theory]
[InlineData("_", "GET /boaform/admin/formLogin")]
[InlineData("", "GET /boaform/admin/formLogin")]
[InlineData("example.com", "GET example.com/boaform/admin/formLogin")]
[InlineData("203.0.113.7:8080", "GET 203.0.113.7:8080/boaform/admin/formLogin")]
public async Task Stores_missing_or_placeholder_host_as_empty(string host, string expectedTarget)
{
var sink = Substitute.For<ITrapEventSink>();
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?> { ["Flypaper:MaxConnections"] = "0" })
.Build();
var services = new ServiceCollection()
.AddLogging()
.AddSingleton(sink)
.AddFlypaperCore(configuration)
.AddFlypaperHttp(configuration)
.BuildServiceProvider();
var app = new ApplicationBuilder(services);
app.UseFlypaper();
app.Run(_ => Task.CompletedTask);
var context = new DefaultHttpContext { RequestServices = services };
context.Connection.RemoteIpAddress = IPAddress.Parse("203.0.113.9");
context.Connection.LocalPort = 8080;
context.Request.Method = "GET";
context.Request.Host = new HostString(host);
context.Request.Path = "/boaform/admin/formLogin";
await app.Build()(context);
sink.Received(1).Publish(Arg.Is<TrapEvent>(e => e.Target == expectedTarget));
}
}