Gazda.HttpMock 1.1.0

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

// Install Gazda.HttpMock as a Cake Tool
#tool nuget:?package=Gazda.HttpMock&version=1.1.0                

Gazda.HttpMock

Gazda.HttpMock is a library that expose tools for mocking the C# HttpClient class. It is especially useful for Unit Testing.


How to use

var mockHttpMessageHandler = new MockHttpMessageHandler();
var response = new HttpResponseMessage();
IMockResponse mockedResponse = mockHttpMessageHandler.PrepareMockResponse(response)
    .ForUrl(x => x.Contains("some_url"))
    .ForContent(x => x.ReadAsStream().Length > 0)
    .ForContent(async x => (await x.ReadAsStringAsync()) == "content");
    .ForMethod(HttpMethod.Post)
    .ForHeaders(x=>x.Contains("someHeaderName"))
    .For(x=> ...your custom logic...)
    .ForCustomMatch((IMockHttpMatcher) yourCustomMatcher);

HttpClient client = mockHttpMessageHandler.ToHttpClient();
client.SendAsync(new HttpRequestMessage());

int count = mockHttpMessageHandler.CountResponseReturns(mockedResponse);
bool check = mockHttpMessageHandler.AssertResponseReturned(mockedResponse, 2);


Available methods to prepare mock HttpResponseMessage to match some request.


All methods are extensions methods on IMockResponse interface, and they might be chained in fluent way.

IMockResponse mockedResponse;

mockedResponse.ForXYZ(...).ForABC(...);

Default MockHttpMatcher

.For(Predicate<HttpRequestMessage> p)

Predicate run against request (HttpRequestMessage). Example:

.For(x => x.Method == HttpMethod.Get)

MockHttpUrlMatcher

.ForUrl(Predicate<string> p)

Predicate run against request.RequestUri.ToString() (string). Example:

.For(x => x.Contains("url_part"))

MockHttpMethodMatcher

.ForMethod(HttpMethod method)

Predicate run against request.Method (HttpMethod). Example:

.ForMethod(HttpMethod.Post)

MockHttpHeaderMatcher

.ForHeaders(Predicate<HttpRequestHeaders> p)

Predicate run against request.Headers (HttpRequestHeaders). Example:

.ForHeaders(x => x.Contains("some_header"))

MockHttpContentMatcher

.ForContent(Predicate<HttpContent?> p)

Predicate run against request.Content (HttpContent?). Example:

.ForContent(x => x != null)

MockHttpContentAsyncMatcher

.ForContent(Func<HttpContent?, Task<bool>> p)

Predicate run against request.Content (HttpContent?). Example:

.ForContent(async x => (await x.ReadAsStringAsync()).Equals("hello"))

CustomMatcher

.ForCustomMatch(IMockHttpMatcher customMatcher)

Pass your own implementation of IMockHttpMatcher . Example:

IMockHttpMatcher customMatcher = new YourOwnCustomMatcher();
.ForCustomMatch(customMatcher)

What happens when no mocked response matches the request made by HttpClient?

If no mocked responses matches the request, default response is produced.
Response which is:

404 Not Found with content "No mocked response was found. Returning default."


You can also set your own default response using:

var mockHttpMessageHandler = new MockHttpMessageHandler();
var newDefaultResponse = new HttpResponseMessage();
mockHttpMessageHandler.SetDefaultResponse(newDefaultResponse);

Available methods to assert response was successfully returned from HttpClient.
var mockHttpMessageHandler = new MockHttpMessageHandler();
var response = new HttpResponseMessage();
IMockResponse mockedResponse = mockHttpMessageHandler.PrepareMockResponse(response)
var check = mockHttpMessageHandler.CheckOrAssertXYZ(mockedResponse);

Asserts that given response was returned 'n' times:

bool AssertResponseReturned(IMockResponse response, int n = 1)

Asserts that given response was not returned at all:

bool AssertResponseNotReturned(IMockResponse response)

Returns count of returns of given response:

int CountResponseReturns(IMockResponse response)

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.
  • net8.0

    • No dependencies.

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.1.0 238 11/15/2023
1.0.2 116 9/27/2023
1.0.1 104 9/20/2023
1.0.0 98 9/20/2023

1.1.0 - 2023-XI-15 - Update to .NET 8
           1.0.2 - 2023-IX-27 - GenerateDocumentationFile
           1.0.1 - 2023-IX-20 - Adds method descriptions and some fixes that should have been there from the beginning
           1.0.0 - 2023-IX-20 - First release