mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 21:10:32 +01:00
Is it possible to use WireMock as a middleware? #560
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @adrianiftode on GitHub (Dec 8, 2023).
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
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 commented on GitHub (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
WireMockMiddlewarewhich I register in the AspNetCoreSelfHost.cs:Maybe that can be used?
@adrianiftode commented on GitHub (Dec 11, 2023):
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
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 commented on GitHub (Dec 12, 2023):
A quick question:
Why not deploy 1 (or multiple) docker container(s) of WireMock.Net ?
@adrianiftode commented on GitHub (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 commented on GitHub (Dec 12, 2023):
Or use one container and register the mappings with a prefix in the path?
@matteus6007 commented on GitHub (Jan 2, 2024):
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
__filesfolder and change any paths in your config tohttp://localhost:8081/api-name/whereapi-nameis 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 commented on GitHub (Apr 1, 2024):
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.
@StefH commented on GitHub (Jul 6, 2024):
@matteus6007
Is it possible that you share this complete solution?
@matthewyost commented on GitHub (Jul 8, 2024):
@StefH Let me see what I can do about sharing this with you all.
@Act0r commented on GitHub (Sep 9, 2024):
I'm very interested in that question as well. I'm working in enviroment where every service must have a bunch of specific middlewares, i can't avoid it. So i have to add that middlewares to wiremock or wiremock middleware to my service. So if this is possible please provide some hint in either direction
@matthewyost commented on GitHub (Sep 10, 2024):
So here's a snapshot of how the implementation is performed:
WiremockServerInstance - This is the class that will be used by the background service.
WiremockContext - Context to allow me to control certain functionality of the Wiremock instance
WireMockDelegationHandler - DelegatingHandler class allowing us to tap into the HttpClient object and perform our magic redirects to Wiremock without having to change our code. THIS is where the magic happens
WireMockBgService - This is the background service that will hold onto our instance of Wiremock and allow us to keep from spinning up new copies with every request.
ServiceCollectionExtensions - Extension methods to make it easy for integrating into any app.
Now for it's usage! Using a minimal API, we can have something like this:
Hopefully this helps some of y'all develop the pattern to make this a thing!
@Act0r commented on GitHub (Sep 11, 2024):
Thank you very much for the detailed example
@StefH commented on GitHub (Sep 19, 2024):
@matthewyost
I did try your solution, however it seems that the path defined in WithPath is not available, it returns a 404.
@matthewyost commented on GitHub (Sep 19, 2024):
@StefH The stuff between the < > is just supposed to be whatever path you want to use. It's meant to be replaced with whatever path you're attempting to mock a response for.
@StefH commented on GitHub (Sep 19, 2024):
About the "path" : I did change that. If you have time, please review my PR: https://github.com/WireMock-Net/WireMock.Net/pull/1175
@StefH commented on GitHub (Sep 23, 2024):
@Act0r did you get it working?
@Act0r commented on GitHub (Sep 25, 2024):
@StefH Actually i posponed the idea and decide to implement requested features by myself.
@StefH commented on GitHub (Sep 25, 2024):
@Act0r
@matteus6007
@matthewyost
I got it working. I was thinking in the wrong direction...
This solution actually translates any calls made from a WebApplication to another API (e.g.
https://real-api:12345/test1) to a call to the running WireMock.Net instance (as background service) in the same WebApplication. This is done by changing the behavior from the HttpClient used in that WebApplication.Example:
I'm not sure that is the same idea as @adrianiftode had?
My thoughts were initially that this WireMock.Net instance would be handling additional calls in a WebApplication.
/weatherforecastwas handled by the WebApplication itself./testwas handled by the WireMock.Net instance.@StefH commented on GitHub (Sep 27, 2024):
PR is merged.