Hi. I've been using WireMock for some testing and...
# wiremock-java
n
Hi. I've been using WireMock for some testing and I had a requirement for a web service to keep looping round whenever it gets a 429 error until the target web site corrects itself and returns a 200. I was wondering if there was any way to simulate this in the WireMock JSON configuration files. I know there is an __admin/requests/count endpoint but that will just tell me the number of requests that have been made at a given time. I want to put a condition in the config file so that e.g. on the first 4 calls, the mock returns 429 but on subsequent calls it returns 200.
t
Hi @Neal Hawman you can use scenarios to create a sequence of responses for a given request, so you could set it up to do 429, 429, 429, 200 or something like that.
n
Thanks. Is there a way to move automatically from one scenario state to the next? From the documentation it looked as if you had to change the state by sending a web request.
t
By setting the
newScenarioState
attribute on the stub, calling that stub will automatically advance the scenario’s state. Here are the relevant docs: https://wiremock.org/docs/stateful-behaviour/
Here’s a full example of how to do what you’re looking for:
Copy code
{
  "mappings": [
    {
      "scenarioName": "rate-limiting",
      "requiredScenarioState": "Started",
      "newScenarioState": "error-1",
      "request": {
        "method": "GET",
        "urlPath": "/something"
      },
      "response": {
        "status": 429,
        "body": "Too fast!"
      }
    },
    {
      "scenarioName": "rate-limiting",
      "requiredScenarioState": "error-1",
      "newScenarioState": "error-2",
      "request": {
        "method": "GET",
        "urlPath": "/something"
      },
      "response": {
        "status": 429,
        "body": "Too fast!"
      }
    },
    {
      "scenarioName": "rate-limiting",
      "requiredScenarioState": "error-2",
      "newScenarioState": "success",
      "request": {
        "method": "GET",
        "urlPath": "/something"
      },
      "response": {
        "status": 429,
        "body": "Too fast!"
      }
    },
    {
      "scenarioName": "rate-limiting",
      "requiredScenarioState": "success",
      "request": {
        "method": "GET",
        "urlPath": "/something"
      },
      "response": {
        "status": 200,
        "body": "OK"
      }
    }
  ]
}
n
Many thanks. That's sounds like exactly what I want.
👍 1