Goldlight.HttpClientTestSupport
1.2.0
See the version list below for details.
dotnet add package Goldlight.HttpClientTestSupport --version 1.2.0
NuGet\Install-Package Goldlight.HttpClientTestSupport -Version 1.2.0
<PackageReference Include="Goldlight.HttpClientTestSupport" Version="1.2.0" />
paket add Goldlight.HttpClientTestSupport --version 1.2.0
#r "nuget: Goldlight.HttpClientTestSupport, 1.2.0"
// Install Goldlight.HttpClientTestSupport as a Cake Addin
#addin nuget:?package=Goldlight.HttpClientTestSupport&version=1.2.0
// Install Goldlight.HttpClientTestSupport as a Cake Tool
#tool nuget:?package=Goldlight.HttpClientTestSupport&version=1.2.0
This library provides developers with the ability to fake requests and responses to the HTTP Client, allowing us to easily mock HTTP calls.
When we want to unit test HttpClient
methods, we quickly run into the limitation that the actual HttpClient
calls, such as PostAsync
,
are not directly mockable. This means that we have a limitation whereby tests appear to need a live endpoint to test against and this
does not support the principles of unit testing where the tests are isolated from external sources. To work around this issue,
the HttpClient
accepts HttpMessageHandler
instances. Internally, all the calls are routed via the SendAsync
method so all we need to do is provide an implementation of this that can handle our test expectations.
This implementation provides the ability to cope with any content that is being transmitted in the request Content
,
as well as augmenting the response message to include the version, status codes and response headers.
Example
In the following example, the FakeHttpMessageHandler
is populated with an expectation that a valid ETag will be returned from
a Post
operation.
[Fact]
public async Task ValidateEagIsSetInResponseHeader()
{
FakeHttpMessageHandler fake = new FakeHttpMessageHandler().WithResponseHeader("ETag", "\"33a64df551425fcc55e4d42a148795d9f25f89d4\"");
HttpClient httpClient = new HttpClient(fake);
string convertedContent = JsonConvert.SerializeObject("MyContent");
StringContent stringContent = new StringContent(convertedContent, Encoding.UTF8, "application/json");
HttpResponseMessage response = await httpClient.PostWrapperAsync("http://www.dummyurl.com", stringContent);
Assert.Equal("\"33a64df551425fcc55e4d42a148795d9f25f89d4\"", response.Headers.ETag.Tag);
}
We can perform pre or post request handling to set up and manage expectations. In this example, we set up a pre-request that increments before every client invocation, using WithPreRequest
.
[Fact]
public async Task GivenPreActionForController_WhenProcessing_ThenActionIsPerformed()
{
int invocationCount = 0;
List<SampleModel> sample = new List<SampleModel>() { new SampleModel(), new SampleModel() };
FakeHttpMessageHandler fake = new FakeHttpMessageHandler().WithPreRequest(() => invocationCount++)
.WithExpectedContent(sample);
HttpClient httpClient = new HttpClient(fake);
ExampleControllerHandling exampleController = new ExampleControllerHandling(httpClient);
IEnumerable<SampleModel> output = await exampleController.GetAll();
Assert.Equal(1, invocationCount);
}
Similarly, the following example demonstrates the post request incrementing a counter when the client is invoked (twice in this case), using WithPostRequest
.
[Fact]
public async Task GivenPostActionForController_WhenProcessing_ThenActionIsPerformed()
{
int invocationCount = 0;
List<SampleModel> sample = new List<SampleModel>() { new SampleModel(), new SampleModel() };
FakeHttpMessageHandler fake = new FakeHttpMessageHandler().WithPostRequest(() => invocationCount++)
.WithExpectedContent(sample);
HttpClient httpClient = new HttpClient(fake);
ExampleControllerHandling exampleController = new ExampleControllerHandling(httpClient);
IEnumerable<SampleModel> output = await exampleController.GetAll();
await exampleController.GetAll();
Assert.Equal(2, invocationCount);
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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 is compatible. |
.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. |
-
.NETStandard 2.0
- System.Text.Json (>= 5.0.2)
-
.NETStandard 2.1
- System.Text.Json (>= 5.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.