11

the documentation of wiremock says that we can mock a a request that retrieve a file thanks to this code :

{ "request": { "method": "GET", "url": "/body-file" }, "response": { "status": 200, "bodyFileName": "path/to/myfile.xml" } }

But now I have to find a way to reaaly upload the file other wise I have 500 error on the request.

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
    <title>Error 500 </title>
</head>
<body>
    <h2>HTTP ERROR: 500</h2>
    <p>Problem accessing /body-file. Reason:

        <pre>    java.lang.RuntimeException: java.io.FileNotFoundException: /wiremock-standalone/./__files/path/to/myfile.xml (No such file or directory)</pre>
    </p>
    <hr />
    <i>
        <small>Powered by Jetty://</small>
    </i>
</body>

Precision : I cannot upload the file directly due to our infrastructure constraints.

kenji_getpowered
  • 338
  • 2
  • 10

2 Answers2

15

Recent Wiremock versions have endpoint to manage stub files (see https://github.com/tomakehurst/wiremock/blob/2.19.0/src/main/java/com/github/tomakehurst/wiremock/admin/AdminRoutes.java#L77)

You can upload a file with a PUT to /__admin/files/{filename}. There are stored under ${pwd}/__files.

LoganMzz
  • 1,597
  • 3
  • 18
  • 31
  • is there an api to delete these files also? – Minisha Jun 23 '21 at 07:38
  • 1
    It seems `DELETE` verb is implemented for the same URL pattern: https://github.com/tomakehurst/wiremock/blob/2.19.0/src/main/java/com/github/tomakehurst/wiremock/admin/AdminRoutes.java#L78 – LoganMzz Jun 23 '21 at 13:44
  • Possible to change the path where the files are stored? Instead of this ${pwd}/__files wanted to be ${pwd}/__files/somepath – Minisha Jun 23 '21 at 13:54
  • @Minisha Depending how you run Wiremock, you can change `${pwd}` by setting root directory. For example, you can use `--root-dir` in standalone mode (http://wiremock.org/docs/running-standalone/) – LoganMzz Jun 23 '21 at 19:48
  • Want pwd to be still the same … however looking for solution to place the file in a nested directory structure like /__files/path1/a.jpg. /__files/path2/path3/b.PDF /__files/path4/c.jpg and so on – Minisha Jun 23 '21 at 23:50
  • @Minisha Sorry I misunderstood, no you can't. I had created a PR to handle it but was never considered: https://github.com/tomakehurst/wiremock/pull/1087 – LoganMzz Jun 25 '21 at 07:05
  • How to get the saved image? GET followed by the image id is not working – Minisha Jun 28 '21 at 07:06
  • @Minisha https://github.com/wiremock/wiremock/pull/1087 PR has been merged. Regarding your last question I don't know what's your refering to. Images are accessed like any other files. – LoganMzz Nov 04 '21 at 12:29
2

A workaround would be to use the "body" parameter, something like this:

{
    {
    "request": {
        "method": "GET",
        "url": "/body-file"
    },
    "response": {
        "status": 200,
        "body": "<example><node Id='1' Action='Insert' /></example>"
    }
}

(Note the single quotes in '1' vs. "1" - you'd need to escape those with \"1\".

See http://wiremock.org/docs/stubbing - Specifying the response body section.

If you need JSON payload, it's even nicer with the "jsonBody" parameter:

{
    {
    "request": {
        "method": "GET",
        "url": "/body-file"
    },
    "response": {
        "status": 200,
        "jsonBody": 
            { 
              "field1": "value1", 
              "field2" : "value2" 
            }
    }
}

Another nice feature is, quoting:

Stub mappings which have been created can be persisted to the mappings directory via a call to WireMock.saveAllMappings in Java or posting a request with an empty body to http://<host>:<port>/__admin/mappings/save.

Siaynoq
  • 452
  • 1
  • 5
  • 13
  • This does not answer the question as I have to pass a file. What you suggest is just the regular usage... – kenji_getpowered May 04 '18 at 12:57
  • @kenji_getpowered True, this would only be a workaround. If you have the file, you could upload the contents to the server and save it. It's not very elegant (especially with huge files), but maybe usable in certain cases. – Siaynoq May 09 '18 at 00:04
  • Precision : I cannot upload the file directly due to our infrastructure constraints. This sentence was in my question. I thank you but your answer is not relevant at all. – kenji_getpowered May 10 '18 at 05:25
  • 1
    Yeah, I saw that, and this method is a workaround - instead of direct upload you can send a POST message and persist the body payload on the local filesystem (on the aforementioned server). Sorry if it's not usable by you, I hope others can still find it useful :) – Siaynoq May 11 '18 at 00:02