JakeCarpenter.MockHttp.Extensions 1.3.0

dotnet add package JakeCarpenter.MockHttp.Extensions --version 1.3.0
                    
NuGet\Install-Package JakeCarpenter.MockHttp.Extensions -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="JakeCarpenter.MockHttp.Extensions" Version="1.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JakeCarpenter.MockHttp.Extensions" Version="1.3.0" />
                    
Directory.Packages.props
<PackageReference Include="JakeCarpenter.MockHttp.Extensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add JakeCarpenter.MockHttp.Extensions --version 1.3.0
                    
#r "nuget: JakeCarpenter.MockHttp.Extensions, 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.
#:package JakeCarpenter.MockHttp.Extensions@1.3.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=JakeCarpenter.MockHttp.Extensions&version=1.3.0
                    
Install as a Cake Addin
#tool nuget:?package=JakeCarpenter.MockHttp.Extensions&version=1.3.0
                    
Install as a Cake Tool

Extensions for RichardSzalay.MockHttp

Partial JSON matching

Match the provided properties from an object to the request intercepted by the MockHttp message handler.

  • Additional properties on a JSON object request will be ignored
  • Works with JSON objects or arrays as root
  • Objects/values that are not a direct child of the root element will be compared using "deep equals"

Examples

// use RichardSzalay.MockHttp HttpMessageHandler
var handler = new MockHttpMessageHandler();
var mockedRequest = handler.When("/my/url");

// Single property object expected in the request.
mockedRequest.WithPartialJson(new { a = "1" });

// Multi-property object expected in the request
mockedRequest.WithPartialJson(new { a = "1", b = 2 });

// Array used as the root of the JSON
mockedRequest.WithPartialJson(new [] { 1, 2, 3 });

Request helpers

ExpectPath

Equivalent to Expect(method, "*" + path), adding a wildcard for everything before the path in a URI which allows SUT HTTP requests to go to an arbitrary host when not important.

// use RichardSzalay.MockHttp HttpMessageHandler
var handler = new MockHttpMessageHandler();

// Expect a POST to /foo/bar on an unimportant host
var mockedRequest = handler.ExpectPath(HttpMethod.Post, "/foo/bar").Respond(HttpStatusCode.OK);

Response helpers

These extension methods are available for shorter setup for responses.

RespondWith

Add a response to the mocked request with a fluent interface. Sets up the response with HTTP 200 by default.

// use RichardSzalay.MockHttp HttpMessageHandler
var handler = new MockHttpMessageHandler();
var mockedRequest = handler.When("/my/url");

// Respond with HTTP 401
mockedRequest.RespondWith(with => with.StatusCode(HttpStatusCode.Unauthorized));

// Include a JSON object serialized in the response (and HTTP 200 implicitly)
mockedRequest.RespondWith(with => with.JsonObject(new { foo = "bar" }));

// Include a JSON string in the response
mockedRequest.RespondWith(with => 
    with.StatusCode(HttpStatusCode.NotFound)
    .JsonString("""{"reason": "not found"}"""));

RespondToAnyRequestWith

Add a request setup directly on an instance of MockHttpMessageHandler with a wildcard URL matcher to match any request and a fluent interface to setup the response (similar to RespondWith).

var handler = new MockHttpMessageHandler();

// Respond to "*" with HTTP 401
handler.RespondToAnyRequestWith(with => with.StatusCode(HttpStatusCode.Unauthorized));

// Include a JSON object serialized in the response to "*" (and HTTP 200 implicitly)
handler.RespondToAnyRequestWith(with => with.JsonObject(new { foo = "bar" }));

// Include a JSON string in the response to "*"
handler.RespondToAnyRequestWith(with => 
    with.StatusCode(HttpStatusCode.NotFound)
    .JsonString("""{"reason": "not found"}"""));
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 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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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 5,329 2/11/2023
1.2.0 457 1/15/2023
1.1.0 410 1/9/2023
1.0.1 419 1/8/2023