Skip to content

Instantly share code, notes, and snippets.

@iknowcodesoup
Created August 3, 2022 18:38
Show Gist options
  • Save iknowcodesoup/c25ca24351a04f3e6269b14e255d1f1e to your computer and use it in GitHub Desktop.
Save iknowcodesoup/c25ca24351a04f3e6269b14e255d1f1e to your computer and use it in GitHub Desktop.
MockHttpMessageHandler
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace Namespace123
{
public class MockHttpMessageHandler : HttpMessageHandler
{
private readonly HttpResponseMessage? message;
public MockHttpMessageHandler(HttpResponseMessage? message = null)
{
this.message = message;
}
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
return SendAsync(request);
}
public virtual Task<HttpResponseMessage> SendAsync(HttpRequestMessage request)
{
return Task.FromResult(
message ??
new HttpResponseMessage(System.Net.HttpStatusCode.OK)
{
RequestMessage = request
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment