PreviousNext

How to unit test a class that consumes an HttpClient with IHttpClientFactory in ASP.NET Core?

by bill-s, 2018-12-28T13:34:24.096Z

A few years ago, Microsoft introduced the HttpClient class as a modern substitute for HttpWebRequest to make web requests from .NET applications. Not only is this new API much easier to use, cleaner, and asynchronous, but it is also easily expandable. The HttpClient class has a constructor that accepts a HttpMessageHandler. The latter is an object that accepts a request (HttpRequestMessage) and returns a response (HttpResponseMessage); the way it does it is completely dependent on the implementation. By default, HttpClient uses HttpClientHandler, a handler that sends a request to a server on the network and returns the response from the server. In this article we will create our own implementation of an HttpMessageHandler by inheriting an abstract class named DelegatingHandler. Finally, for all this to be possible, HttpClient must not be used directly, but used with the dependency injection that allow mocking by using IHttpClientFactory interface.

Read More