Hello. I am using WireMock (not WireMock Cloud) in...
# general
v
Hello. I am using WireMock (not WireMock Cloud) in standalone on a docker. So I configure my mappings via JSON files. Is it possible to use each, and conditions, in the JSON to make it dynamic ? (eg : I receive an array of X element, so I return another array of X elements) I struggle to find documentation of response templating for WireMock standalone on docker. Thanks !
t
Hi Victor, the templating language is the same regardless of which edition so everything in https://wiremock.org/docs/response-templating/ applies. The
each
,
if
and other conditional helpers are all available.
v
Hi Tom thanks for this quick answer. It seems that I was missing the ""transformers": ["response-template"]". However when I am trying to do something slightly more complicated than just a single if, when I do it on, several lines, I have issues on startup : "Illegal unquoted character ((CTRL-CHAR, code 13)): has to be escaped using backslash to be included in string value". Do I need to set an option to allow "coding" on several lines ?"
t
No, you shouldn't need that. If you post the code you're trying to implement I might be able to see what's wrong.
v
{
"request": {
"method": "GET",
"url": "/conditional/stuff"
},
"response": {
"status": 200,
"body": "{{#if (contains 'abcde' 'abc')}}
YES
{{/if}}",
"transformers": ["response-template"],
"headers": {
"Content-Type": "text/json"
}
}
}
As simple as that 🙂 When runing the docker I get :
probably something to do with tabulations ? or LF/CRLF ?
So after transforming tabulations into spaces, and CRLF to LF, I still have the issue at the end of the line :
"body": "{{#if (contains 'abcde' 'abc')}}
t
Yeah, it looks like you’ve got some UTF16 characters in there maybe?
Ah sorry - missed the obvious thing - yes, in JSON you can’t have multi-line strings
Copy code
{
  "request": {
    "method": "GET",
    "url": "/conditional/stuff"
  },
  "response": {
    "status": 200,
    "body": "{{#if (contains 'abcde' 'abc')}}\nYES\n{{/if}}",
    "transformers": [
      "response-template"
    ],
    "headers": {
      "Content-Type": "text/json"
    }
  }
}
v
So I have to keep all my "code" in a single line... =(
Not easy to read/maintain 😕
But this is what I was afraid of, thx for confirming
t
This is standard JSON unfortunately, not a lot we can do about that. We have a PR open to expand to JSON5 and another to add YAML support so these will help in cases like this.
v
I'll be happy to define it using something else. My needs are for it to be easy and simple. A docker running with files in the mapping folder. I don't wan't to create a java app for this mock
t
Do you know you can also extract the response body to its own file?
v
Yeah I think I read that but in this extracted file, I won't be able to have multi-line right ?
t
So you create a file called e.g.
my-response-body.json
under
__files
then reference it
"bodyFileName": "my-response-body.json"
in your stub instead of
body
v
What I need is to have some "complicated" stuff like :
Copy code
"body": "[ {{#each (jsonPath request.body '$') as |todo|}}
                  { \"todo_id\":
                      {{#if (todo.todo_id == null) }}
                          1{{randomValue length=1 type='NUMERIC'}}
                      {{else}}
                          {{todo.todo_id}}
                      {{/if}}
                },
            {{/each}} ]"
On multi-line so it can be on source control, and be reviewed
t
Then you can format the body however you like
Nothing you stop you source controlling from the parent folder of
mappings
and
__files
This is what lots of teams do
v
Can I really format it the way I want ? is is still a json "my-response-body*.json*"
t
Yes, although it can be whatever format you’re using
It’s just a text file that’s used in place of the
body
value
v
I'm in a meeting rn but I'll give that a try after 🙂 thanks !
👍 1
t
The
Content-Type
response header is what actually determines the type, as far as your calling client is concerned
v
Yep it works ! thanks for that, it is not perfect, but it will definitly cover my needs
5-star support here 😉
t
No probs 👍