3

Stub mapping

{
    "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"
        }
    }
}

cannot match the URL

https://mywebsite/validation/session/687d69ae-42a8-4584-a395-8e0c876bacae (Both absolute path and the relative path is not working)

Also tried replacing urlPathPattern with urlPattern with no success

Tried all the below combination. Nothing worked

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

Note: Wiremock version 2.18.0, Spring Boot 2.0

Actually, except url (in stub mapping with absolute URL) nothing is working for me.

It works perfectly if I use Wiremock alone. It doesn't work if I use Wiremock in tandem with WireMockRestServiceServer (spring-cloud-contract).

Working code - with Wiremock alone

@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\"}")));

Not working - code with WireMockRestServiceServer

MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate)
      .stubs("classpath:/stubs/**/validate-ip-success-mock-response.json").build();
Thiagarajan Ramanathan
  • 1,035
  • 5
  • 24
  • 32
  • your `urlPathPattern` is absolute URL, did you try relative path? – yishaiz Sep 26 '18 at 07:31
  • 1
    also, please give example of what *is* working for you. If nothing is working then we might need more information in order to help you, as the issue might be somewhere else in your configuration. – yishaiz Sep 26 '18 at 07:33
  • You might want to edit your first stub mapping snippet. Your urlPathPattern regex is missing a closing parenthesis `([a-zA-Z0-9/-]*` – jmrah Sep 26 '18 at 13:34
  • It is just a copy paste error. I just added the parenthesis. It is more of a WireMockRestServiceServer issue. It has nothing to do with Wiremock. – Thiagarajan Ramanathan Sep 26 '18 at 14:29

2 Answers2

2

As discussed on the WireMock Mailinglist thread the example should be changed to a relative URL as described in the WireMock Guide on Request Matching:

"urlPathPattern": "/validation/session/([a-zA-Z0-9/-]*)"
A. Kootstra
  • 6,827
  • 3
  • 20
  • 43
1

I found an interim solution. I have written my own pattern matcher for dynamic path variable.

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));

I have raised this issue with spring cloud contract GitHub page. https://github.com/spring-cloud/spring-cloud-contract/issues/740

Thiagarajan Ramanathan
  • 1,035
  • 5
  • 24
  • 32