WireMock.Net icon indicating copy to clipboard operation
WireMock.Net copied to clipboard

Is it possible to use WireMock as a middleware?

Open adrianiftode opened this issue 6 months ago • 7 comments

Currently I see that I can use wiremock hosted in different forms, however what I would like to do is to have a path prefix and everything after that path to be handled by wiremock.

So something like

app.Use(async (context, next) =>
{
    if (context.Request.Path.StartsWith("wiremock"))
    {
        await UseWiremock()
        return;
    }

    await next(context);
});

I would like to deploy an arbitrary .NET Service, but still use wiremock at the port 80 and let it handle some prefixed requests.

adrianiftode avatar Dec 08 '23 22:12 adrianiftode

@adrianiftode Sounds like an interesting idea.

I guess you are using a modern .NET version like 6 or higher?

There is already an internal class named WireMockMiddleware which I register in the AspNetCoreSelfHost.cs:

appBuilder.UseMiddleware<WireMockMiddleware>();

Maybe that can be used?

StefH avatar Dec 09 '23 08:12 StefH

Yes, but the reason for asking this is I would like to deploy it with the tested service.

I have a service ServiceB that I would like to mock. Then I have the client service, ServiceA, that will do requests to ServiceB. ServiceB is an external (and expensive and legacy) service, and the only way to use it in test scenarios is to mock it.

I want to configure WireMock inside ServiceA. So when ServiceA is started, the Mocked ServiceB is also started and ready to accept requests. The Mock should listen to any HTTP request to a path that starts with /service-b-mock.

I will give it a try to the WireMockMiddleware.

To expand this, I would also like to deploy a WireMock image configured with different mocked services

  • appBuilder.UseMiddleware<WireMockMiddleware>("service-b")
  • appBuilder.UseMiddleware<WireMockMiddleware>("service-c");

This image is then deployed and accessible from the Sandbox/Tests servers, and it has some kind of "mock isolation". So each path prefix has its own WireMock configuration.

adrianiftode avatar Dec 11 '23 08:12 adrianiftode

A quick question:

Why not deploy 1 (or multiple) docker container(s) of WireMock.Net ?

StefH avatar Dec 12 '23 16:12 StefH

Even deploying it within a separated container, I was hoping to be a single one. I think I will go with this route anyway (multiple containers, one per every mocked service)

adrianiftode avatar Dec 12 '23 20:12 adrianiftode

Or use one container and register the mappings with a prefix in the path?

StefH avatar Dec 12 '23 22:12 StefH

Why not just use wiremock as a docker container using docker compose, see https://github.com/matteus6007/MyDomain.Api.Template/blob/main/docker-compose.dev-env.yml#L58 as an example of setting this up, then add your mocks in the normal JSON format into the __files folder and change any paths in your config to http://localhost:8081/api-name/ where api-name is a unique name for each API you want to mock. Doing it like this means you don't have to add anything specific to your code.

matteus6007 avatar Jan 02 '24 15:01 matteus6007

I actually built this as a combination of a .NET HostedService (for Wiremock Server) and a DelegatingHandler which checks the original request headers for something like "X-WireMockStatus" and then rerouted all HttpClient calls to WireMockServer. This worked well to allow me to run this WireMockServer in all our lower environments for testing purposes.

matthewyost avatar Apr 01 '24 14:04 matthewyost