Random status code returned for existing mapping #549

Closed
opened 2025-12-29 08:29:55 +01:00 by adam · 3 comments
Owner

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.

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)](https://www.wiremock.org)? 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.
adam added the question label 2025-12-29 08:29:55 +01:00
adam closed this issue 2025-12-29 08:29:56 +01:00
Author
Owner

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

var server = WireMockServer.Start();

server
    .Given(Request.Create().WithPath("/api/data").UsingGet())
    .WithProbability(0.3)
    .RespondWith(Response.Create().WithStatusCode(200).WithBody(@"{""message"": ""Success""}"));

server
    .Given(Request.Create().WithPath("/api/data").UsingGet())
    .RespondWith(Response.Create().WithStatusCode(500).WithBody("Internal Server Error"));

In this example, we set up two mappings with different priorities. Both mappings match on a GET request to the /api/data path.
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).

@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: ``` csharp {linenos=false} var server = WireMockServer.Start(); server .Given(Request.Create().WithPath("/api/data").UsingGet()) .WithProbability(0.3) .RespondWith(Response.Create().WithStatusCode(200).WithBody(@"{""message"": ""Success""}")); server .Given(Request.Create().WithPath("/api/data").UsingGet()) .RespondWith(Response.Create().WithStatusCode(500).WithBody("Internal Server Error")); ``` In this example, we set up two mappings with different priorities. Both mappings match on a GET request to the `/api/data` path. 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).
Author
Owner

@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): That is really interesting, I will give that a try now. Thanks
Author
Owner

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

MServer.Given(Request.Create().WithPath("/v1/organizations/*").UsingGet()
        .WithParam("select", true, MatchBehaviour.AcceptOnMatch)
    )
    .WithProbability(0.9)
    .RespondWith(Response.Create().WithStatusCode(401).WithBody("Test v1 Unauthorised response"));
@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?): ``` js MServer.Given(Request.Create().WithPath("/v1/organizations/*").UsingGet() .WithParam("select", true, MatchBehaviour.AcceptOnMatch) ) .WithProbability(0.9) .RespondWith(Response.Create().WithStatusCode(401).WithBody("Test v1 Unauthorised response")); ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#549