Files
Flypaper/test/Flypaper.Tests/Core/HtmlDripStrategyTests.cs
T
2026-11-14 16:08:45 +01:00

36 lines
1.3 KiB
C#

using System.Text;
using Flypaper.Core.Models;
using Flypaper.Core.Strategies;
using Flypaper.Tests.Fakes;
using Shouldly;
namespace Flypaper.Tests.Core;
public sealed class HtmlDripStrategyTests
{
[Fact]
public async Task Drips_html_in_small_chunks_until_cancelled()
{
var strategy = CreateStrategy();
var channel = new FakeTrapChannel();
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(300));
await Should.ThrowAsync<OperationCanceledException>(() => strategy.RunAsync(channel, cts.Token));
channel.Writes.Count.ShouldBeGreaterThan(5);
channel.Writes.ShouldAllBe(write => write.Length >= 1 && write.Length <= 64);
Encoding.UTF8.GetString(channel.Writes.SelectMany(write => write).ToArray()).ShouldStartWith("<!doctype html>");
}
[Theory]
[InlineData(TrapProtocol.Http, true)]
[InlineData(TrapProtocol.Ssh, false)]
public void Supports_only_http(TrapProtocol protocol, bool expected) =>
CreateStrategy().Supports(protocol).ShouldBe(expected);
private static HtmlDripStrategy CreateStrategy() => new(
TestOptions.Monitor(new DripOptions { MinDelay = TimeSpan.FromMilliseconds(1), MaxDelay = TimeSpan.FromMilliseconds(5) }),
new SeededRandomSource(21),
TimeProvider.System);
}