Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to use WireMock as a middleware? #1035

Open
adrianiftode opened this issue Dec 8, 2023 · 7 comments
Open

Is it possible to use WireMock as a middleware? #1035

adrianiftode opened this issue Dec 8, 2023 · 7 comments
Labels

Comments

@adrianiftode
Copy link

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.

@StefH
Copy link
Collaborator

StefH commented Dec 9, 2023

@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?

@adrianiftode adrianiftode changed the title Is it possible to use Wiremock as a middleware? Is it possible to use WireMock as a middleware? Dec 11, 2023
@adrianiftode
Copy link
Author

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("service-b")
  • appBuilder.UseMiddleware("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.

@StefH
Copy link
Collaborator

StefH commented Dec 12, 2023

A quick question:

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

@adrianiftode
Copy link
Author

adrianiftode commented Dec 12, 2023

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)

@StefH
Copy link
Collaborator

StefH commented Dec 12, 2023

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

@matteus6007
Copy link

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.

@matthewyost
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants