using System.Net;
using System.Net.Http.Headers;

namespace GameList.Tests.Support;

internal class StubHttpMessageHandler : HttpMessageHandler
{
    private Func<HttpRequestMessage, HttpResponseMessage> _responder = DefaultResponder;

    public void SetResponder(Func<HttpRequestMessage, HttpResponseMessage> responder)
    {
        _responder = responder;
    }

    private static HttpResponseMessage DefaultResponder(HttpRequestMessage _)
    {
        var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent([]) };
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
        response.Content.Headers.ContentLength = 0;
        return response;
    }

    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        return Task.FromResult(_responder(request));
    }
}
