Path: redb-route/tests/redb.Route.Tests.Mail/MailSearchTermIntegrationTests.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.Mail/MailSearchTermIntegrationTests.cs ---- using System.Collections.Concurrent; using System.Text; using MailKit; using MailKit.Net.Imap; using MailKit.Net.Smtp; using MailKit.Search; using MimeKit; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.Mail; namespace redb.Route.Tests.Mail; /// /// Apache Camel's mail searchTerm against GreenMail (SMTP 3025, IMAP 3143, POP3 3110, API 8080): the simple options /// select the mails on IMAP (server SEARCH, exact date bounds on the client) and on POP3 (on the client), and /// searchTerm=#name takes a MailKit search query from the registry. /// [Trait("Category", "Integration")] public sealed class MailSearchTermIntegrationTests { private static readonly HttpClient Http = new(); private static async Task NewUserAsync() { var login = $"st-{Guid.NewGuid():N}"; using var content = new StringContent( $"{{\"email\":\"{login}@localhost\",\"login\":\"{login}\",\"password\":\"secret\"}}", Encoding.UTF8, "application/json"); using var _ = await Http.PostAsync("http://localhost:8080/api/user", content); return login; } private static async Task SendAsync(string to, string subject, DateTimeOffset? date = null) { var mime = new MimeMessage(); mime.From.Add(new MailboxAddress("Billing", "billing@acme.localhost")); mime.To.Add(new MailboxAddress(to, $"{to}@localhost")); mime.Subject = subject; if (date is { } d) mime.Date = d; mime.Body = new TextPart("plain") { Text = "body of " + subject }; using var smtp = new SmtpClient(); await smtp.ConnectAsync("localhost", 3025, MailKit.Security.SecureSocketOptions.None); await smtp.AuthenticateAsync(to, "secret"); await smtp.SendAsync(mime); await smtp.DisconnectAsync(true); } private sealed class Collect : IProcessor { public readonly ConcurrentQueue Subjects = new(); public Task Process(IExchange exchange, CancellationToken ct = default) { Subjects.Enqueue(exchange.In.GetHeader(MailHeaders.Subject) ?? ""); return Task.CompletedTask; } } private static async Task ConsumeAsync(IEndpoint endpoint, int expected, int quietMs = 2500) { var route = new Collect(); var consumer = endpoint.CreateConsumer(route); await consumer.Start(); var deadline = DateTime.UtcNow.AddSeconds(15); while (route.Subjects.Count < expected && DateTime.UtcNow < deadline) await Task.Delay(100); await Task.Delay(quietMs); // nothing else may come await consumer.Stop(); return [.. route.Subjects]; } private static IEndpoint Imap(string user, string query) => new ImapComponent().CreateEndpoint(EndpointUriParser.Parse( $"imap://localhost?port=3143&security=None&username={user}&password=secret&delay=300&{query}")); [Fact] public async Task Imap_takes_only_the_mails_the_terms_select() { var user = await NewUserAsync(); await SendAsync(user, "Invoice 1"); await SendAsync(user, "Newsletter"); // GreenMail 2.1.2 matches IMAP FROM against the whole address (FromTerm), where RFC 3501 and real servers take a // substring: the full address passes on both. var subjects = await ConsumeAsync(Imap(user, "searchTerm.subject=invoice&searchTerm.from=billing@acme.localhost&postProcess=Delete"), expected: 1); subjects.Should().Equal("Invoice 1"); } [Fact] public async Task Imap_checks_a_now_bound_against_the_exact_time() { var user = await NewUserAsync(); await SendAsync(user, "Old invoice", DateTimeOffset.Now.AddHours(-3)); await SendAsync(user, "New invoice", DateTimeOffset.Now.AddMinutes(-5)); var subjects = await ConsumeAsync(Imap(user, "searchTerm.fromSentDate=now-1h"), expected: 1); subjects.Should().Equal(["New invoice"], "IMAP SENTSINCE compares days; the consumer checks the hour"); } [Fact] public async Task Imap_unseen_false_takes_a_seen_mail_and_the_default_does_not() { var user = await NewUserAsync(); await SendAsync(user, "Seen invoice"); using (var imap = new ImapClient()) { await imap.ConnectAsync("localhost", 3143, MailKit.Security.SecureSocketOptions.None); await imap.AuthenticateAsync(user, "secret"); await imap.Inbox.OpenAsync(FolderAccess.ReadWrite); await imap.Inbox.AddFlagsAsync(await imap.Inbox.SearchAsync(SearchQuery.All), MessageFlags.Seen, silent: true); await imap.DisconnectAsync(true); } (await ConsumeAsync(Imap(user, "searchTerm.subject=invoice"), expected: 1)).Should().BeEmpty("searchTerm.unseen is true by default"); // A seen mail matches searchTerm.unseen=false on every poll; Delete takes it away once it went through. (await ConsumeAsync(Imap(user, "searchTerm.subject=invoice&searchTerm.unseen=false&postProcess=Delete"), expected: 1)) .Should().Equal("Seen invoice"); } [Fact] public async Task Imap_takes_a_search_query_from_the_registry_and_ignores_the_options_beside_it() { var user = await NewUserAsync(); await SendAsync(user, "alpha report"); await SendAsync(user, "beta report"); var route = new Collect(); await using var context = new RouteContext(); context.AddComponent(new ImapComponent()); context.AddToRegistry("alphaOnly", SearchQuery.SubjectContains("alpha")); context.AddRoutes(r => r.From( $"imap://localhost?port=3143&security=None&username={user}&password=secret&delay=300" + "&searchTerm=%23alphaOnly&searchTerm.subject=beta&postProcess=Delete") .Process(route)); await context.Start(); var deadline = DateTime.UtcNow.AddSeconds(15); while (route.Subjects.IsEmpty && DateTime.UtcNow < deadline) await Task.Delay(100); await Task.Delay(2500); await context.Stop(); route.Subjects.Should().Equal(["alpha report"], "the registry's query wins over the searchTerm options, as in Camel"); } [Fact] public async Task Pop3_checks_the_terms_on_the_client() { var user = await NewUserAsync(); await SendAsync(user, "Invoice 7"); await SendAsync(user, "Holiday"); var endpoint = new Pop3Component().CreateEndpoint(EndpointUriParser.Parse( $"pop3://localhost?port=3110&security=None&username={user}&password=secret&delay=300&searchTerm.subject=invoice&postProcess=Delete")); (await ConsumeAsync(endpoint, expected: 1)).Should().Equal("Invoice 7"); } [Fact] public async Task Pop3_refuses_at_start_what_it_cannot_check() { var user = await NewUserAsync(); var endpoint = new Pop3Component().CreateEndpoint(EndpointUriParser.Parse( $"pop3://localhost?port=3110&security=None&username={user}&password=secret&searchTerm.fromReceivedDate=now-1h")); var act = () => endpoint.CreateConsumer(new Collect()).Start(); await act.Should().ThrowAsync().WithMessage("*need the arrival time*"); } }