Goldlight.HttpClientTestSupport 1.3.0

dotnet add package Goldlight.HttpClientTestSupport --version 1.3.0
NuGet\Install-Package Goldlight.HttpClientTestSupport -Version 1.3.0
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="Goldlight.HttpClientTestSupport" Version="1.3.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Goldlight.HttpClientTestSupport --version 1.3.0
#r "nuget: Goldlight.HttpClientTestSupport, 1.3.0"
#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 Goldlight.HttpClientTestSupport as a Cake Addin
#addin nuget:?package=Goldlight.HttpClientTestSupport&version=1.3.0

// Install Goldlight.HttpClientTestSupport as a Cake Tool
#tool nuget:?package=Goldlight.HttpClientTestSupport&version=1.3.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 Compatible and additional computed target framework versions.
.NET 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.0 1,379 8/22/2023
1.2.0 1,434 9/14/2021
1.1.0 336 5/10/2021
1.0.0 319 5/7/2021