A:初版
This commit is contained in:
1 parent
237cbd8788
commit
ab3721912d
102 files changed
+7929
-20
No files matched your search
@@ -0,0 +1,32 @@
|
||||
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)
|
||||
=> new(code) { Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json") };
|
||||
|
||||
public static HttpResponseMessage Text(HttpStatusCode code, string text)
|
||||
=> new(code) { Content = new StringContent(text) };
|
||||
}
|
||||
Reference in new issue
Block a user