Search code examples
wiremockwiremock-standalone

How to reference key from same JSON during response templating in Wiremock Standalone


I'm using WireMock Standalone to mock API responses, and I need to dynamically calculate values within my response templates. Specifically, I want to perform basic arithmetic operation (multiplication) on values extracted from the request.

  {
    "price": {{jsonPath node $.price}},
    "quantity": {{jsonPath node $.quantity}},
    "amt": {{multiply (parseInt (this.price)) (parseFloat (this.quantity))}}
  }

To achieve the desired outcome, I need to multiply the 'price' and 'quantity' values (are of type string) and store the result in a new key named 'amt' within the JSON response. However, I'm encountering a couple of challenges:

  1. parseInt & parseFloat are not a valid functions in this context. I need to help on how to correctly convert this to int / floats beforehand to multiply them.
  2. I'm uncertain about how to correctly reference the keys price and quantity within the same JSON response template."

Open to other suggestions as well. TIA.


Solution

  • One way to achieve this is to assign the price and quantity to some variables and then use the math helpers to multiply them together.

    The assign helper works like this:

    {{#assign 'price'}}{{jsonPath request.body '$.price'}}{{/assign}}
    

    This means that you can use {{price}} in your templates. Do the same for quantity:

    {{#assign 'quantity'}}{{jsonPath request.body '$.quantity'}}
    

    The math helper then references these variables like this:

    {{math price '*' quantity}}
    

    So, your response template will look something like this:

    {{#assign 'price'}}{{jsonPath request.body '$.price'}}{{/assign}}
    {{#assign 'quantity'}}{{jsonPath request.body '$.quantity'}}{{/assign}}
    {
    "price": {{price}},
    "quantity": {{quantity}},
    "amt": {{math price '*' quantity}} 
    }
    

    A full WireMock json file would be:

    {
          "request": {
            "method": "POST",
            "url": "/multiply"
          },
    
          "response": {
            "status": 200,
            "body": "{{#assign 'price'}}{{jsonPath request.body '$.price'}}{{/assign}}{{#assign 'quantity'}}{{jsonPath request.body '$.quantity'}}{{/assign}}{\"price\": {{price}},\"quantity\": {{quantity}},\"amt\": {{math price '*' quantity}} }",
            "transformers": ["response-template"],
            "headers": {
              "Content-Type": "application/json"
            }
          }
        }
    

    If you then send a request with the following payload:

    {
      "price": "2.5",
      "quantity": "10"
    }
    

    You will get a response like this:

    {
      "price": 2.5,
      "quantity": 10,
      "amt": 25.0
    }
    

    A curl request for the above stub might looks like this:

    curl -X POST --location "http://localhost:8080/multiply" \
        -H "Content-Type: application/json" \
        -d '{
              "price": "2.5",
              "quantity": "10"
            }'