mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 21:10:32 +01:00
Random status code returned for existing mapping #549
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 @fromorbonia on GitHub (Oct 24, 2023).
Originally assigned to: @StefH on GitHub.
Is your feature request related to a problem? Please describe.
No
Describe the solution you'd like
I want WireMock to return a status code like 401 or 429 either randomly or every x requests, similar to WithRandomDelay. Many OAS files are complex and it is insanely easy to read a file in and mock that API. I'm using WireMock to mock a number of third party APIs.
To use WithCallback, I have to convert those APIs into code - but that then becomes less manageable. It would be great to, once the mapping model is loaded to simple apply a generic "return a 401 every 10 request" or similar to all endpoints.
Describe alternatives you've considered
As a test, I tried using code and WithCallback, with a generic function - but WithCallback can't just change the status code, it has to set the body as well.
However, what I really wanted is just a way to alter the mapping model. So, I tried to alter a loaded mapping model (by updating it, deleting the original mapping and adding the altered one back in) - but I don't think I can use the Dynamic Response provider to pick up all the settings from the existing mapping?
Is your feature request supported by WireMock (java version)? Please provide details.
I don't think it is supported.
Additional context
So, feels as though I have some options, but none as simple as being able to add WithRandomStatus.
@StefH commented on GitHub (Oct 24, 2023):
@fromorbonia
It's not yet on the wiki, but I think you can use this solution:
Simulating Unreliable Networks
Another important aspect of chaos engineering is testing how your application performs under unreliable network conditions, such as broken connections or packet loss. WireMock.Net allows you to simulate these scenarios by randomly returning error responses.
Here's an example of how to use WireMock.Net to simulate a flaky API:
In this example, we set up two mappings with different priorities. Both mappings match on a GET request to the
/api/datapath.But the first mapping has a 30% chance of being matched due to the defined probability. If that request is not matched, the second mapping will be used instead, simulating an internal server error (status code 500).
@fromorbonia commented on GitHub (Oct 24, 2023):
That is really interesting, I will give that a try now. Thanks
@fromorbonia commented on GitHub (Oct 24, 2023):
Tested and up and running!
That is just what I wanted 👍🏻 thanks for the quick response.
Based on the path I could change the behaviour per API modelled.
I just used a wildcard on the initial tests (e.g. .WithPath("/some/*")), but the endpoints this was supposed to trigger on had parameters as well. I had to make sure that I included the parameter to get it to match (which I think is to do with scoring?):