38 lines
1.1 KiB
C#
38 lines
1.1 KiB
C#
using System;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace NetClient.Test.TestDoubles;
|
|
|
|
public sealed class TestHttpMessageHandler : HttpMessageHandler
|
|
{
|
|
private readonly Func<HttpRequestMessage, Task<HttpResponseMessage>> _handler;
|
|
private int _calls;
|
|
|
|
public int Calls => _calls;
|
|
|
|
public TestHttpMessageHandler(Func<HttpRequestMessage, Task<HttpResponseMessage>> handler)
|
|
{
|
|
_handler = handler;
|
|
}
|
|
|
|
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
Interlocked.Increment(ref _calls);
|
|
return await _handler(request);
|
|
}
|
|
|
|
public static HttpResponseMessage Json(HttpStatusCode code, string json)
|
|
{
|
|
return new HttpResponseMessage(code)
|
|
{ Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json") };
|
|
}
|
|
|
|
public static HttpResponseMessage Text(HttpStatusCode code, string text)
|
|
{
|
|
return new HttpResponseMessage(code) { Content = new StringContent(text) };
|
|
}
|
|
} |