TestableHttpClient 0.9.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package TestableHttpClient --version 0.9.3                
NuGet\Install-Package TestableHttpClient -Version 0.9.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TestableHttpClient" Version="0.9.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TestableHttpClient --version 0.9.3                
#r "nuget: TestableHttpClient, 0.9.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TestableHttpClient as a Cake Addin
#addin nuget:?package=TestableHttpClient&version=0.9.3

// Install TestableHttpClient as a Cake Tool
#tool nuget:?package=TestableHttpClient&version=0.9.3                

TestableHttpClient

GitHub GitHub Workflow Status Nuget

Creating unittest for code that uses HttpClient can be difficult to test. It requires a custom HttpMessageHandler or a mocked version. TestableHttpClient provides a testable version of HttpMessageHandler and several helper functions to configure the TestableHttpHandler and several ways to assert which requests were made.

How to install

TestableHttpClient is released as a NuGet packages and can be installed via the NuGet manager in VisualStudio or by running the following command on the command line:

dotnet add package TestableHttpClient

How to use TestableHttpClient

The following code block shows the basic use case for asserting that certain requests are made:

TestableHttpMessageHandler testHandler = new();
HttpClient httpClient = new(testHandler); // or testHandler.CreateClient();

var result = await httpClient.GetAsync("http://httpbin.org/status/200");

testHandler.ShouldHaveMadeRequestsTo("https://httpbin.org/*");

The default response is an empty response message with a 200 OK StatusCode, in order to return real content the response need to be configured:

TestableHttpMessageHandler testHandler = new();
testHandler.RespondWith(Responses.Json(new { Hello: "World" }));
HttpClient httpClient = new(testHandler); // or testHandler.CreateClient();

var result = await httpClient.GetAsync("http://httpbin.org/status/200");

testHandler.ShouldHaveMadeRequestsTo("https://httpbin.org/*");

More examples can be found in the IntegrationTests project

Supported .NET versions

TestableHttpClient is build as a netstandard2.0 library, so theoretically it can work on every .NET version that support netstandard2.0. The following versions are being actively tested and thus supported:

  • .NET Framework 4.6, 4.7 and 4.8
  • .NET 6.0
  • .NET 7.0

These versions are supported as long as Microsoft supports them, we do our best to test and support newer versions as soon as possible.

Contributing

Please read CONTRIBUTING.md for details on how you can help us out.

Versioning

We use SemVer for versioning. For the versions available, see the releases on this repository.

Note that currently every library will always be released when a new tag is created, even though it might not have any changes.

Authors

See also the list of contributors who participated in this project.

License

This project is released under the MIT license, see LICENSE.md for more information.

Acknowledgments

This project is largely inspired by the HttpTest functionality from Flurl.
A lot of the ideas came from the thread about unit testing HttpClient code in this dotnet issue.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on TestableHttpClient:

Package Downloads
TestableHttpClient.NFluent

NFluent checks for checking HttpResponseMessages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.11.26 226 6/15/2024
0.10.5 696 12/3/2022
0.9.3 330 11/25/2022
0.8.8 540 11/8/2022
0.7.35 696 9/22/2022
0.6.6 565 2/24/2021
0.5.0 2,012 6/25/2020
0.4.0 862 5/26/2020
0.3.0 466 5/24/2020
0.2.0 441 5/10/2020
0.1.0 484 3/26/2020