Path: redb-identity/tests/redb.Identity.Tests/OpenIddict/Adapter/ApplyHandlerTests.cs Snapshot: redb-identity, 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-identity Page: https://redbase.app/src/dev-2026-12-06/redb-identity/tests/redb.Identity.Tests/OpenIddict/Adapter/ApplyHandlerTests.cs ---- using System.Text.Json; using AwesomeAssertions; using Microsoft.Extensions.Options; using OpenIddict.Abstractions; using OpenIddict.Server; using redb.Identity.Core.Configuration; using redb.Identity.Core.OpenIddict; using redb.Identity.Core.OpenIddict.Handlers; using redb.Identity.Tests.Infrastructure; using redb.Route.Abstractions; using Xunit; using static OpenIddict.Server.OpenIddictServerEvents; namespace redb.Identity.Tests.Adapter; public class ApplyHandlerTests { private static (OpenIddictServerTransaction transaction, TestExchange exchange) CreateTransactionWithExchange() { var exchange = new TestExchange(); var transaction = new OpenIddictServerTransaction(); transaction.Properties[RedbRouteOpenIddictServerHelpers.ExchangePropertyKey] = exchange; return (transaction, exchange); } [Fact] public async Task ApplyTokenResponse_SuccessResponse_WritesToExchangeOut() { var (transaction, exchange) = CreateTransactionWithExchange(); transaction.Response = new OpenIddictResponse(); transaction.Response.AccessToken = "eyJ-test-token"; transaction.Response.TokenType = "Bearer"; transaction.Response.ExpiresIn = 3600; var context = new ApplyTokenResponseContext(transaction); await new ApplyTokenResponseHandler().HandleAsync(context); exchange.Out.Should().NotBeNull(); exchange.Out!.ContentType.Should().Be("application/json"); exchange.Pattern.Should().Be(ExchangePattern.InOut); context.IsRequestHandled.Should().BeTrue(); var body = exchange.Out.Body.Should().BeOfType>().Subject; body["access_token"].Should().Be("eyJ-test-token"); body["token_type"].Should().Be("Bearer"); body["expires_in"].Should().Be(3600L); } [Fact] public async Task ApplyTokenResponse_ErrorResponse_WritesErrorToExchangeOut() { var (transaction, exchange) = CreateTransactionWithExchange(); transaction.Response = new OpenIddictResponse { Error = "invalid_client", ErrorDescription = "Unknown client" }; var context = new ApplyTokenResponseContext(transaction); await new ApplyTokenResponseHandler().HandleAsync(context); exchange.Out.Should().NotBeNull(); context.IsRequestHandled.Should().BeTrue(); var body = exchange.Out!.Body.Should().BeOfType>().Subject; body["error"].Should().Be("invalid_client"); body["error_description"].Should().Be("Unknown client"); } [Fact] public async Task ApplyTokenResponse_NoExchange_Skips() { var transaction = new OpenIddictServerTransaction(); transaction.Response = new OpenIddictResponse(); var context = new ApplyTokenResponseContext(transaction); await new ApplyTokenResponseHandler().HandleAsync(context); context.IsRequestHandled.Should().BeFalse(); } [Fact] public async Task ApplyAuthorizationResponse_WritesToExchangeOut() { var (transaction, exchange) = CreateTransactionWithExchange(); transaction.Response = new OpenIddictResponse(); transaction.Response.Code = "auth-code-123"; var context = new ApplyAuthorizationResponseContext(transaction); await new ApplyAuthorizationResponseHandler().HandleAsync(context); exchange.Out.Should().NotBeNull(); context.IsRequestHandled.Should().BeTrue(); var body = exchange.Out!.Body.Should().BeOfType>().Subject; body["code"].Should().Be("auth-code-123"); } [Fact] public async Task ApplyIntrospectionResponse_WritesToExchangeOut() { var (transaction, exchange) = CreateTransactionWithExchange(); transaction.Response = new OpenIddictResponse(); transaction.Response.SetParameter("active", new OpenIddictParameter(true)); var context = new ApplyIntrospectionResponseContext(transaction); await new ApplyIntrospectionResponseHandler().HandleAsync(context); exchange.Out.Should().NotBeNull(); context.IsRequestHandled.Should().BeTrue(); } [Fact] public async Task ApplyRevocationResponse_WritesToExchangeOut() { var (transaction, exchange) = CreateTransactionWithExchange(); transaction.Response = new OpenIddictResponse(); var context = new ApplyRevocationResponseContext(transaction); await new ApplyRevocationResponseHandler().HandleAsync(context); exchange.Out.Should().NotBeNull(); context.IsRequestHandled.Should().BeTrue(); } [Fact] public async Task ApplyDiscoveryResponse_WritesToExchangeOut() { var (transaction, exchange) = CreateTransactionWithExchange(); transaction.Response = new OpenIddictResponse(); transaction.Response.SetParameter("issuer", new OpenIddictParameter("https://identity.local/")); var context = new ApplyConfigurationResponseContext(transaction); await new ApplyDiscoveryResponseHandler(Options.Create(new RedbIdentityOptions())).HandleAsync(context); exchange.Out.Should().NotBeNull(); context.IsRequestHandled.Should().BeTrue(); var body = exchange.Out!.Body.Should().BeOfType>().Subject; body["issuer"].Should().Be("https://identity.local/"); } [Theory] [InlineData(false, true, true, true)] // JAR off: nothing about request_uri is published [InlineData(true, true, true, true)] [InlineData(true, true, false, false)] [InlineData(true, false, true, null)] // request_uri off: the flag is not published public async Task ApplyDiscoveryResponse_PublishesRequireRequestUriRegistration( bool enableJar, bool enableRequestUri, bool requireRegistration, bool? expected) { var (transaction, exchange) = CreateTransactionWithExchange(); transaction.Response = new OpenIddictResponse(); transaction.Response.SetParameter("issuer", new OpenIddictParameter("https://identity.local/")); var options = new RedbIdentityOptions(); options.Features.EnableJar = enableJar; options.Jar.EnableRequestUri = enableRequestUri; options.Jar.RequireRequestUriRegistration = requireRegistration; await new ApplyDiscoveryResponseHandler(Options.Create(options)).HandleAsync(new ApplyConfigurationResponseContext(transaction)); var body = exchange.Out!.Body.Should().BeOfType>().Subject; if (!enableJar || expected is null) body.Should().NotContainKey("require_request_uri_registration"); else body["require_request_uri_registration"]!.ToString()!.ToLowerInvariant().Should().Be(expected.Value ? "true" : "false"); } [Fact] public async Task ApplyUserinfoResponse_WritesToExchangeOut() { var (transaction, exchange) = CreateTransactionWithExchange(); transaction.Response = new OpenIddictResponse(); transaction.Response.SetParameter("sub", new OpenIddictParameter("user-123")); var context = new ApplyUserInfoResponseContext(transaction); await new ApplyUserinfoResponseHandler().HandleAsync(context); exchange.Out.Should().NotBeNull(); context.IsRequestHandled.Should().BeTrue(); var body = exchange.Out!.Body.Should().BeOfType>().Subject; body["sub"].Should().Be("user-123"); } [Fact] public async Task ApplyTokenResponse_PreservesExistingOut() { var (transaction, exchange) = CreateTransactionWithExchange(); transaction.Response = new OpenIddictResponse { AccessToken = "tok" }; // Pre-create Out message exchange.Out = new TestMessage(); var context = new ApplyTokenResponseContext(transaction); await new ApplyTokenResponseHandler().HandleAsync(context); // Should reuse existing Out, not create a new one exchange.Out.Should().NotBeNull(); var body = exchange.Out!.Body.Should().BeOfType>().Subject; body["access_token"].Should().Be("tok"); } [Fact] public async Task ApplyTokenResponse_NormalizesJsonElementValues() { var (transaction, exchange) = CreateTransactionWithExchange(); transaction.Response = new OpenIddictResponse(); // SetParameter with JsonElement values (as OpenIddict may produce internally) transaction.Response.SetParameter("string_val", new OpenIddictParameter(JsonSerializer.Deserialize("\"hello\""))); transaction.Response.SetParameter("number_val", new OpenIddictParameter(JsonSerializer.Deserialize("42"))); transaction.Response.SetParameter("bool_val", new OpenIddictParameter(JsonSerializer.Deserialize("true"))); var context = new ApplyTokenResponseContext(transaction); await new ApplyTokenResponseHandler().HandleAsync(context); var body = exchange.Out!.Body.Should().BeOfType>().Subject; // JsonElement should be unwrapped to native types, not left as JsonElement body["string_val"].Should().BeOfType().And.Be("hello"); body["number_val"].Should().NotBeOfType("numbers should be unwrapped"); Convert.ToInt64(body["number_val"]).Should().Be(42L); body["bool_val"].Should().BeOfType().And.Be(true); } }