Hi everyone. I was wondering if there was a way we...
# help
v
Hi everyone. I was wondering if there was a way we could tell wiremock to use a custom mapping file name when we create and save the desired mapping via the admin API. Please not that I’m using POSTMAN to create and save me mappings, therefore Java solutions won’t help.
l
I am not near my laptop at the moment but there is a command line argument to set the file name template. Maybe that would help ? --filename-template: Set filename template in handlebar format. For endpoint: GET /pets/{id} using the format: }-}.json output will be get-pets-id.json. Default format: }-}-}.json hence by default template filename will be: get-pets-id-1.json.
v
Hi Lee. Thanks very much for your reply. I was wondering, based on this formatting protocol, if I want to add a prefix to the mapping name which changes between different mappings for example “myCustomMappingName-get-pets-id-1.json”, how should I set the file name template inside the mapping file. As far as I understood, the solution you proposed is applied server-wide not per mapping…
l
The default template for the filename is
{{#if name}}{{{name}}}{{else}}{{{method}}}-{{{url}}}{{/if}}-{{{id}}}
so for this mapping:
Copy code
{
  "mappings": [
    {
      "name": "lees-cool-mapping",
      "request": {
        "url": "/test",
        "method": "GET"
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "application/json"
        },
        "body": "{\"message\": \"Hello, World!\"}"
      }
    }
  ]
}
You will get a filename that looks something like -
lees-cool-mapping-a294d0c5-0fc8-4b40-a7e2-12dd015ac5e0.json
If you don't have a name in the stub it will use the url and method instead. If I set the template to something like this -
java -jar wiremock-standalone-3.9.2.jar --filename-template {{{name}}}-{{{method}}}-{{{url}}}.json
The file will be saved with the name along with the method and url -
lees-cool-mapping-get-test.json
Is that the kind of thing you are looking for?
v
Thanks very much. I will try this solution and see if it will work. If we have existing mapping files and modify them to have a name and saver them, will this solution still work? Or do we need to do the mappings all over again?
l
It should work. I haven't tested this though so might be worth doing a test yourself with one mapping
v
Thanks Lee. It did work, although you’d need to delete and re-add your mappings with a name for the name changes to take effect.
👍 1
l
Awesome.