Your interface exposes the concrete HttpClient
class, therefore any classes that use this interface are tied to it, this means that it cannot be mocked.
HttpClient
does not inherit from any interface so you will have to write your own. I suggest a decorator-like pattern:
public interface IHttpHandler
{
HttpResponseMessage Get(string url);
HttpResponseMessage Post(string url, HttpContent content);
Task<HttpResponseMessage> GetAsync(string url);
Task<HttpResponseMessage> PostAsync(string url, HttpContent content);
}
And your class will look like this:
public class HttpClientHandler : IHttpHandler
{
private HttpClient _client = new HttpClient();
public HttpResponseMessage Get(string url)
{
return GetAsync(url).Result;
}
public HttpResponseMessage Post(string url, HttpContent content)
{
return PostAsync(url, content).Result;
}
public async Task<HttpResponseMessage> GetAsync(string url)
{
return await _client.GetAsync(url);
}
public async Task<HttpResponseMessage> PostAsync(string url, HttpContent content)
{
return await _client.PostAsync(url, content);
}
}
The point in all of this is that HttpClientHandler
creates its own HttpClient
, you could then of course create multiple classes that implement IHttpHandler
in different ways.
The main issue with this approach is that you are effectively writing a class that just calls methods in another class, however you could create a class that inherits from HttpClient
(See Nkosi's example, it's a much better approach than mine). Life would be much easier if HttpClient
had an interface that you could mock, unfortunately it does not.
This example is not the golden ticket however. IHttpHandler
still relies on HttpResponseMessage
, which belongs to System.Net.Http
namespace, therefore if you do need other implementations other than HttpClient
, you will have to perform some kind of mapping to convert their responses into HttpResponseMessage
objects. This of course is only a problem if you need to use multiple implementations of IHttpHandler
but it doesn't look like you do so it's not the end of the world, but it's something to think about.
Anyway, you can simply mock IHttpHandler
without having to worry about the concrete HttpClient
class as it has been abstracted away.
I recommend testing the non-async methods, as these still call the asynchronous methods but without the hassle of having to worry about unit testing asynchronous methods, see here