Hi All, I am new to wiremock. I am trying basic m...
# help
k
Hi All, I am new to wiremock. I am trying basic mock example with decimal and I couldn't get it to work. And I dont see any example online either. Below is the response section of my json file
Copy code
"response": {
    "status": 200,
    "jsonBody": {
      "weekly_rate": "{{randomDecimal lower=100.00 upper=800.00}}",
      "max_amount": "{{randomDecimal lower=5000.00 upper=20000.00}}"
    },
    "transformers": ["response-template"]
  }
and I get this response
Copy code
{
  "weekly_rate": "220.87939761296946",
  "max_amount": "8860.916637737479"
}
Two issues 1. The numbers have more than 2 decimal places so the precision is not respected. 2. the numbers are strings instead of numeric
g
for the integer response can try this bro
Copy code
"response": {
    "status": 200,
    "jsonBody": {
      "weekly_rate": {{randomDecimal lower=100.00 upper=800.00}},
      "max_amount": {{randomDecimal lower=5000.00 upper=20000.00}}
    },
    "transformers": ["response-template"]
  }
even tho the json will look like error, but it works, just try
also you can combine many kind of random things like this to make the two decimal digit
Copy code
"weekly_rate": {{randomInt lower=100 upper=800}}.{{randomInt lower=1 upper=9}}{{randomInt lower=1 upper=9}}
l
You might need to switch to the
body
element instead of the
jsonBody
element to make sure the numeric values work.
As for the random decimal, I can't see a way to specify the decimal places required which would perhaps be a nice feature to add
k
@Gradito Tunggulcahyo I get
Unexpected character ('{' (code 123)): was expecting double-quote to start field name
jsonBody cannot seem to output numeric. I had to change to body and do this way
Copy code
"body": "{\n  \"weekly_rate\": {{randomDecimal lower=100.00 upper=800.00}},\n   \"max_amount\": {{pickRandom 5000.01 10000.02 15000.03 20000.04}}\n}"
even randomDecimal lower and upper cannot seem to respect the precision at all. In short this framework is just terrible.
l
To be fair,
jsonBody
can handle numeric values but not when you are using templating to generate those values. This is nothing to do with the framework, this is just basic json. Anything that makes the json invalid isn't going to work and adding
{{
to generate a numeric value is always going to be invalid json. There are many ways around this using
body
or
bodyFileName
. The random decimal precision does seem to be a limitation of the handlebars helper. I'll add an issue and see if we can do something about that for the next release.
k
how else to generate numeric values with jsonBody?
The fact that it take so long to do simple things like this I would just blame the framework. Whatever the reasons are there must be a easy way to generate numerics and currently I am not seeing that.
you can have a valid json and generate numerics(since it is all about how to interpret the template and produce the desired output) but that is not happening so that is limitation of framework too
l
Looks like this should work -
{{numberFormat (randomDecimal lower=100.00 upper=800.00) '###.00' 'en_GB'}}
k
so like this
Copy code
"jsonBody": {
      "weekly_rate": "{{numberFormat (randomDecimal lower=100.00 upper=800.00) '###.00' 'en_GB'}}",
      "max_amount": "{{numberFormat (randomDecimal lower=5000.00 upper=20000.00) '###.00' 'en_GB'}}"
    },
This still generates strings
the precision is fine
Copy code
{
  "weekly_rate": "583.14",
  "max_amount": "13682.04"
}
I am looking for
Copy code
{"weekly_rate":583.14,max_amount":13682.04}
l
The
jsonBody
element won't support this as it is expecting it to be valid json. You can either switch to the
body
element like you have above or extract the body out into a separate file using the
bodyFileName
element:
Copy code
{
  "request": {
    "method": "GET",
    "url": "/basic"
  },
  "response": {
    "status": 200,
    "bodyFileName": "basic.txt",
    "transformers": [
      "response-template"
    ]
  }
}
Then
basic.txt
needs to be stored in the
__files
directory and can look like this:
Copy code
{
  "weekly_rate": {{numberFormat (randomDecimal lower=100.00 upper=800.00) '###.00' 'en_GB'}},
  "max_amount": {{numberFormat (randomDecimal lower=5000.00 upper=20000.00) '###.00' 'en_GB'}}
}
This should then generate a response like this:
Copy code
{
  "weekly_rate": 573.97,
  "max_amount": 16922.91
}
🙌 1
k
Thank you, I will stick with body for now because lot of my other mock templates are store under mappings. I really dont get why we cannot have have a valid json spec that generates the right template. for example if the templating is designed this way
Copy code
{
  "jsonBody": {
    "weekly_rate": "{{randomDecimal lower=100.00 upper=800.00 output_type='NUMERIC'}}"
  }
}
This is a valid json and the output type is clear. so its really all about how well the wiremock templating protocol is designed. I think things have to change there
anyways I am good with this after spending so many hours. Thanks for the help.
l
No problem. I see this question on stackoverflow as well. I will add the same answers over there just in case anyone comes across that question also