Search code examples
spring-bootwiremock

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?


Solution

  • 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"]
        }
    }