👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Unit Testing HttpClient in dotnet

Unit Testing HttpClient in dotnet

http client

6 Articles


In this article, let's learn about how to Unit Test HTTP Client in .NET.

Note: If you have not done so already, I recommend you read the article on Extending HTTPClient with Custom Http Message Handlers in dotnet.

Table of Contents

  1. Introduction
  2. Why Unit Testing?
  3. What is a Http Client?
  4. Unit Testing Http Client
  5. Summary


In this article, lets focus on unit testing http client in dotnet. There are multiple ways to Unit Test HTTP Client in dotnet. Most of the examples you will find on the internet will be using Moq or NSubstitute. But I prefer to write Custom Http Message Handlers to Unit Test HTTP Client in dotnet.

Why Unit Testing ?

Unit testing allows developers to verify the behavior and logic of individual units of code in isolation. By writing tests, you can ensure that your code functions correctly and remains reliable even after making changes or adding new features. Unit tests help catch bugs early in the development process and provide confidence in the correctness of the code.

Note: If you have not done so already, I recommend you read the article on Implementing TDD in C# .Net.

What is a Http Client ?

Integrating with a web API is a common task required by many applications. Http Client is a class that simplifies the consumption of web APIs. It provides a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI. The HttpClient class is typically used to send and receive requests from a web service by using the HTTP protocol.

Unit Testing Http Client

You're working on a part of your application that relies on HttpClient to make API calls. Testing this can become cumbersome, as you don't want to actually call the API. Because it's not an integration test. Moreover, constantly calling a service that's cloud hosted can cost you quite a bit of money. You just want to test one specific piece of functionality that happens to rely on data being returned from an HTTP call.

Take error handling, for example. You want to ensure that when the API responds with a dreaded 401 Unauthorized status code that your application handles it gracefully. You don't want to actually call the API, though. You just want to ensure that your application handles the response correctly. This is where Custom Http Message Handlers come in handy. Well, we can write a custom handler to stop communication with the API and return the response required for our test. For our use case, we don't want to communicate with the API, but we do want to get back a 401 Unauthorized response.

The following code shown a simple implementation of a custom message handler that returns a 401 Unauthorized response. Notice that we are using HttpMessageHandler instead of DelegatingHandler. This is because we don't want to pass the request to the next handler in the pipeline. We want to return the response we want. So, we are using HttpMessageHandler to short circuit it.

Code Sample - Custom Message Handler to return 401 response

Now, we can use this custom message handler to unit test our http client. The following code shows a simple implementation of a unit test for http client. All we need to do is to pass our custom message handler to the http client and make the http call. The http call will be intercepted by our custom message handler and will return the response we want which is 401 UnAuthorized Status Code in our case. This will help you to assert your code against the response you want.

Code Sample - Unit Testing Http Client with Custom Message Handler


In this article, we learned about how to Unit Test HTTP Client in dotnet. We also learned about why we should write unit tests and what is a http client. We also learned about how to write custom http message handlers to unit test http client in dotnet. I hope you enjoyed reading this article. With this I'm concluding this HTTP Client series. Please share this with your network and help other dotnet devs.

  • Http Client
  • Unit Testing