Path: redb-identity/tests/redb.Identity.Client.Tests/TestKit/FakeHttpMessageHandler.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/FakeHttpMessageHandler.cs
----
using System.Net;
using System.Text;
namespace redb.Identity.Client.Tests.TestKit;
///
/// In-memory driven by a queued response builder.
/// Records all incoming requests so tests can assert path / method / body / headers.
///
internal sealed class FakeHttpMessageHandler : HttpMessageHandler
{
private readonly Func> _responder;
public List Requests { get; } = new();
public List RequestBodies { get; } = new();
public FakeHttpMessageHandler(Func> responder)
{
_responder = responder;
}
public FakeHttpMessageHandler(HttpStatusCode status, string? body = null, string mediaType = "application/json")
: this(_ => Task.FromResult(BuildResponse(status, body, mediaType)))
{
}
/// Answers a request with this handler's responder, without recording it.
public Task SendForTest(HttpRequestMessage request) => _responder(request);
public static HttpResponseMessage BuildResponse(HttpStatusCode status, string? body, string mediaType = "application/json")
{
var resp = new HttpResponseMessage(status);
if (body is not null)
{
resp.Content = new StringContent(body, Encoding.UTF8, mediaType);
}
return resp;
}
protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Requests.Add(request);
if (request.Content is not null)
{
RequestBodies.Add(await request.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false));
}
else
{
RequestBodies.Add(null);
}
return await _responder(request).ConfigureAwait(false);
}
}