alltej
alltej

Reputation: 7285

Wiremock Standalone with Dynamic response

I have a standalone instance of Wiremock server. The mappings are stored as json files under the mappings folder. I have a POST request that needs to return a dynamic ID(integer) in the response. Is there a way to configure this in the json file?

Upvotes: 10

Views: 25954

Answers (5)

hd84335
hd84335

Reputation: 9893

Although this might be late, it could still be helpful. WireMock now offers a random value helper that allows you to generate random strings of various types.

For example, to generate a random number, you can include the following in your JSON mapping file:

"response": {
   ...,
   "jsonBody": {
      "id": "{{randomValue length=4 type='NUMERIC'}}"
      "transformers": ["response-template"]
   }
}

This will generate a random number with 4 digits.

Remember to enable the global-response-templating option when starting the WireMock server.

I hope this helps!

Upvotes: 0

Deekshith Anand
Deekshith Anand

Reputation: 2662

As @Jeff mentions, If you are running it as a stand-alone process, you need to add this flag --global-response-templating. This will apply templating to each and every reponse. However, few of your responses may be jsut plain json with no templating required.

In that case use --local-response-templating. and add this field inside reponse json:

response:{
"transformers": ["response-template"]
}

Upvotes: 0

Jeff
Jeff

Reputation: 1591

To make the above examples work, I had to run the standalone jar with the --global-response-templating. Then I saw, for example, {{now}} working which is what I wanted. Not sure if the documentation specifies this -- I tried the always-useful --help.

Upvotes: 8

A. Kootstra
A. Kootstra

Reputation: 6961

In WireMock there are a number of response template helper functions for generating random strings. In the below example I'm using the one for generating a UUID, but several other options exist.

Mapping file: dynamic_id.json

{
  "request": {
    "method": "POST",
    "url": "/dynamic_id"
  },
  "response": {
    "headers": {
      "Content-Type": "application/json"
    },
    "status": 200,
    "body": "{{randomValue type='UUID'}}",
    "transformers": ["response-template"]

  }
}

Using an empty POST http://wiremock/dynamic_id will return an id similar to: c2e6bf32-c9a3-45c0-b988-94fad04cc7a2.

Start WireMock:

java -jar wiremock-standalone-2.18.0.jar --port 8181 --verbose --local-response-templating

Upvotes: 8

Mark Han
Mark Han

Reputation: 3135

This seems like a perfect use-case for OpenTable's Wiremock Body Transformer.

It can be easily integrated with the Standalone Server like this:

java -cp "wiremock-body-transformer-1.1.6.jar:wiremock-2.3.1-standalone.jar" com.github.tomakehurst.wiremock.standalone.WireMockServerRunner --verbose --extensions com.opentable.extension.BodyTransformer

And allows you to easily specify a dynamic variable that you would want to match in the response.

Here is an example to get a random integer without having to specify anything in the request, however if you need to match a specific variable in the request to the response, then that is also very doable with this extension and numerous examples can be found in the readme.

{
    "request": {
        "method": "POST",
        "urlPath": "/transform",
    },
    "response": {
        "status": 200,
        "body": "{\"randomInteger\": \"$(!RandomInteger)\"}",
        "headers": {
            "Content-Type": "application/json"
        },
        "transformers": ["body-transformer"]
    }
}

Upvotes: 3

Related Questions