using System.Net;
using System.Text;
namespace ClaudeDo.Worker.Tests.Tickets;
///
/// Fakes the HTTP transport for BandelTicketClient tests. No test using this may touch the
/// network — reused by later tasks that also exercise the ticket client.
///
internal sealed class StubHandler : HttpMessageHandler
{
public readonly List Requests = new();
public readonly List Bodies = new();
private readonly Func _respond;
private readonly Exception? _throw;
public StubHandler(Func respond) => _respond = respond;
/// Lets a test reproduce a transport that blows up instead of answering.
public StubHandler(Exception throwOnSend)
{
_respond = _ => (HttpStatusCode.OK, "");
_throw = throwOnSend;
}
protected override async Task SendAsync(HttpRequestMessage request, CancellationToken ct)
{
Requests.Add(request);
Bodies.Add(request.Content is null ? "" : await request.Content.ReadAsStringAsync(ct));
if (_throw is not null) throw _throw;
var (code, body) = _respond(request);
return new HttpResponseMessage(code) { Content = new StringContent(body, Encoding.UTF8, "application/json") };
}
}