Unit Testing HttpClient In An ASP.NET MVC Controller

November 7 2013

The nice thing about HttpClient is that it was designed with unit testing in mind, unlike previous .NET http objects like WebClient and WebRequest which required you to write wrappers. As such you don’t need to use dependency injection in order to unit test HttpClient, which keeps your code that much more readable.

So, how to do it?

First, you need to pass HttpClient into the controller’s constructor in your MVC project. Again, the nice thing is that you don’t have to do dependency injection for this with a framework like Unity. The easiest way to do that is via constructor chaining, like this:

public class MyController: Controller
  private readonly HttpClient _httpClient;

  public MyController() : this(new HttpClient())

  public MyController(HttpClient httpClient)
    _httpClient= httpClient;


Then, in your tests, you’ll need to fashion up a FakeHandler like this:

 public class FakeHandler : DelegatingHandler
            public HttpResponseMessage Response { get; set; }

            protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
                                                        CancellationToken cancellationToken)
                return Task.Factory.StartNew(() => Response);

which you will use when you new up your HttpClient in your tests. Now, in you’ll need to set up the HttpResponseMessage before you instantiate the HttpClient in your tests. If you are expecting string content in the response, in the case of getting JSON for instance, here’s how that might look:

string json;
using (StreamReader reader = new StreamReader("json.txt"))
        json = reader.ReadToEnd();
var response = new HttpResponseMessage(HttpStatusCode.OK);
byte[] array = Encoding.ASCII.GetBytes(json);
ByteArrayContent content = new ByteArrayContent(array);
response.Content = content;

Here we are grabbing some pre crafted JSON from the file system, throwing it in a byte array and then giving that to our HttpResponseMessage.

Now, that we have our expected response set up, we use that in our FakeHandler when we instantiate the HttpClient for the test:

var httpClient = new HttpClient(new FakeHandler
       Response = response,
       InnerHandler = new HttpClientHandler()
MyContoller myController = new MyController(httpClient);

And, there you have it!