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

Regex Wiremock请求匹配-与WireMockRestServiceServer一起使用时,无法将url与动态路径变量匹配,regex,spring-boot,junit,testcase,wiremock,Regex,Spring Boot,Junit,Testcase,Wiremock,存根映射 { "request": { "method": "GET", "urlPathPattern": "/validation/session/([a-zA-Z0-9/-]*)" }, "response": { "status": 200, "jsonBody": { "isIpBlocked": "Y", "serviceMessage": {

存根映射

{
    "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"
        }
    }
}
无法匹配URL

(绝对路径和相对路径均不工作)

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

尝试了以下所有组合。没用

/validation/session/([a-zA-Z0-9\\-]+)
/validation/session/([a-zA-Z0-9\\-]*)
/validation/session/([a-zA-Z0-9/-]+)
/validation/session/([a-zA-Z0-9/-]*)
注意:Wiremock 2.18.0版,Spring Boot 2.0版

实际上,除了url(在带有绝对url的存根映射中)之外,我什么都做不到

如果我单独使用Wiremock,效果会非常好。如果我将Wiremock与WireMockRestServiceServer(SpringCloudContract)结合使用,它将不起作用

工作代码-仅使用Wiremock

@Rule
  public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8080).httpsPort(443));

stubFor(get(urlMatching("/validation/session/([a-zA-Z0-9/-]*)"))        .willReturn(aResponse().withStatus(HttpStatus.OK.value()).withHeader("Content-Type", "application/json")
              .withBody("{\"isIpBlocked\": \"Y\"}")));
不工作-使用WireMockRestServiceServer编码

MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate)
      .stubs("classpath:/stubs/**/validate-ip-success-mock-response.json").build();

如WireMock邮件列表中所述,应将示例更改为相对URL,如中所述:


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

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));
MockRestServiceServer=WireMockRestServiceServer.with(this.restemplate)
.stubs(“classpath:/stubs/valic get user details mock response.json”).build()//没有动态url
Resource validateIpResponse=new ClassPathResource(“/stubs/validate ip success mock response.json”);
Pattern=Pattern.compile(“https://helloworld.com/validation/session/([a-zA-Z0-9/-]*)”;
Matcher Matcher=MatchesPattern.MatchesPattern(模式);
expect(MockRestRequestMatchers.requestTo(matcher))
.andRespond(mockResponseSecreators.withSuccess(validateIpResponse,MediaType.APPLICATION_JSON));
我在SpringCloudContract GitHub页面上提出了这个问题。

您的
urlPathPattern
是绝对URL,您尝试过相对路径吗?另外,请举例说明什么对您有效。如果什么都不起作用,那么我们可能需要更多信息来帮助您,因为问题可能在您的配置中的其他地方。您可能需要编辑第一个存根映射片段。您的urlPathPattern正则表达式缺少一个右括号
([a-zA-Z0-9/-]*
这只是一个复制粘贴错误。我刚刚添加了括号。这更像是WireMockRestServiceServer问题。它与Wiremock无关。我已经尝试使用绝对和相对URL。它不起作用。此外,更新了我的问题。
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));