Path: redb-identity/tests/redb.Identity.Client.Tests/TestKit/IdentityClientFixture.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.Client.Tests/TestKit/IdentityClientFixture.cs
----
using System.Net;
using System.Text.Json;
using Microsoft.Extensions.Options;
using redb.Identity.Client.Auth;
using redb.Identity.Client.Tests.TestKit;
namespace redb.Identity.Client.Tests.TestKit;
///
/// Builds an wired to a captured
/// so each endpoint test can assert request URI / method / body and stub a JSON response.
///
internal sealed class IdentityClientFixture
{
public FakeHttpMessageHandler Handler { get; }
public IdentityClient Client { get; }
/// The issuer; its discovery document names the protocol endpoints on the same host as the API.
public const string Authority = "https://identity.test/";
public IdentityClientFixture(Func responder)
{
Handler = new FakeHttpMessageHandler(req => Task.FromResult(responder(req)));
var http = new HttpClient(new DiscoveryAnswer(Handler)) { BaseAddress = new Uri("https://identity.test/") };
var opts = Options.Create(new IdentityClientOptions
{
BaseUrl = new Uri("https://identity.test/"),
Authority = new Uri(Authority),
});
Client = new IdentityClient(http, opts);
}
///
/// Answers the discovery document itself and hands every other request to , so the endpoint
/// tests see only the requests they make.
///
private sealed class DiscoveryAnswer(HttpMessageHandler inner) : DelegatingHandler(inner)
{
protected override Task SendAsync(HttpRequestMessage request, CancellationToken ct)
{
if (request.RequestUri!.ToString() != $"{Authority}.well-known/openid-configuration")
return base.SendAsync(request, ct);
return Task.FromResult(FakeHttpMessageHandler.BuildResponse(HttpStatusCode.OK, $$"""
{"issuer":"{{Authority}}",
"token_endpoint":"{{Authority}}connect/token",
"introspection_endpoint":"{{Authority}}connect/introspect",
"revocation_endpoint":"{{Authority}}connect/revocation",
"userinfo_endpoint":"{{Authority}}connect/userinfo",
"jwks_uri":"{{Authority}}.well-known/jwks"}
"""));
}
}
public IdentityClientFixture(HttpStatusCode status, string? body = null, string mediaType = "application/json")
: this(_ => FakeHttpMessageHandler.BuildResponse(status, body, mediaType))
{
}
public static string Json(T value) => JsonSerializer.Serialize(value, new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
});
}