using System.Net;
using System.Text;
namespace CursorLang.Tests.Shared;
///
/// The network as the test writes it: the answer is decided here rather than
/// by a repository somewhere.
///
public sealed class FakeHttpHandler : HttpMessageHandler
{
private readonly Func _reply;
public FakeHttpHandler(Func reply) => _reply = reply;
public List Requests { get; } = [];
public static FakeHttpHandler Json(string json) => new(_ => new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(json, Encoding.UTF8, "application/json"),
});
public static FakeHttpHandler Status(HttpStatusCode status) =>
new(_ => new HttpResponseMessage(status));
public static FakeHttpHandler Bytes(byte[] content) => new(_ => new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(content),
});
public HttpClient CreateClient() => new(this);
protected override Task SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
Requests.Add(request);
return Task.FromResult(_reply(request));
}
}