WireMock4Microservices.Net 0.1.1-preview

This is a prerelease version of WireMock4Microservices.Net.
There is a newer version of this package available.
See the version list below for details.
dotnet add package WireMock4Microservices.Net --version 0.1.1-preview                
NuGet\Install-Package WireMock4Microservices.Net -Version 0.1.1-preview                
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="WireMock4Microservices.Net" Version="0.1.1-preview" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add WireMock4Microservices.Net --version 0.1.1-preview                
#r "nuget: WireMock4Microservices.Net, 0.1.1-preview"                
#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 WireMock4Microservices.Net as a Cake Addin
#addin nuget:?package=WireMock4Microservices.Net&version=0.1.1-preview&prerelease

// Install WireMock4Microservices.Net as a Cake Tool
#tool nuget:?package=WireMock4Microservices.Net&version=0.1.1-preview&prerelease                

WireMock4Microservices.Net

WireMock4Microservices.Net is a .Net library built on top of WireMock.Net. It enhances support for mocked requests by injecting one or more services and extends WireMock's features using the original Callback Response.

Why 4Microservices?

It's common behavior for microservices to share contracts, SDKs, and messages among themselves. While WireMock supports webhooks, it cannot send or consume messages from queues or topics. By implementing the IWiremockEndpoint (see the "Using" section), it becomes possible to inject any service and create the expected behavior for simple or more complex scenarios.

Using

To begin creating mocked endpoints, implement the IWiremockEndpoint or inherit from WiremockEndpoint to start creating mocked endpoints as follows:

class MyFakeEndpoint : IWiremockEndpoint
{
    public IRequestMatcher RequestMatcher { get; }
    public IWebhook[] Webhooks { get; }

    public Task<ResponseMessage> CallbackHandler(IRequestMessage requestMessage)
    {
        throw new NotImplementedException();
    }
}

RequestMatcher Property

Start by defining the request matching criteria within the RequestMatcher property. For more details, refer to the WireMock.Net documentation.

Webhooks Property

The Webhooks property is optional and allows you to configure webhooks. Further information can be found in the WireMock.Net documentation.

CallbackHandler Method

The CallbackHandler method is responsible for generating the response based on the RequestMessage. You can use the original IRequestMessage and utilize services to create more complex scenarios. Here are examples of different response types:

Single text
public class GetHealth : WiremockEndpoint
{
    public override IRequestMatcher RequestMatcher
        => Request.Create().UsingGet().WithPath("/health");

    public override Task<ResponseMessage> CallbackHandler(IRequestMessage requestMessage)
    {
        return Task.FromResult(
            ResponseMessageBuilder
                .Create()
                .WithStatusCode(HttpStatusCode.OK)
                .BuildWithText("Healthy")
        );
    }
}
Json String
public class PostOrdersCheckoutWillReturnAccepted : WiremockEndpoint
{
    public override IRequestMatcher RequestMatcher
        => Request.Create().UsingGet().WithPath("/orders/checkout");

    public override Task<ResponseMessage> CallbackHandler(IRequestMessage requestMessage)
    {
        return Task.FromResult(
            ResponseMessageBuilder
                .Create()
                .WithStatusCode(HttpStatusCode.Accepted)
                .BuildWithStringAsJson("""
                                       {
                                         "orderId: "xpto",
                                         "status": "Processing"
                                       }
                                       """));
    }
}
Json Data
public class PostOrdersCheckoutWillReturnAccepted : WiremockEndpoint
{
    public override IRequestMatcher RequestMatcher
        => Request.Create().UsingGet().WithPath("/orders/checkout");

    public override Task<ResponseMessage> CallbackHandler(IRequestMessage requestMessage)
    {
        return Task.FromResult(
            ResponseMessageBuilder
                .Create()
                .WithStatusCode(HttpStatusCode.Accepted)
                .BuildWithDataAsJson(new() { orderId = "xpto", status = "Processing" }));
    }
}

Using with ASP.NET Web

To use WireMock4Microservices.Net with ASP.NET Web, you need to perform two steps in your Program or Startup file:

var builder = WebApplication.CreateBuilder(args);

IConfiguration configuration = builder.Configuration;

var services = builder.Services;

services.AddWiremockEndpoints(configuration, Assembly.GetExecutingAssembly());

In the AddWiremockEndpoints method, the Assembly parameter specifies where your implementations of IWiremockEndpoint are located. Through reflection, all implementations will be created using the WireMockServer singleton instance. The IConfiguration is used to get the WireMockServerSettings, see the WireMock.Net documentation for more information.

Finally, use the IApplicationBuilder to call the following method:

app.UseWiremockEndpoints();

This setup enables WireMock4Microservices.Net within your ASP.NET Web application.

Product Compatible and additional computed target framework versions.
.NET 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. 
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
0.1.2 138 2/6/2024
0.1.1 147 10/5/2023
0.1.1-preview 117 10/3/2023
0.1.0-preview 99 10/3/2023
0.0.2-preview1 126 10/2/2023
0.0.1 154 10/1/2023
0.0.1-preview1 107 10/2/2023