Skip to content

Instantly share code, notes, and snippets.

@ByteDev
Last active August 12, 2021 03:26
Show Gist options
  • Save ByteDev/0055cb8754f304891e0b298012c9adec to your computer and use it in GitHub Desktop.
Save ByteDev/0055cb8754f304891e0b298012c9adec to your computer and use it in GitHub Desktop.
public class FakeResponseHandler : HttpMessageHandler
{
    private readonly HttpStatusCode _httpStatusCode;
    private readonly HttpContent _httpContent;

    public FakeResponseHandler(HttpStatusCode httpStatusCode) : this(httpStatusCode, null)
    {
    }

    public FakeResponseHandler(HttpStatusCode httpStatusCode, HttpContent httpContent)
    {
        _httpStatusCode = httpStatusCode;
        _httpContent = httpContent;
    }

    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var response = new HttpResponseMessage(_httpStatusCode);

        if (_httpContent != null)
            response.Content = _httpContent;
    
        return Task.FromResult(response);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment