80 lines
3.1 KiB
C#
80 lines
3.1 KiB
C#
using System.Net;
|
|
using System.Text;
|
|
using Flypaper.Core.Abstractions;
|
|
using Flypaper.Core.Models;
|
|
using Flypaper.Core.Runtime;
|
|
using Flypaper.Core.Strategies;
|
|
using Flypaper.Tests.Fakes;
|
|
using Shouldly;
|
|
|
|
namespace Flypaper.Tests.Core;
|
|
|
|
public sealed class StrategySelectionTests
|
|
{
|
|
private static readonly TrapContext Http =
|
|
new(new TrapSession(Guid.NewGuid(), TrapProtocol.Http, IPAddress.Parse("203.0.113.9"), DateTimeOffset.UtcNow));
|
|
|
|
[Theory]
|
|
[InlineData(DripContent.Html, "html-drip")]
|
|
[InlineData(DripContent.Text, "text-drip")]
|
|
public void Selects_the_configured_content(DripContent content, string expected) =>
|
|
Selector(content).Select(Http).ShouldNotBeNull().Name.ShouldBe(expected);
|
|
|
|
[Fact]
|
|
public void Alternating_picks_both_strategies()
|
|
{
|
|
var selector = Selector(DripContent.Alternating);
|
|
|
|
var names = Enumerable.Range(0, 50).Select(_ => selector.Select(Http)!.Name).Distinct().ToList();
|
|
|
|
names.ShouldBe(["html-drip", "text-drip"], ignoreOrder: true);
|
|
}
|
|
|
|
[Fact]
|
|
public void Returns_nothing_for_unsupported_protocols()
|
|
{
|
|
var ssh = Http with { Session = Http.Session with { Protocol = TrapProtocol.Ssh } };
|
|
|
|
Selector(DripContent.Html).Select(ssh).ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void Strategies_describe_themselves_with_their_parameters()
|
|
{
|
|
var options = TestOptions.Monitor(new DripOptions { MinDelay = TimeSpan.FromMilliseconds(100), MaxDelay = TimeSpan.FromMilliseconds(250), MinChunkSize = 2, MaxChunkSize = 8 });
|
|
var text = new TextDripStrategy(options, new SeededRandomSource(1), TimeProvider.System);
|
|
|
|
text.Describe().ShouldBe("text-drip 100-250ms 2-8B");
|
|
text.MediaType.ShouldStartWith("text/plain");
|
|
new HtmlDripStrategy(options, new SeededRandomSource(1), TimeProvider.System).MediaType.ShouldStartWith("text/html");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Text_strategy_drips_plain_text()
|
|
{
|
|
var strategy = new TextDripStrategy(
|
|
TestOptions.Monitor(new DripOptions { MinDelay = TimeSpan.FromMilliseconds(1), MaxDelay = TimeSpan.FromMilliseconds(2), MinChunkSize = 32, MaxChunkSize = 64 }),
|
|
new SeededRandomSource(3),
|
|
TimeProvider.System);
|
|
var channel = new FakeTrapChannel();
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(200));
|
|
|
|
await Should.ThrowAsync<OperationCanceledException>(() => strategy.RunAsync(channel, cts.Token));
|
|
|
|
var text = Encoding.UTF8.GetString(channel.Writes.SelectMany(w => w).ToArray());
|
|
text.Length.ShouldBeGreaterThan(200);
|
|
text.ShouldNotContain("<");
|
|
text.ShouldContain("\n");
|
|
}
|
|
|
|
private static ConfiguredStrategySelector Selector(DripContent content)
|
|
{
|
|
var options = TestOptions.Monitor(new DripOptions { Content = content });
|
|
var random = new SeededRandomSource(11);
|
|
return new ConfiguredStrategySelector(
|
|
[new HtmlDripStrategy(options, random, TimeProvider.System), new TextDripStrategy(options, random, TimeProvider.System)],
|
|
options,
|
|
random);
|
|
}
|
|
}
|