【问题标题】:Is it possible to pass in a file as part of the request body using WireMock?是否可以使用 WireMock 将文件作为请求正文的一部分传递?
【发布时间】:2020-02-18 13:40:29
【问题描述】:

我目前正在使用 Wiremock 编写集成测试。目前,我将 id 和组织 id 定义为参数。与其对 50 个字段重复此过程,是否可以将 JSON 文件作为请求传递?即使用这个文件作为requestBody。

stubFor(post(urlEqualTo("/v1/transaction"))
            .withRequestBody(
                    matchingJsonPath("$.data.id", containing("1")))
            .withRequestBody(matchingJsonPath("$.data.organisation_id", containing("2")))
            .willReturn(aResponse()
                    .withHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE)
                    .withStatus(HttpStatus.CREATED.value())
                    .withBodyFile("create_successful_response.json")));

【问题讨论】:

  • requestBody 不是请求正文;请求正文由客户端传输。您设置的内容将在请求正文上执行。

标签: java junit5 wiremock


【解决方案1】:

您可能会发现 2.26.0 中添加的占位符功能更适合您在这里想要的内容。

占位符允许您使用 equalToJson(...) 与特定字段的松散匹配,例如

{
  "data": {
    "id": "${json-unit.any-string}",
    "organisation_id": "${json-unit.regex}.*1.*}"
  }
}

更多详情:http://wiremock.org/docs/request-matching/#placeholders

【讨论】:

    猜你喜欢
    • 2012-03-19
    • 2016-04-23
    • 2021-11-14
    • 2013-03-17
    • 2021-05-02
    • 2019-02-16
    • 2017-12-09
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多