1️⃣ Intro

As author from WireMock.Net, I started this blog series where I’ll explain new features or advanced functionality / usage scenarios for WireMock.Net.


2️⃣ Overview

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. It’s based on the functionality from WireMock.org but extended with more functionality.

In this blog I’ll explain some new advanced features like generating C# code.


3️⃣ Define mappings (existing functionality)

Defining mappings for WireMock.Net can be done in several ways:

3.1 Via C# code

Defining a mapping can be done using the fluent syntax. This is usually done when using WireMock.Net in a unit/integration test scenario, but can also be done when running WireMock.Net as console-app with some pre-defined mappings.

Example C# code to start the server and define a mapping would look like:

var server = WireMockServer.Start();
server
    .Given(Request.Create()
        .UsingMethod("GET")
        .WithPath("/foo1")
    )
    .RespondWith(Response.Create()
        .WithBody("Hello World")
    );

See this link for an detailed example on how to use WireMock.Net in a unit/integration test.

And see this link for a more extended example.

3.2 Via the Admin REST-Api

When WireMock.Net is running as standalone process (console-app, Docker or dotnet-tool) there are no mappings defined initially. So when you want to dynamically add new mappings, you can use the Admin REST-Api to post a new mapping in JSON to the server.

An example POST-request to the URL where WireMock.Net is running (http://localhost:9091/__admin/mappings) would be like:

{
    "UpdatedAt": "2023-01-27T21:32:14.1994041Z",
    "Request": {
        "Path": {
            "Matchers": [
                {
                    "Name": "WildcardMatcher",
                    "Pattern": "/foo1"
                }
            ]
        },
        "Methods": [
            "GET"
        ],
    },
    "Response": {
        "Body": "Hello World"
    }
}
  • Note that WireMock.Net must be started with the Admin-Interface enabled, otherwise this is not possible.
    And accessing the Admin-Interface requires authentication, see this link for more information.

  • Note that this JSON mapping is exactly the same as the C# example above.

3.3 Via a static mapping file

It’s also possible to define the JSON mapping(s) in a file and place this file in the folder __admin/mappings, this will make sure that when the WireMock.Net server is started, these static mappings are imported.


4️⃣ Get an existing mapping

When WireMock.Net is running as a service (WebApp or Docker, console-app, dotnet-tool) and one or more mappings are defined, all mappings (or one specific mapping) can be retrieved via the Admin REST-API.

4.1 Get as JSON (existing functionality)

When calling the Admin REST-API using a GET on the URL http://localhost:9091/__admin/mappings/90356dba-b36c-469a-a17e-669cd84f1f05, the following mapping (as JSON) is returned.

{
    "Guid": "90356dba-b36c-469a-a17e-669cd84f1f05",
    "UpdatedAt": "2023-01-27T21:32:14.1994041Z",
    "Request": {
        "Path": {
            "Matchers": [
                {
                    "Name": "WildcardMatcher",
                    "Pattern": "/foo1"
                }
            ]
        },
        "Methods": [
            "GET"
        ],
        "Params": [
            {
                "Name": "p1",
                "Matchers": [
                    {
                        "Name": "ExactMatcher",
                        "Pattern": "xyz",
                        "IgnoreCase": false
                    }
                ]
            }
        ]
    },
    "Response": {
        "Body": "Hello World"
    }
}

4.2 Get as C# code (new functionality)

Since version 1.5.15 (see ChangeLog) it’s possible to retrieve the defined mappings as ready to use, copy-paste C# code.

When calling the Admin REST-API using a GET on the URL http://localhost:9091/__admin/mappings/code/90356dba-b36c-469a-a17e-669cd84f1f05, the mapping is returned as C# code:

var server = WireMockServer.Start();
server
    .Given(Request.Create()
        .UsingMethod("GET")
        .WithPath("/foo1")
        .WithParam("p1", "xyz")
    )
    .WithGuid("90356dba-b36c-469a-a17e-669cd84f1f05")
    .RespondWith(Response.Create()
        .WithBody("Hello World")
    );

This is very useful when you want to define this mapping as C# code in a unit/integration test. With this new feature it’s easy to just copy-paste the generated C# code.

4.2.1 Use builder code

By default the C# code which is generated will use the WireMockServer to generate the fluent builder code.

In case you want to use the MappingBuilder (see issue-868), an additional query parameter MappingConverterType with value Builder should be added to the URL, like this: http://localhost:9091/__admin/mappings/code/90356dba-b36c-469a-a17e-669cd84f1f05?MappingConverterType=Builder

This returns the following C# code:

var builder = new MappingBuilder();
builder
    .Given(Request.Create()
        .UsingMethod("GET")
        .WithPath("/foo1")
        .WithParam("p1", "xyz")
    )
    .WithGuid("90356dba-b36c-469a-a17e-669cd84f1f05")
    .RespondWith(Response.Create()
        .WithBody("Hello World")
    );

Note that instead of the var server = WireMockServer.Start(), C# code to a create a new builder is returned : var builder = new MappingBuilder().

4.2.2 Get all mappings as C# code

If you want to get the C# code from all the mappings currently defined in WireMock.Net, do a GET request to URL http://localhost:9091/__admin/mappings/code.

This will return this C# code (example):

var server = WireMockServer.Start();
server
    .Given(Request.Create()
        .UsingMethod("GET")
        .WithPath("/foo1")
        .WithParam("p1", "xyz")
    )
    .WithGuid("90356dba-b36c-469a-a17e-669cd84f1f05")
    .RespondWith(Response.Create()
        .WithBody("Hello World")
    );

server
    .Given(Request.Create()
        .UsingMethod("GET")
        .WithPath("/foo2")
    )
    .WithGuid("90356dba-3333-2222-1111-669cd84f1f05")
    .RespondWith(Response.Create()
        .WithBody("Hello World 2")
    );

4.2.3 Get mappings as C# code using the fluent syntax

Note that it’s also possible to get the C# code for a mapping when using the fluent syntax.

This code would be like:

var server = WireMockServer.Start();

// Get the C# code for a specific mapping
string one = server.MappingToCSharpCode(new Guid("90356dba-b36c-469a-a17e-669cd84f1f05"));

// Get the C# code for all defined mappings
string all = server.MappingsToCSharpCode();

❔ Feedback or questions

  • Questions and issues can also be added on my GitHub-project.
  • Do you have any feedback or questions regarding this or one of my other blogs? Feel free to contact me!

📚 Additional resources


comments powered by Disqus