Wiremock请求匹配-与WireMockRestServiceServer一起使用时,无法将URL与动态路径变量匹配

时间:2018-09-21 16:30:21

标签: regex spring-boot junit testcase wiremock

存根映射

range(-2**63, 2**63)

无法匹配网址

Python's array module (绝对路径和相对路径均无效)

还尝试用urlPattern替换urlPathPattern,但没有成功

尝试以下所有组合。什么都没用

range(2**64)

注意: Wiremock版本2.18.0,Spring Boot 2.0

实际上,除了url(在具有绝对URL的存根映射中),对我来说没有任何作用。

如果我单独使用Wiremock,它会完美工作。如果我将Wiremock与WireMockRestServiceServer(spring-cloud-contract)结合使用,则不起作用。

工作代码-单独使用Wiremock

{
    "request": {
        "method": "GET",
        "urlPathPattern": "/validation/session/([a-zA-Z0-9/-]*)"
    },
    "response": {
        "status": 200,
        "jsonBody": {
            "isIpBlocked": "Y",
            "serviceMessage": {
                "type": "OK",
                "code": "200",
                "description": "IP validated successfully. No result found"
            }
        },
        "headers": {
            "Content-Type": "application/json"
        }
    }
}

不起作用-使用WireMockRestServiceServer代码

/validation/session/([a-zA-Z0-9\\-]+)
/validation/session/([a-zA-Z0-9\\-]*)
/validation/session/([a-zA-Z0-9/-]+)
/validation/session/([a-zA-Z0-9/-]*)

2 个答案:

答案 0 :(得分:0)

如WireMock邮件列表thread上所述,应将示例更改为相对的网址,如WireMock Guide on Request Matching中所述:

"urlPathPattern": "/validation/session/([a-zA-Z0-9/-]*)"

答案 1 :(得分:0)

我找到了一个临时解决方案。我已经为动态路径变量编写了自己的模式匹配器。

MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate)
          .stubs("classpath:/stubs/valic-get-user-details-mock-response.json").build();  //no dynamic url

  Resource validateIpResponse = new ClassPathResource("/stubs/validate-ip-success-mock-response.json");
  Pattern pattern = Pattern.compile("https://helloworld.com/validation/session/([a-zA-Z0-9/-]*)");
  Matcher<String> matcher = MatchesPattern.matchesPattern(pattern);
  server.expect(MockRestRequestMatchers.requestTo(matcher))
      .andRespond(MockRestResponseCreators.withSuccess(validateIpResponse, MediaType.APPLICATION_JSON));

我在Spring Cloud Contract GitHub页面上提出了这个问题。 https://github.com/spring-cloud/spring-cloud-contract/issues/740