diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockRestServiceServer.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockRestServiceServer.java index 97f9821835..245e7d8c08 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockRestServiceServer.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockRestServiceServer.java @@ -23,12 +23,27 @@ import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.regex.Pattern; + import javax.xml.xpath.XPathExpressionException; +import com.github.tomakehurst.wiremock.common.Json; +import com.github.tomakehurst.wiremock.http.HttpHeader; +import com.github.tomakehurst.wiremock.http.MultiValue; +import com.github.tomakehurst.wiremock.http.ResponseDefinition; import com.github.tomakehurst.wiremock.matching.ContentPattern; +import com.github.tomakehurst.wiremock.matching.MatchResult; +import com.github.tomakehurst.wiremock.matching.MatchesJsonPathPattern; +import com.github.tomakehurst.wiremock.matching.MatchesXPathPattern; +import com.github.tomakehurst.wiremock.matching.MultiValuePattern; +import com.github.tomakehurst.wiremock.matching.RequestPattern; +import com.github.tomakehurst.wiremock.stubbing.StubMapping; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; +import org.hamcrest.Matcher; import org.hamcrest.MatcherAssert; +import org.hamcrest.TypeSafeMatcher; + import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.http.HttpHeaders; @@ -47,17 +62,6 @@ import org.springframework.util.StreamUtils; import org.springframework.util.StringUtils; import org.springframework.web.client.RestTemplate; -import com.github.tomakehurst.wiremock.common.Json; -import com.github.tomakehurst.wiremock.http.HttpHeader; -import com.github.tomakehurst.wiremock.http.MultiValue; -import com.github.tomakehurst.wiremock.http.ResponseDefinition; -import com.github.tomakehurst.wiremock.matching.MatchResult; -import com.github.tomakehurst.wiremock.matching.MatchesJsonPathPattern; -import com.github.tomakehurst.wiremock.matching.MatchesXPathPattern; -import com.github.tomakehurst.wiremock.matching.MultiValuePattern; -import com.github.tomakehurst.wiremock.matching.RequestPattern; -import com.github.tomakehurst.wiremock.stubbing.StubMapping; - import static org.springframework.test.web.client.match.MockRestRequestMatchers.header; import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; @@ -200,7 +204,7 @@ public class WireMockRestServiceServer { Collections.sort(mappings, new StubMappingComparator()); } for (StubMapping mapping : mappings) { - ResponseActions expect = server.expect(requestTo(request(mapping.getRequest()))); + ResponseActions expect = responseActions(server, mapping); expect.andExpect(method(HttpMethod.valueOf(mapping.getRequest().getMethod().getName()))); mapping.getRequest().getBodyPatterns(); bodyPatterns(expect, mapping.getRequest()); @@ -210,6 +214,13 @@ public class WireMockRestServiceServer { return server; } + private ResponseActions responseActions(MockRestServiceServer server, StubMapping mapping) { + if (StringUtils.hasText(mapping.getRequest().getUrl()) || StringUtils.hasText(mapping.getRequest().getUrlPath())) { + return server.expect(requestTo(request(mapping.getRequest()))); + } + return server.expect(requestTo(requestMatcher(mapping.getRequest()))); + } + private void bodyPatterns(ResponseActions expect, RequestPattern request) { if (request.getBodyPatterns() == null) { return; @@ -248,6 +259,27 @@ public class WireMockRestServiceServer { : request.getUrlPath()); } + private Matcher requestMatcher(RequestPattern request) { + return new TypeSafeMatcher() { + @Override + protected boolean matchesSafely(String item) { + if (request.getUrlMatcher() != null) { + return request.getUrlMatcher().match(item).isExactMatch(); + } else if (request.getUrlPathPattern() != null) { + return Pattern.compile(request.getUrlPathPattern()).matcher(item).matches(); + } else if (request.getUrlPattern() != null) { + return Pattern.compile(request.getUrlPattern()).matcher(item).matches(); + } + return false; + } + + @Override + public void describeTo(Description description) { + + } + }; + } + private String pattern(String location) { if (!StringUtils.getFilename(location).contains(".") && !location.contains("*")) { if (!location.endsWith("/")) { diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/WiremockMockServerApplicationTests.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/WiremockMockServerApplicationTests.java index 8f57a61452..ede21d9ff0 100644 --- a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/WiremockMockServerApplicationTests.java +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/WiremockMockServerApplicationTests.java @@ -4,15 +4,15 @@ import java.net.URI; import java.util.Collections; import java.util.List; +import com.fasterxml.jackson.annotation.JsonCreator; import org.junit.Test; + import org.springframework.http.MediaType; import org.springframework.http.RequestEntity; import org.springframework.http.ResponseEntity; import org.springframework.test.web.client.MockRestServiceServer; import org.springframework.web.client.RestTemplate; -import com.fasterxml.jackson.annotation.JsonCreator; - import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; @@ -382,6 +382,26 @@ public class WiremockMockServerApplicationTests { } } + @Test + public void getWithUrlPathMatching() throws Exception { + MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) // + .baseUrl("http://example.org") // + .stubs("classpath:/mappings/url-path-pattern.json").build(); + assertThat(this.restTemplate.getForObject("http://example.org/123", + String.class)).isEqualTo("Hello Url Path Matcher"); + server.verify(); + } + + @Test + public void getWithUrlMatching() throws Exception { + MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) // + .baseUrl("http://example.org") // + .stubs("classpath:/mappings/url-matches.json").build(); + assertThat(this.restTemplate.getForObject("http://example.org/123", + String.class)).isEqualTo("Hello Url Matcher"); + server.verify(); + } + public static class Things { public List things; diff --git a/spring-cloud-contract-wiremock/src/test/resources/mappings/url-matches.json b/spring-cloud-contract-wiremock/src/test/resources/mappings/url-matches.json new file mode 100644 index 0000000000..0d4ce3a135 --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/resources/mappings/url-matches.json @@ -0,0 +1,10 @@ +{ + "request": { + "method": "GET", + "urlPattern": "^.*[0-9]{3}$" + }, + "response" : { + "status" : 200, + "body" : "Hello Url Matcher" + } +} \ No newline at end of file diff --git a/spring-cloud-contract-wiremock/src/test/resources/mappings/url-path-pattern.json b/spring-cloud-contract-wiremock/src/test/resources/mappings/url-path-pattern.json new file mode 100644 index 0000000000..0821ef4f0d --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/resources/mappings/url-path-pattern.json @@ -0,0 +1,10 @@ +{ + "request": { + "method": "GET", + "urlPathPattern": "/([a-zA-Z0-9/-]*)" + }, + "response" : { + "status" : 200, + "body" : "Hello Url Path Matcher" + } +} \ No newline at end of file