Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 226
alltej Avatar asked Oct 17 '18 18:10

alltej


2 Answers

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.

like image 195
Jeff Avatar answered Oct 01 '22 07:10

Jeff


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
like image 43
A. Kootstra Avatar answered Oct 01 '22 08:10

A. Kootstra