Path: redb-route/tests/redb.Route.Tests/Core/OptionGroupTests.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/OptionGroupTests.cs
----
using AwesomeAssertions;
using redb.Route.Abstractions;
using redb.Route.Core;
namespace redb.Route.Tests.Core;
///
/// Option groups (): Apache Camel's prefixed options, group.member=value, bound
/// by the one binder; an unknown member is refused naming the group and its members; [Sensitive] members are masked as
/// group.member; endpoints without groups bind exactly as before.
///
public sealed class OptionGroupTests
{
public sealed class Filter
{
[OptionGroupValue] public string? Reference { get; set; }
public bool Unseen { get; set; } = true;
public string? Subject { get; set; }
public int Limit { get; set; }
[Sensitive] public string? Token { get; set; }
public DynamicValue? Label { get; set; }
}
public sealed class Plain
{
public string? Name { get; set; }
}
public sealed class GroupedOptions : EndpointOptions
{
public string? Host { get; set; }
[EndpointRole(EndpointRole.Consumer)] [OptionGroup] public Filter? SearchTerm { get; set; }
[OptionGroup] public Plain? Extra { get; set; }
public override void Validate() { }
}
/// A plain option named like the group of another endpoint: not a group, binds as ever.
public sealed class PlainSearchTermOptions : EndpointOptions
{
public string? SearchTerm { get; set; }
public override void Validate() { }
}
[LenientProperties]
public sealed class LenientDottedOptions : EndpointOptions
{
public string? Host { get; set; }
public override void Validate() { }
}
private static GroupedOptions Bind(params (string Key, string Value)[] parameters)
{
var options = new GroupedOptions();
options.BindFromUri(parameters.ToDictionary(p => p.Key, p => p.Value, StringComparer.OrdinalIgnoreCase));
return options;
}
[Fact]
public void Members_bind_into_the_group_which_is_created_on_first_use()
{
var options = Bind(("host", "h"), ("searchTerm.subject", "invoice"), ("SEARCHTERM.LIMIT", "5"), ("searchTerm.unseen", "false"));
options.Host.Should().Be("h");
options.SearchTerm.Should().NotBeNull();
options.SearchTerm!.Subject.Should().Be("invoice");
options.SearchTerm.Limit.Should().Be(5);
options.SearchTerm.Unseen.Should().BeFalse();
options.Extra.Should().BeNull("no member of it was written");
options.UnmappedParameters.Should().BeEmpty();
}
[Fact]
public void A_member_takes_an_expression_as_an_option_of_the_endpoint_does()
{
var options = Bind(("searchTerm.label", "${header.x}"));
options.SearchTerm!.Label!.Value.IsDynamic.Should().BeTrue();
}
[Fact]
public void The_bare_group_name_binds_the_value_member()
{
var options = Bind(("searchTerm", "#query"), ("searchTerm.subject", "x"));
options.SearchTerm!.Reference.Should().Be("#query");
options.SearchTerm.Subject.Should().Be("x");
}
[Fact]
public void An_unknown_member_is_refused_naming_the_group_and_its_members()
{
var act = () => Bind(("searchTerm.subjct", "x"));
act.Should().Throw()
.WithMessage("'searchTerm.subjct' is not an option of the Grouped endpoint: the group searchTerm takes searchTerm, " +
"searchTerm.unseen, searchTerm.subject, searchTerm.limit, searchTerm.token, searchTerm.label." +
" Did you mean 'searchTerm.subject'?*");
}
[Fact]
public void A_bare_group_without_a_value_member_is_refused_naming_its_members()
{
var act = () => Bind(("extra", "x"));
act.Should().Throw()
.WithMessage("'extra' is a group of options of the Grouped endpoint, written extra.member: extra.name.*");
}
[Fact]
public void A_value_that_does_not_convert_names_the_member()
{
var act = () => Bind(("searchTerm.limit", "many"));
act.Should().Throw().WithMessage("'searchTerm.limit=many' is not a valid value*");
}
[Fact]
public void BindableOptions_names_each_member_as_the_uri_writes_it()
{
EndpointOptions.BindableOptions(typeof(GroupedOptions)).Select(o => o.Name).Should().BeEquivalentTo(
"Host", "SearchTerm", "SearchTerm.Unseen", "SearchTerm.Subject", "SearchTerm.Limit", "SearchTerm.Token",
"SearchTerm.Label", "Extra.Name");
}
[Fact]
public void A_sensitive_member_is_masked_as_group_member()
{
var options = Bind(("searchTerm.token", "s3cret-token"));
EndpointUri.Sanitize("grouped:x?searchTerm.token=s3cret-token&host=h")
.Should().Be("grouped:x?searchTerm.token=****&host=h");
options.SensitiveValues().Should().Contain("s3cret-token");
}
[Fact]
public void A_member_belongs_to_the_side_of_its_group()
{
var uri = EndpointUriParser.Parse("grouped:x?searchTerm.subject=a&searchTerm=%23q&host=h");
EndpointOptions.WrittenForOtherRole(typeof(GroupedOptions), uri, EndpointRole.Producer)
.Should().BeEquivalentTo("searchTerm.subject", "searchTerm");
EndpointOptions.WrittenForOtherRole(typeof(GroupedOptions), uri, EndpointRole.Consumer).Should().BeEmpty();
}
[Fact]
public void A_plain_option_named_like_a_group_elsewhere_binds_as_before()
{
var plain = new PlainSearchTermOptions();
plain.BindFromUri(new Dictionary(StringComparer.OrdinalIgnoreCase) { ["searchTerm"] = "x" });
plain.SearchTerm.Should().Be("x");
var dotted = () => new PlainSearchTermOptions().BindFromUri(
new Dictionary(StringComparer.OrdinalIgnoreCase) { ["searchTerm.subject"] = "x" });
dotted.Should().Throw().WithMessage("'searchTerm.subject' is not an option of the PlainSearchTerm endpoint.*");
}
[Fact]
public void A_lenient_endpoint_keeps_dotted_parameters_it_has_no_group_for()
{
var options = new LenientDottedOptions();
options.BindFromUri(new Dictionary(StringComparer.OrdinalIgnoreCase)
{
["host"] = "h", ["param.id"] = "7", ["searchTerm.subject"] = "x",
});
options.UnmappedParameters.Should().BeEquivalentTo(new Dictionary
{
["param.id"] = "7", ["searchTerm.subject"] = "x",
});
}
[Fact]
public void The_short_log_form_cuts_before_a_member_not_inside_it()
{
var uri = "grouped:inbox?host=mail.example.com&searchTerm.subject=" + new string('s', 60) +
"&searchTerm.fromSentDate=now-24h&searchTerm.to=" + new string('t', 60);
var form = EndpointUri.ForLog(uri);
var kept = form[..form.IndexOf("... [", StringComparison.Ordinal)];
kept.Should().EndWith("&searchTerm.subject=" + new string('s', 60),
"the cut falls on the '&' before a member, so no member and no value is split");
}
public sealed class BadGroupOptions : EndpointOptions
{
[OptionGroup] public string? Text { get; set; }
public override void Validate() { }
}
[Fact]
public void A_group_whose_type_is_not_a_class_with_a_constructor_is_a_declaration_error()
{
var act = () => EndpointOptions.BindableOptions(typeof(BadGroupOptions));
act.Should().Throw().WithMessage("BadGroupOptions.Text is an [OptionGroup]*");
}
}