Wiremock:如何使用API​​将文件上传到文件夹__files

时间:2017-09-20 07:56:23

标签: wiremock

wiremock的文档说我们可以通过这段代码模拟一个检索文件的请求:

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

但是现在我必须找到一种方法来重新上传文件,否则我会在请求中出现500错误。

<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>

精确:由于我们的基础设施限制,我无法直接上传文件。

2 个答案:

答案 0 :(得分:2)

最新的Wiremock版本具有用于管理存根文件的端点(请参见https://github.com/tomakehurst/wiremock/blob/2.19.0/src/main/java/com/github/tomakehurst/wiremock/admin/AdminRoutes.java#L77

您可以将带有PUT的文件上传到/__admin/files/{filename}。存储在${pwd}/__files下。

答案 1 :(得分:0)

解决方法是使用"body"参数,如下所示:

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

(请注意'1'"1"中的单引号 - 您需要使用\"1\"转义那些。

请参阅http://wiremock.org/docs/stubbing - 指定响应正文部分。

如果您需要JSON有效负载,"jsonBody"参数会更好:

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

另一个不错的功能是引用:

  

已创建的存根映射可以通过调用Java中的WireMock.saveAllMappings或将空主体的请求发布到http://<host>:<port>/__admin/mappings/save来持久保存到映射目录。