0

我试图在延迟后在wiremock中生成响应,该延迟来自传入的请求。例如,请求中用户的姓氏是“delay_10000”然后延迟 10000 毫秒,或者 delay_20000 然后延迟 20000 ......

{
  "request": {
    "method": "POST",
    "headers": {
      "SOAPAction": {
        "matches": "http://redacted"
      }
    },
    "bodyPatterns": [
      {
        "matchesXPath": {
          "expression": "//*[local-name()=\"family-name\"]/text()",
          "matches": "^delay_([0-9]+)$"
        }
      }
    ]
  },
  "response": {
    "status": 200,
    "bodyFileName": "verify.xml",
    "fixedDelayMilliseconds": "{{soapXPath request.body 'number(substring-after(//*[local-name()=\"family-name\"]/text(), \"delay_\"))'}}"
  }
}

任何人都可以确认哪些字段可以被模板化。doco 建议“响应标头和正文”,以及其他地方的 bodyFileName (我有工作),但它没有说明其他响应字段是否可以模板化。

目前我看到

 
2020-09-22 02:43:24.441 Verbose logging enabled
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" com.github.tomakehurst.wiremock.standalone.MappingFileException: Error loading file /home/wiremock/./mappings/equifax_generic_verify_identity_delay.json:
Cannot deserialize value of type `java.lang.Integer` from String "{{soapXPath request.body 'number(substring-after(//*[local-name()="family-name"]/text(), "timeout_"))'}}": not a valid Integer value
    at com.github.tomakehurst.wiremock.standalone.JsonFileMappingsSource.loadMappingsInto(JsonFileMappingsSource.java:121)
    at com.github.tomakehurst.wiremock.core.WireMockApp.loadMappingsUsing(WireMockApp.java:204)
    at com.github.tomakehurst.wiremock.core.WireMockApp.loadDefaultMappings(WireMockApp.java:200)
    at com.github.tomakehurst.wiremock.core.WireMockApp.<init>(WireMockApp.java:103)
    at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:73)
    at com.github.tomakehurst.wiremock.standalone.WireMockServerRunner.run(WireMockServerRunner.java:65)
    at com.github.tomakehurst.wiremock.standalone.WireMockServerRunner.main(WireMockServerRunner.java:134)
stream closed           

首先,我可以看到它在哪里被捕获,但不清楚它在哪里被抛出 https://github.com/tomakehurst/wiremock/blob/master/src/main/java/com/github/tomakehurst/wiremock/standalone/JsonFileMappingsSource。爪哇#L121

其次,我不清楚我是否只是错误地驱动了wiremock,这不可能通过响应转换器实现,但可以通过扩展和“响应定义转换”(http://wiremock.org/docs/extending -wiremock/ )

我可以用一组固定的延迟来解决这个问题——但如果它是动态的会更好

帮助表示赞赏!

4

1 回答 1

0

所以我最初的假设是这是不可能的,因为模板的任何使用都会解析为一个字符串,但fixedDelayMilliseconds需要一个数字,所以 WireMock 会抛出错误:(

我认为您可以使用自定义转换器扩展 WireMock,该转换器将解析响应正文中的延迟并将其作为数字添加到响应中。有关更多信息,请查看扩展 WireMock文档。编辑:我现在看到您已经得出了这个假设。我认为你是对的:D——这很容易通过变压器实现。

于 2020-09-29T16:03:11.500 回答