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
<PackageReference Include="JakeCarpenter.MockHttp.Extensions" Version="1.3.0" />
<PackageVersion Include="JakeCarpenter.MockHttp.Extensions" Version="1.3.0" />
<PackageReference Include="JakeCarpenter.MockHttp.Extensions" />
paket add JakeCarpenter.MockHttp.Extensions --version 1.3.0
#r "nuget: JakeCarpenter.MockHttp.Extensions, 1.3.0"
#:package JakeCarpenter.MockHttp.Extensions@1.3.0
#addin nuget:?package=JakeCarpenter.MockHttp.Extensions&version=1.3.0
#tool nuget:?package=JakeCarpenter.MockHttp.Extensions&version=1.3.0
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 | Versions 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. |
-
net6.0
- RichardSzalay.MockHttp (>= 6.0.0)
- SystemTextJson.JsonDiffPatch (>= 1.3.1)
-
net7.0
- RichardSzalay.MockHttp (>= 6.0.0)
- SystemTextJson.JsonDiffPatch (>= 1.3.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.