roadie/Roadie.Api.Library.Tests/HttpClientFactoryBaseTests.cs

44 lines
No EOL
1.5 KiB
C#

using Moq;
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace Roadie.Library.Tests
{
public abstract class HttpClientFactoryBaseTests
{
protected readonly IHttpClientFactory _httpClientFactory;
public HttpClientFactoryBaseTests()
{
// var clientHandlerStub = new DelegatingHandlerStub();
// var client = new HttpClient(clientHandlerStub);
var client = new HttpClient();
var mockFactory = new Mock<IHttpClientFactory>();
mockFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(client);
_httpClientFactory = mockFactory.Object;
}
protected class DelegatingHandlerStub : DelegatingHandler
{
private readonly Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> _handlerFunc;
public DelegatingHandlerStub()
{
_handlerFunc = (request, cancellationToken) => Task.FromResult(request.CreateResponse(HttpStatusCode.OK));
}
public DelegatingHandlerStub(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> handlerFunc)
{
_handlerFunc = handlerFunc;
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return _handlerFunc(request, cancellationToken);
}
}
}
}