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 55cc0e7ae5..8d9e2b1493 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 @@ -26,6 +26,7 @@ import java.util.List; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; +import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.http.HttpHeaders; @@ -89,7 +90,7 @@ public class WireMockRestServiceServer { /** * Flag to tell the MockRestServiceServer to ignore the order of calls when matching - * requests. The default is true because there is an impleid ordering in the stubs + * requests. The default is true because there is an implied ordering in the stubs * (by url path and with more specific request matchers first). * * @param ignoreExpectOrder flag value (default true) @@ -206,10 +207,29 @@ public class WireMockRestServiceServer { private DefaultResponseCreator response(ResponseDefinition response) { return withStatus(HttpStatus.valueOf(response.getStatus())) - .body(response.getBody()).contentType(contentType(response)) + .body(body(response)).contentType(contentType(response)) .headers(responseHeaders(response)); } + private String body(ResponseDefinition response) { + if (response.getBody()!=null) { + return response.getBody(); + } + String file = response.getBodyFileName(); + if (file!=null) { + ClassPathResource files = new ClassPathResource("__files/"); + if (files.exists()) { + try { + return StreamUtils.copyToString(files.createRelative(file).getInputStream(), Charset.forName("UTF-8")); + } + catch (IOException e) { + throw new IllegalStateException("Cannot locate body file: " + file, e); + } + } + } + return ""; + } + private void requestHeaders(ResponseActions expect, RequestPattern request) { if (request.getHeaders() != null) { for (final String header : request.getHeaders().keySet()) { @@ -269,6 +289,17 @@ public class WireMockRestServiceServer { int value = request(one.getRequest()).compareTo(request(two.getRequest())); if (value == 0) { + if (one.getPriority()!=null) { + if (two.getPriority()!=null) { + return one.getPriority().compareTo(two.getPriority()); + } else { + return -one.getPriority(); + } + } + if (two.getPriority()!=null) { + return -two.getPriority(); + } + // Every mapping has a url pattern, and zero or more header patterns int twos = 0; if (two.getRequest().getHeaders() != null) { 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 8d0be00b9d..21375ca214 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 @@ -24,6 +24,16 @@ public class WiremockMockServerApplicationTests { server.verify(); } + @Test + public void simpleGetWithBodyFile() throws Exception { + MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) // + .baseUrl("http://example.org") // + .stubs("classpath:/mappings/resource-with-body-file.json").build(); + assertThat(this.restTemplate.getForObject("http://example.org/resource", + String.class)).isEqualTo("{\"message\":\"Hello World\"}"); + server.verify(); + } + @Test public void simpleGetWithEmptyPath() throws Exception { MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) // @@ -135,6 +145,16 @@ public class WiremockMockServerApplicationTests { // The first one matches, not the most precise! } + @Test + public void getWithPriortyOrder() throws Exception { + WireMockRestServiceServer.with(this.restTemplate) // + .baseUrl("http://example.org") // + .stubs("classpath:/mappings/resource-with-low-priority.json", + "classpath:/mappings/resource-with-high-priority.json").build(); + assertThat(this.restTemplate.getForObject("http://example.org/resource", + String.class)).isEqualTo("Hello High"); + } + @Test public void simpleGetWithAllStubs() throws Exception { WireMockRestServiceServer.with(this.restTemplate) // diff --git a/spring-cloud-contract-wiremock/src/test/resources/__files/hello.json b/spring-cloud-contract-wiremock/src/test/resources/__files/hello.json new file mode 100644 index 0000000000..33e91d47b0 --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/resources/__files/hello.json @@ -0,0 +1 @@ +{"message":"Hello World"} \ No newline at end of file diff --git a/spring-cloud-contract-wiremock/src/test/resources/mappings/resource-with-body-file.json b/spring-cloud-contract-wiremock/src/test/resources/mappings/resource-with-body-file.json new file mode 100644 index 0000000000..08076b1aa7 --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/resources/mappings/resource-with-body-file.json @@ -0,0 +1,11 @@ +{ + "priority": 100, + "request" : { + "urlPath" : "/resource", + "method" : "GET" + }, + "response" : { + "status" : 200, + "bodyFileName" : "hello.json" + } +} \ No newline at end of file diff --git a/spring-cloud-contract-wiremock/src/test/resources/mappings/resource-with-high-priority.json b/spring-cloud-contract-wiremock/src/test/resources/mappings/resource-with-high-priority.json new file mode 100644 index 0000000000..03ba732953 --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/resources/mappings/resource-with-high-priority.json @@ -0,0 +1,11 @@ +{ + "priority" : 500, + "request" : { + "urlPath" : "/resource", + "method" : "GET" + }, + "response" : { + "status" : 200, + "body" : "Hello High" + } +} \ No newline at end of file diff --git a/spring-cloud-contract-wiremock/src/test/resources/mappings/resource-with-low-priority.json b/spring-cloud-contract-wiremock/src/test/resources/mappings/resource-with-low-priority.json new file mode 100644 index 0000000000..4f684d7d67 --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/resources/mappings/resource-with-low-priority.json @@ -0,0 +1,11 @@ +{ + "priority" : 1000, + "request" : { + "urlPath" : "/resource", + "method" : "GET" + }, + "response" : { + "status" : 200, + "body" : "Hello Low" + } +} \ No newline at end of file diff --git a/spring-cloud-contract-wiremock/src/test/resources/mappings/resource.json b/spring-cloud-contract-wiremock/src/test/resources/mappings/resource.json index 41a9d4e920..02e6285181 100644 --- a/spring-cloud-contract-wiremock/src/test/resources/mappings/resource.json +++ b/spring-cloud-contract-wiremock/src/test/resources/mappings/resource.json @@ -1,4 +1,5 @@ { + "priority": 1, "request" : { "urlPath" : "/resource", "method" : "GET"