74 lines
3.0 KiB
C#
74 lines
3.0 KiB
C#
using Flypaper.Core.Settings;
|
|
using Flypaper.Core.Strategies;
|
|
using Shouldly;
|
|
|
|
namespace Flypaper.Tests.Core;
|
|
|
|
public sealed class HoldMethodParserTests
|
|
{
|
|
private static readonly HoldMethodInput Valid = new("Alternating", "50", "1000", "1", "64", "01:00:00");
|
|
|
|
[Fact]
|
|
public void Parses_valid_input()
|
|
{
|
|
var settings = RuntimeSettingsParser.ParseHoldMethod(Valid).Settings.ShouldNotBeNull();
|
|
|
|
settings.Content.ShouldBe(DripContent.Alternating);
|
|
settings.MinDelay.ShouldBe(TimeSpan.FromMilliseconds(50));
|
|
settings.MaxDelay.ShouldBe(TimeSpan.FromSeconds(1));
|
|
settings.MinChunkSize.ShouldBe(1);
|
|
settings.MaxChunkSize.ShouldBe(64);
|
|
settings.MaxHoldDuration.ShouldBe(TimeSpan.FromHours(1));
|
|
settings.ToConfiguration().Keys.ShouldBe(SettingKeys.HoldMethod, ignoreOrder: true);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("html", DripContent.Html)]
|
|
[InlineData("Text", DripContent.Text)]
|
|
public void Accepts_content_case_insensitively(string value, DripContent expected) =>
|
|
RuntimeSettingsParser.ParseHoldMethod(Valid with { Content = value }).Settings.ShouldNotBeNull().Content.ShouldBe(expected);
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData("fake-env")]
|
|
[InlineData("7")]
|
|
public void Rejects_unknown_content(string value) =>
|
|
RuntimeSettingsParser.ParseHoldMethod(Valid with { Content = value }).Errors.Keys.ShouldBe([nameof(HoldMethodInput.Content)]);
|
|
|
|
[Theory]
|
|
[InlineData("9", "1000")]
|
|
[InlineData("50", "30001")]
|
|
[InlineData("x", "1000")]
|
|
public void Rejects_delays_out_of_range(string min, string max) =>
|
|
RuntimeSettingsParser.ParseHoldMethod(Valid with { MinDelayMs = min, MaxDelayMs = max }).Settings.ShouldBeNull();
|
|
|
|
[Theory]
|
|
[InlineData("0", "64")]
|
|
[InlineData("1", "4097")]
|
|
public void Rejects_chunk_sizes_out_of_range(string min, string max) =>
|
|
RuntimeSettingsParser.ParseHoldMethod(Valid with { MinChunkSize = min, MaxChunkSize = max }).Settings.ShouldBeNull();
|
|
|
|
[Fact]
|
|
public void Requires_minimum_not_above_maximum()
|
|
{
|
|
var errors = RuntimeSettingsParser.ParseHoldMethod(Valid with { MinDelayMs = "2000", MaxDelayMs = "1000", MinChunkSize = "100", MaxChunkSize = "10" }).Errors;
|
|
|
|
errors.Keys.ShouldBe([nameof(HoldMethodInput.MaxDelayMs), nameof(HoldMethodInput.MaxChunkSize)], ignoreOrder: true);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("10")]
|
|
[InlineData("00:00:00")]
|
|
[InlineData("8.00:00:00")]
|
|
public void Rejects_invalid_hold_durations(string value) =>
|
|
RuntimeSettingsParser.ParseHoldMethod(Valid with { MaxHoldDuration = value }).Errors.Keys.ShouldBe([nameof(HoldMethodInput.MaxHoldDuration)]);
|
|
|
|
[Fact]
|
|
public void Computes_average_bandwidth()
|
|
{
|
|
HoldMethodSettings.AverageBytesPerSecond(TimeSpan.FromMilliseconds(50), TimeSpan.FromSeconds(1), 1, 64)
|
|
.ShouldBe(61.9, tolerance: 0.1);
|
|
HoldMethodSettings.AverageBytesPerSecond(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1), 10, 10).ShouldBe(10);
|
|
}
|
|
}
|