11

I'm using Wiremock to stub out some API calls made by mobile clients.

Struggling to get templated filename from request.

Can anyone confirm if this should even work with Wiremock standalone pls?

"response": { "status": 200, "bodyFileName": "Account-{{jsonPath request.body '$.user.identity'}}.json”}

I've got static files to be returned fine, just seems like bodyFileName doesn't like templating.

Example:

I configure wm with following Json:

{
    "request": {
        "method" : "GET",
        "url": "/users/D8428899330"
    },
    "response": {
        "status": 200,
        "bodyFileName": "user-{{request.path.[1]}}.json"
    }
}

Then when I attempt the url, http://localhost:9696/users/D8428899330 I get the following error,

HTTP ERROR: 500 Problem accessing /users/D8428899330. Reason:

java.lang.RuntimeException: java.io.FileNotFoundException: /Users/iainframe/Documents/__files/user-{{request.path.[1]}}.json (No such file or directory)

Should indicate that the file user-D8428899330.json resides in the correct location as I've hard coded it and it returns it ok. The command to start WireMock is:

 java -jar ~/Documents/wm.jar --port 9696 --global-response-templating --verbose --root-dir /Users/iainframe/Documents/ 
A. Kootstra
  • 6,827
  • 3
  • 20
  • 43
iFrame
  • 111
  • 1
  • 5
  • Did you enable templating when you started the WireMock process? – Tom Oct 16 '17 at 12:15
  • Yes, I ran this command line : java -jar ~/Documents/wm.jar --port 9696 --global-response-templating --verbose --root-dir /Users/iainframe/Documents/ – iFrame Oct 17 '17 at 15:05
  • Can you describe the problem in more detail? Are you seeing an error message? – Tom Oct 17 '17 at 16:42
  • 1
    The error message is the HTTP 500 error as highlighted in the question. This still persists in the `wiremock-standalone-2.14.0` version. – A. Kootstra Jan 19 '18 at 16:40
  • I faced the same problem and found that {{request.path.[1]}} would not be mapped within bodyFileName. This means you have to create your custom Transformer, to do that. If you create a file named "user-{{request.path.[1]}}.json" this would work, but you doesn't have the dynamic part from your request :( – matzino Apr 06 '18 at 14:55

2 Answers2

1

The following response definition works fine for me with Wiremock 2.25.1 using the response template transformer.

  "response": {
     "transformers": ["response-template"],
     "status": 200,
     "bodyFileName": "user-{{jsonPath request.body '$.userid'}}.json" }

and this also works fine:

"response": {
     "transformers": ["response-template"],
     "status": 200,
     "bodyFileName": "user-{{request.path.[3]}}.json" }
1

Interesting, I was running into the same problem and was able to solve it by starting WireMock with --global-response-templating. But you are using that parameter too... If it matters, I'm using WireMock 2.33.2.

Jon Onstott
  • 13,499
  • 16
  • 80
  • 133