Enhanced.Testing.Component.WireMock
1.6.0
dotnet add package Enhanced.Testing.Component.WireMock --version 1.6.0
NuGet\Install-Package Enhanced.Testing.Component.WireMock -Version 1.6.0
<PackageReference Include="Enhanced.Testing.Component.WireMock" Version="1.6.0" />
<PackageVersion Include="Enhanced.Testing.Component.WireMock" Version="1.6.0" />
<PackageReference Include="Enhanced.Testing.Component.WireMock" />
paket add Enhanced.Testing.Component.WireMock --version 1.6.0
#r "nuget: Enhanced.Testing.Component.WireMock, 1.6.0"
#:package Enhanced.Testing.Component.WireMock@1.6.0
#addin nuget:?package=Enhanced.Testing.Component.WireMock&version=1.6.0
#tool nuget:?package=Enhanced.Testing.Component.WireMock&version=1.6.0
Enhanced.Testing.Component.WireMock
A WireMock.Net harness for Enhanced.Testing.Component: fakes external HTTP APIs with an in-process server instead of calling real services. No Docker needed.
dotnet add package Enhanced.Testing.Component.WireMock
Usage
var quotesApi = new WireMockHarness { ConfigurationKey = "QuotesApi:BaseUrl" };
var component = ComponentBuilder.Create<Program>()
.AddHarness(quotesApi)
.AddHarness(http)
.Build();
The server starts with the component on a free port, and its URL is passed to the application as the
QuotesApi:BaseUrl setting. Point the application's HttpClient at that setting.
In a test, stub the external API, call the application, and verify the calls:
quotesApi.Server
.Given(Request.Create().WithPath("/quote").UsingGet())
.RespondWith(Response.Create().WithStatusCode(200).WithBody("Stay hungry"));
var response = await http.CreateClient().GetStringAsync("/quote");
Assert.Equal("Stay hungry", response);
Assert.Single(quotesApi.Server.LogEntries);
Serveris theWireMockServer, for stubbing and verification.Reset()removes stubs, scenarios and the request log; call it between tests that share the harness.BaseAddressandCreateClient()give the test the server's address or anHttpClientfor it.ConfigureServercustomizes theWireMockServerSettingsbefore the server starts.
Service discovery
An application that finds its dependencies by name with Microsoft.Extensions.ServiceDiscovery needs no extra
configuration key. Give the harness the service name, and it registers the server as that service:
var payments = new WireMockHarness { ServiceName = "payments" };
var greeter = new WireMockHarness { ServiceName = "greeter", EndpointName = "grpc", UseHttp2 = true };
| The application calls | Harness |
|---|---|
http://payments or https+http://payments |
ServiceName = "payments" |
http://_api.payments (a named endpoint) |
ServiceName = "payments", EndpointName = "api" |
The address is passed to the application as Services:{ServiceName}:{EndpointName}:0; EndpointName is http
unless set. The server speaks plain HTTP, so a client fixed to https://payments cannot be pointed at it. Endpoints
of the same service listed in the application's own configuration are not removed.
Faking a gRPC service
The same harness fakes a gRPC service the application calls. Set UseHttp2, and describe calls and responses with
the generated message types:
var greeter = new WireMockHarness { ConfigurationKey = "Greeter:Address", UseHttp2 = true };
greeter.Server
.Given(Request.Create()
.UsingGrpc(Greeter.Descriptor, nameof(Greeter.GreeterClient.SayHelloAsync))
.WithGrpcMessage<HelloRequest>(request => request.Name == "world"))
.RespondWith(Response.Create().WithGrpcMessage(new HelloReply { Message = "Hello, world" }));
UsingGrpc(service, method)takes the service descriptor from the generated code and a method name. Name the method withnameofon the generated client, so a wrong name does not compile. Without generated code, pass the full method name as a string:UsingGrpc("greet.Greeter/SayHello").WithGrpcMessage<TRequest>(predicate)matches the request message; leave it out to match every call of the method.WithGrpcMessage(message)responds with a message, andWithGrpcStatus(StatusCode.NotFound, "...")fails the call with a status.WithGrpcStatus(code, message, details)adds error details to the status andWithGrpcTrailer(key, value)adds a trailer; see Error details and trailers.- A call that no stub matches fails on the client with the
Unimplementedstatus. The status detail tells whether the method has stubs and shows the request message, for examplethe request did not satisfy the 1 stub(s) of this method. Request: { "name": "someone else" }. - A stub whose message type is not the one the method takes throws when it is created, instead of never matching.
- The extensions cover unary calls, not streaming.
- A server with
UseHttp2does not answer HTTP/1.1 requests. Use one harness for gRPC and another for HTTP APIs.
Error details and trailers
A service that follows the gRPC rich error model sends a
google.rpc.Status with details next to the status code. Pass the details to WithGrpcStatus:
greeter.Server
.Given(Request.Create().UsingGrpc(Greeter.Descriptor, nameof(Greeter.GreeterClient.SayHelloAsync)))
.RespondWith(Response.Create()
.WithGrpcStatus(StatusCode.FailedPrecondition, "The person is blocked",
new ErrorInfo { Reason = "PERSON_BLOCKED", Domain = "greeter" }));
The application reads them as it does from the real service, for example with GetRpcStatus() of the
Grpc.StatusProto package:
var error = exception.GetRpcStatus()?.GetDetail<ErrorInfo>();
- A detail is any protobuf message: a standard one such as
ErrorInfo(packageGoogle.Api.CommonProtos) or a message of the service's own. Each one is packed intoAny; anAnyis sent as it is. - To send a
Google.Rpc.Statusyou already have, pass its parts:WithGrpcStatus((StatusCode)status.Code, status.Message, [.. status.Details]). WithGrpcTrailer(key, value)adds any other trailer, after a message or with a failure status. A text value is printable ASCII; a trailer whose name ends with-bintakes bytes and is sent base64-encoded, as gRPC requires. The client reads trailers fromRpcException.Trailers, or fromGetTrailers()of a call that succeeded. Names that start withgrpc-are reserved: set the status, its message and its details withWithGrpcStatus.WithGrpcStatuscalled again replaces the status set before, with its message and details.
Response.Create()
.WithGrpcStatus(StatusCode.PermissionDenied, "Blocked")
.WithGrpcTrailer("error-code", "PERSON_BLOCKED")
.WithGrpcTrailer("signature-bin", signature);
The package depends on WireMock.Net.Minimal. Add the full WireMock.Net package if you need its extra matchers
(GraphQL, ProtoBuf).
| Product | Versions 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. net9.0 is compatible. 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 is compatible. 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. net11.0 is compatible. |
-
net10.0
- Enhanced.Testing.Component (>= 1.6.0)
- Google.Protobuf (>= 3.36.2)
- Grpc.Core.Api (>= 2.85.0)
- WireMock.Net.Minimal (>= 2.31.0)
-
net11.0
- Enhanced.Testing.Component (>= 1.6.0)
- Google.Protobuf (>= 3.36.2)
- Grpc.Core.Api (>= 2.85.0)
- WireMock.Net.Minimal (>= 2.31.0)
-
net8.0
- Enhanced.Testing.Component (>= 1.6.0)
- Google.Protobuf (>= 3.36.2)
- Grpc.Core.Api (>= 2.85.0)
- WireMock.Net.Minimal (>= 2.31.0)
-
net9.0
- Enhanced.Testing.Component (>= 1.6.0)
- Google.Protobuf (>= 3.36.2)
- Grpc.Core.Api (>= 2.85.0)
- WireMock.Net.Minimal (>= 2.31.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.