Last active
June 8, 2021 19:39
-
-
Save jfoshee/85003d9e8213f58fa00e4dfc688e9960 to your computer and use it in GitHub Desktop.
Extensions for Mock<IHttpClientFactory> that help setup the HttpClient with fake response content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Moq; | |
using System; | |
using System.Net.Http; | |
namespace Example.Testing | |
{ | |
public static class HttpMocking | |
{ | |
public static void SetupHttpClient(this Mock<IHttpClientFactory> httpClientFactory, string response = "") | |
{ | |
var handler = new MockMessageHandler(request => | |
{ | |
return new HttpResponseMessage(System.Net.HttpStatusCode.OK) | |
{ | |
Content = new StringContent(response) | |
}; | |
}); | |
SetupHttpClient(httpClientFactory, handler); | |
} | |
public static void SetupHttpClient(this Mock<IHttpClientFactory> httpClientFactory, Uri expectedUrl, string response) | |
{ | |
var handler = new MockMessageHandler(request => | |
{ | |
if (request.RequestUri != expectedUrl) | |
throw new Exception("Wrong URI for HttpClient" | |
+ $"\n Expected: {expectedUrl}" | |
+ $"\n Actual: {request.RequestUri}"); | |
return new HttpResponseMessage(System.Net.HttpStatusCode.OK) | |
{ | |
Content = new StringContent(response) | |
}; | |
}); | |
SetupHttpClient(httpClientFactory, handler); | |
} | |
public static void SetupHttpClient(this Mock<IHttpClientFactory> httpClientFactory, string expectedUrl, string response) | |
{ | |
SetupHttpClient(httpClientFactory, new Uri(expectedUrl), response); | |
} | |
private static void SetupHttpClient(Mock<IHttpClientFactory> httpClientFactory, MockMessageHandler handler) | |
{ | |
var client = new HttpClient(handler); | |
httpClientFactory.Setup(f => f.CreateClient(It.IsAny<string>())) | |
.Returns(client); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net.Http; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace Example.Testing | |
{ | |
/// <summary> | |
/// HttpClient cannot be mocked, but the message handler can be replaced | |
/// See https://github.com/aspnet/HttpClientFactory/issues/67 | |
/// </summary> | |
public class MockMessageHandler : HttpMessageHandler | |
{ | |
private Func<HttpRequestMessage, HttpResponseMessage> _handler; | |
public MockMessageHandler(Func<HttpRequestMessage, HttpResponseMessage> handler) | |
{ | |
_handler = handler; | |
} | |
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | |
{ | |
return Task.FromResult(_handler(request)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment