Mappings could have a remaining hit counter that upon reaching 0 would stop being considered #318

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

Originally created by @edabreu on GitHub (Dec 2, 2020).

Is your feature request related to a problem? Please describe.
I've recently replaced the use of MockServer with WireMock.net. The main issue that I've found so far is not being able to setup stubs/expectations to have a counter to control its availability. Let's imagine the scenario where I want to simulate the recovery from a failure when calling an external service. Imagining that I have a Polly retry policy for http transient errors I would like to test a scenario where, for example, the first N attempts would return status 5XX, and after that I could have a successful one returning 200.

Describe the solution you'd like
In order for this to test I would like to be able to setup a stub that would respond with status 5XX for the first N calls with priority 1 and another stub that would respond with a payload and status 200 with priority 2. Both stubs would match the same request, but the first one would only match while the defined counter was greater than 0.

Describe alternatives you've considered
Taking into consideration the previously presented communication's recovery scenario I don't think that any of the existing alternatives (timeouts, random timeouts, mixed priorities) would allow this to be tested because once a stub is created it will be there until a reset is made.

Is your feature request supported by WireMock (java version)? Please provide details.
It is my understanding that the java version also doesn't support this.

Originally created by @edabreu on GitHub (Dec 2, 2020). **Is your feature request related to a problem? Please describe.** I've recently replaced the use of MockServer with WireMock.net. The main issue that I've found so far is not being able to setup stubs/expectations to have a counter to control its availability. Let's imagine the scenario where I want to simulate the recovery from a failure when calling an external service. Imagining that I have a Polly retry policy for http transient errors I would like to test a scenario where, for example, the first N attempts would return status 5XX, and after that I could have a successful one returning 200. **Describe the solution you'd like** In order for this to test I would like to be able to setup a stub that would respond with status 5XX for the first N calls with priority 1 and another stub that would respond with a payload and status 200 with priority 2. Both stubs would match the same request, but the first one would only match while the defined counter was greater than 0. **Describe alternatives you've considered** Taking into consideration the previously presented communication's recovery scenario I don't think that any of the existing alternatives (timeouts, random timeouts, mixed priorities) would allow this to be tested because once a stub is created it will be there until a reset is made. **Is your feature request supported by [WireMock (java version)](https://www.wiremock.org)? Please provide details.** It is my understanding that the java version also doesn't support this.
adam added the question label 2025-12-29 08:26:00 +01:00
adam closed this issue 2025-12-29 08:26:00 +01:00
Author
Owner

@StefH commented on GitHub (Dec 2, 2020):

Hello @edabreu,

I think your question can be solved by using https://github.com/WireMock-Net/WireMock.Net/wiki/Scenarios-and-States

Particularly, in your case, "https://github.com/WireMock-Net/WireMock.Net/wiki/Scenarios-and-States#stay-in-the-same-state-for-a-number-of-requests" would be solution.

@StefH commented on GitHub (Dec 2, 2020): Hello @edabreu, I think your question can be solved by using https://github.com/WireMock-Net/WireMock.Net/wiki/Scenarios-and-States Particularly, in your case, "https://github.com/WireMock-Net/WireMock.Net/wiki/Scenarios-and-States#stay-in-the-same-state-for-a-number-of-requests" would be solution.
Author
Owner

@edabreu commented on GitHub (Dec 4, 2020):

Hello @StefH

Thank you for clearing this issue. I've successfully created a test scenario as mentioned before using the Scenario states, but I've been trying to setup another scenario as follows:

this.wiremockServer.Given(Request.Create()
    .WithPath($"/search")
    .UsingPost())
    .InScenario("SearchFailureRecovery")
    .WillSetStateTo("Timeout failure", 1)
    .RespondWith(Response.Create()
    .WithHeader("Content-Type", "application/json")
    .WithBodyAsJson(searchResult)
    .WithStatusCode(200)
    .WithDelay(600));

this.wiremockServer.Given(Request.Create()
    .WithPath($"/search")
    .UsingPost())
    .InScenario("SearchFailureRecovery")
    .WhenStateIs("Timeout failure")
    .WillSetStateTo("Dependency Internal Server Error", 1)
    .RespondWith(Response.Create()
    .WithStatusCode(500));

this.wiremockServer.Given(Request.Create()
    .WithPath($"/search")
    .UsingPost())
    .InScenario("SearchFailureRecovery")
    .WhenStateIs("Dependency Internal Server Error")
    .RespondWith(Response.Create()
    .WithHeader("Content-Type", "application/json")
    .WithBodyAsJson(searchResult)
    .WithStatusCode(200));

On my service I have a timeout of 500ms. I can't manage to have this scenario working because the wiremock is stuck in the first state, always responding with a delay of 600ms for all retry attempts. I think that wiremock might not be updating its scenario state because my service is closing the connection due to timeout. Or am I doing something wrong?

@edabreu commented on GitHub (Dec 4, 2020): Hello @StefH Thank you for clearing this issue. I've successfully created a test scenario as mentioned before using the Scenario states, but I've been trying to setup another scenario as follows: ``` c# this.wiremockServer.Given(Request.Create() .WithPath($"/search") .UsingPost()) .InScenario("SearchFailureRecovery") .WillSetStateTo("Timeout failure", 1) .RespondWith(Response.Create() .WithHeader("Content-Type", "application/json") .WithBodyAsJson(searchResult) .WithStatusCode(200) .WithDelay(600)); this.wiremockServer.Given(Request.Create() .WithPath($"/search") .UsingPost()) .InScenario("SearchFailureRecovery") .WhenStateIs("Timeout failure") .WillSetStateTo("Dependency Internal Server Error", 1) .RespondWith(Response.Create() .WithStatusCode(500)); this.wiremockServer.Given(Request.Create() .WithPath($"/search") .UsingPost()) .InScenario("SearchFailureRecovery") .WhenStateIs("Dependency Internal Server Error") .RespondWith(Response.Create() .WithHeader("Content-Type", "application/json") .WithBodyAsJson(searchResult) .WithStatusCode(200)); ``` On my service I have a timeout of 500ms. I can't manage to have this scenario working because the wiremock is stuck in the first state, always responding with a delay of 600ms for all retry attempts. I think that wiremock might not be updating its scenario state because my service is closing the connection due to timeout. Or am I doing something wrong?
Author
Owner

@StefH commented on GitHub (Dec 4, 2020):

Quick answer: .WithHeader("Content-Type", "application/json") should also be added to the second part ?

And what you can do is observe the logging, maybe you see some details there why is it's not matching?

@StefH commented on GitHub (Dec 4, 2020): Quick answer: `.WithHeader("Content-Type", "application/json")` should also be added to the second part ? And what you can do is observe the logging, maybe you see some details there why is it's not matching?
Author
Owner

@edabreu commented on GitHub (Dec 11, 2020):

Thank you for all the help.

@edabreu commented on GitHub (Dec 11, 2020): Thank you for all the help.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#318