using System.Net; using System.Text; using System.Text.RegularExpressions; using Flypaper.Core.Models; using Flypaper.Core.Runtime; using Flypaper.Core.Strategies; using Flypaper.Tests.Fakes; using Shouldly; namespace Flypaper.Tests.Core; public sealed partial class MazeContentTests { [GeneratedRegex("href=\"([^\"]*)\"")] private static partial Regex Href(); private static List Links(string html) => Href().Matches(html).Select(match => match.Groups[1].Value).ToList(); [Fact] public void Head_start_already_carries_many_links() { using var fragments = MazeContent.Page(new SeededRandomSource(1), "/wiki/lorem", "example.com", 160).GetEnumerator(); var head = Encoding.UTF8.GetString(DripSchedule.TakeHead(fragments, 1024).Span); head.Length.ShouldBeGreaterThanOrEqualTo(1024); Links(head).Count.ShouldBeGreaterThanOrEqualTo(10); } [Fact] public void Keeps_delivering_links_at_the_configured_spacing() { var body = string.Concat(MazeContent.Page(new SeededRandomSource(2), "/wiki/lorem", "example.com", 160).Skip(4).Take(40)); var tight = string.Concat(MazeContent.Page(new SeededRandomSource(2), "/wiki/lorem", "example.com", 40).Skip(4).Take(40)); var bytesPerLink = Encoding.UTF8.GetByteCount(body) / (double)Links(body).Count; var tightBytesPerLink = Encoding.UTF8.GetByteCount(tight) / (double)Links(tight).Count; bytesPerLink.ShouldBeInRange(100, 450); tightBytesPerLink.ShouldBeLessThan(bytesPerLink); } [Theory] [InlineData("/")] [InlineData("/index.php")] [InlineData("//images/images/cache.php")] public void Pages_outside_the_roots_only_link_into_the_disallowed_roots(string path) { var page = string.Concat(MazeContent.Page(new SeededRandomSource(3), path, "example.com", 80).Take(300)); var baseUri = new Uri("http://example.com" + path); foreach (var href in Links(page)) { var target = new Uri(baseUri, href); target.Host.ShouldBe("example.com"); MazeContent.Roots.ShouldContain(target.AbsolutePath.Split('/', StringSplitOptions.RemoveEmptyEntries).First()); } } [Fact] public void Listings_below_a_root_never_leave_the_root() { var listing = string.Concat(MazeContent.Listing(new SeededRandomSource(3), "/files/lorem/", "example.com").Take(200)); var baseUri = new Uri("http://example.com/files/lorem/"); foreach (var href in Links(listing).Where(href => href != "../")) { new Uri(baseUri, href).AbsolutePath.Split('/', StringSplitOptions.RemoveEmptyEntries).First().ShouldBeOneOf(MazeContent.Roots); } } [Fact] public void Relative_links_below_a_root_never_leave_the_root() { var page = string.Concat(MazeContent.Page(new SeededRandomSource(4), "/blog/2025/03/lorem", "example.com", 80).Take(300)); var baseUri = new Uri("http://example.com/blog/2025/03/lorem"); foreach (var href in Links(page).Where(href => !href.StartsWith('/') && !href.StartsWith("http", StringComparison.Ordinal))) { new Uri(baseUri, href).AbsolutePath.ShouldStartWith("/blog/"); } } [Fact] public void Mixes_relative_and_absolute_links_and_limits_depth() { var links = Links(string.Concat(MazeContent.Page(new SeededRandomSource(5), "/docs/lorem", "example.com", 80).Take(300))); links.ShouldContain(href => href.StartsWith("http://example.com/docs/", StringComparison.Ordinal)); links.ShouldContain(href => href.StartsWith("/docs/", StringComparison.Ordinal)); links.ShouldContain(href => !href.StartsWith('/') && !href.StartsWith("http", StringComparison.Ordinal)); links.ShouldContain(href => href.StartsWith("/blog/20", StringComparison.Ordinal)); var deepPath = "/docs/" + string.Join('/', Enumerable.Repeat("x", 8)) + "/"; var deep = Links(string.Concat(MazeContent.Page(new SeededRandomSource(6), deepPath, null, 80).Take(300))); deep.ShouldContain("../"); deep.ShouldNotContain(href => href.StartsWith("http", StringComparison.Ordinal)); } [Fact] public void Ignores_a_hostile_host_header() { var page = string.Concat(MazeContent.Page(new SeededRandomSource(7), "/wiki/a", "evil\">