Path: redb-route/tests/redb.Route.Tests/Core/UriLogFormTests.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests/Core/UriLogFormTests.cs ---- using AwesomeAssertions; using Microsoft.Extensions.Logging; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.Tests.Cluster; using Xunit.Abstractions; namespace redb.Route.Tests.Core; /// /// The short log form of a long endpoint URI (): a SQL statement or a long query string /// no longer fills the "Compiled route" and "Endpoint started" lines, the form still tells endpoints apart, never shows /// part of a masked secret, and the whole masked URI is on the same line when Debug is on. /// public class UriLogFormTests(ITestOutputHelper output) { private const string LongSql = "sql:SELECT o.id, o.number, o.created_at, c.name, c.email, SUM(l.amount) AS total FROM orders o " + "JOIN customers c ON c.id = o.customer_id JOIN order_lines l ON l.order_id = o.id " + "WHERE o.status = :#status AND o.created_at > :#since GROUP BY o.id, o.number, o.created_at, c.name, c.email" + "?dataSource=main&outputType=SelectList&useIterator=true"; private const string LongHttp = "https://api.partner.example/v2/orders/export?format=csv&fields=id,number,customer,total,status,created,updated" + "&since=2026-01-01&until=2026-12-31&page_size=500&apiKey=k-123456789&inboundAuth=basic&inboundPassword=s3cret"; private const string As4 = "as4://0.0.0.0:8443/as4?connectionFactory=node&partner=acme&timeout=15000"; [Fact] public void ShortUri_IsLoggedWhole() { EndpointUri.ForLog(As4).Should().Be(As4); EndpointUri.ForLog("kafka://orders?brokers=localhost:9092&password=x").Should().Be("kafka://orders?brokers=localhost:9092&password=****"); } [Fact] public void LongSql_IsCutAtAParameterBoundaryOrTheLength_WithWhatIsLeftAndAHash() { var form = EndpointUri.ForLog(LongSql); form.Should().MatchRegex(@"^sql:SELECT o\.id, .*\.\.\. \[\+\d+ chars #[0-9a-f]{8}\]$"); form.Length.Should().BeLessThan(EndpointUri.LogLength + 30); var shown = form[..form.IndexOf("... [", StringComparison.Ordinal)]; var left = int.Parse(form[(form.IndexOf("[+", StringComparison.Ordinal) + 2)..form.IndexOf(" chars", StringComparison.Ordinal)]); (shown.Length + left).Should().Be(LongSql.Length); } [Fact] public void LongQueryString_IsCutBeforeAParameter_AndKeepsSecretsMasked() { var form = EndpointUri.ForLog(LongHttp); form.Should().NotContain("s3cret").And.NotContain("k-123456789"); var shown = form[..form.IndexOf("... [", StringComparison.Ordinal)]; EndpointUri.Sanitize(LongHttp).Should().StartWith(shown); "?&".Should().Contain(EndpointUri.Sanitize(LongHttp)[shown.Length].ToString(), "the cut falls before a parameter"); } [Fact] public void ACutThatWouldFallInsideAMask_KeepsTheMaskWhole() { // "x://h?" then a long secret parameter name: the only boundary is the '?' at 5 (below half the length), so the // cut falls at the length, which is inside the mask that starts at 7 + 111 = 118. var key = new string('p', 103) + "password"; var uri = $"x://h?{key}=secretvalue&more=1"; var full = EndpointUri.Sanitize(uri); var maskAt = full.IndexOf(EndpointUri.Redacted, StringComparison.Ordinal); maskAt.Should().BeLessThan(EndpointUri.LogLength); (maskAt + EndpointUri.Redacted.Length).Should().BeGreaterThan(EndpointUri.LogLength, "the plain cut would fall inside the mask"); var form = EndpointUri.ForLog(uri); form.Should().StartWith(full[..(maskAt + EndpointUri.Redacted.Length)] + "... [", "the mask is kept whole"); form.Should().NotContain("secretvalue"); } [Fact] public void TwoLongUrisWithTheSamePrefix_ReadApart() { var a = EndpointUri.ForLog(LongSql); var b = EndpointUri.ForLog(LongSql.Replace("useIterator=true", "useIterator=false")); a.Should().NotBe(b); a[..a.IndexOf("... [", StringComparison.Ordinal)].Should().Be(b[..b.IndexOf("... [", StringComparison.Ordinal)], "they differ only after the cut"); a[(a.IndexOf('#') + 1)..].Should().NotBe(b[(b.IndexOf('#') + 1)..], "the hash of the whole URI tells them apart"); } [Fact] public void BeforeAndAfter_ForTheReport() { foreach (var uri in new[] { LongSql, LongHttp, As4 }) { output.WriteLine("before: " + EndpointUri.Sanitize(uri)); output.WriteLine("after: " + EndpointUri.ForLog(uri)); output.WriteLine(""); } } [Theory] [InlineData(LogLevel.Information, false)] [InlineData(LogLevel.Debug, true)] public async Task CompiledRouteAndEndpointStartedLines_AreShort_AndWholeAtDebug(LogLevel level, bool whole) { var capture = new CapturingLoggerProvider(); using var loggerFactory = LoggerFactory.Create(b => b.AddProvider(capture).SetMinimumLevel(level)); var longDirect = "direct://" + new string('q', 200) + "?timeout=5000"; await using var context = new RouteContext(loggerFactory: loggerFactory); context.AddRoutes(r => r.From(longDirect).RouteId("long-route").To("mock:out")); await context.Start(); var lines = capture.Entries.Where(e => e.Level == LogLevel.Information && (e.Message.StartsWith("Compiled route 'long-route'", StringComparison.Ordinal) || e.Message.StartsWith("Endpoint direct://", StringComparison.Ordinal))) .Select(e => e.Message).ToList(); lines.Should().HaveCountGreaterThanOrEqualTo(2); foreach (var line in lines) { if (whole) line.Should().Contain(longDirect).And.NotContain("chars #"); else line.Should().NotContain(longDirect).And.MatchRegex(@"\.\.\. \[\+\d+ chars #[0-9a-f]{8}\]"); } } [Theory] [InlineData(LogLevel.Information, false)] [InlineData(LogLevel.Debug, true)] public async Task ConnectorStartAndStopLines_AreShort_AndWholeAtDebug(LogLevel level, bool whole) { var capture = new CapturingLoggerProvider(); using var loggerFactory = LoggerFactory.Create(b => b.AddProvider(capture).SetMinimumLevel(level)); var longDirect = "direct://" + new string('d', 200); var longSeda = "seda://" + new string('s', 200); await using (var context = new RouteContext(loggerFactory: loggerFactory)) { context.AddRoutes(r => { r.From(longDirect).RouteId("to-seda").To(longSeda); r.From(longSeda).RouteId("from-seda").To("mock:out"); }); await context.Start(); await context.Stop(); } // The Direct consumer (its own lines) and the SEDA consumer (the shared DrainableConsumer line), each started and // stopped. var lines = capture.Entries.Where(e => e.Level == LogLevel.Information && (e.Message.Contains("dddd", StringComparison.Ordinal) || e.Message.Contains("ssss", StringComparison.Ordinal)) && (e.Message.Contains(" started", StringComparison.Ordinal) || e.Message.Contains(" stopped", StringComparison.Ordinal))) .Select(e => e.Message).ToList(); lines.Should().Contain(l => l.StartsWith("Direct consumer started: ", StringComparison.Ordinal)); lines.Should().Contain(l => l.StartsWith("seda://", StringComparison.Ordinal) && l.EndsWith(" started.", StringComparison.Ordinal)); foreach (var line in lines) { if (whole) line.Should().Match(l => l.Contains(longDirect) || l.Contains(longSeda)).And.NotContain("chars #"); else line.Should().NotContain(longDirect).And.NotContain(longSeda).And.MatchRegex(@"\.\.\. \[\+\d+ chars #[0-9a-f]{8}\]"); } } }