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