Wiremock模板的fixedDelayMilliseconds

问题描述

我正在尝试在一个延迟后的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.java#L121

第二,我不清楚我是否只是在错误地操纵wiremock,这不可能通过响应转换器实现,但是可以通过扩展和“响应定义转换”(http://wiremock.org/docs/extending-wiremock/)实现>

我可以通过一组固定的延迟来解决此问题-但如果它是动态的,那就更好了

帮助表示赞赏!

解决方法

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

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