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 4539612890..c9a9f38f29 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
@@ -16,6 +16,9 @@
package org.springframework.cloud.contract.wiremock;
+import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
+import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
+
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
@@ -42,9 +45,6 @@ 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.requestTo;
-import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
-
/**
* Convenience class for loading WireMock stubs into a {@link MockRestServiceServer}. In
* this way using a {@link RestTemplate} can mock the responses from a server using
@@ -119,7 +119,16 @@ public class WireMockRestServiceServer {
/**
* Add some resource locations for stubs. Each location can be a resource path (to a
* single JSON file), or a pattern with ant-style wildcards to load all stubs that
- * match.
+ * match, or a plain directory name (which will have
+ * **/*.json appended, where ".json" is the value of the
+ * {@link #suffix(String) suffix}). Examples:
+ *
+ *
+ * classpath:/mappings/foo.json + * classpath:/mappings/*.json + * classpath:META-INF/com.example/stubs/1.0.0/mappings/**/*.json + * file:src/test/resources/stubs + ** * @param locations a set of resource locations * @return this @@ -141,15 +150,13 @@ public class WireMockRestServiceServer { try { for (Resource resource : this.resolver.getResources(pattern(location))) { StubMapping mapping = mapping(resource); - ResponseActions expect = server.expect( - requestTo(this.baseUrl + mapping.getRequest().getUrlPath())); + ResponseActions expect = server.expect(requestTo(this.baseUrl + mapping.getRequest().getUrlPath())); requestHeaders(expect, mapping.getRequest()); expect.andRespond(response(mapping.getResponse())); } } catch (IOException e) { - throw new IllegalStateException("Cannot load resources for: " + location, - e); + throw new IllegalStateException("Cannot load resources for: " + location, e); } } return server; @@ -160,20 +167,19 @@ public class WireMockRestServiceServer { if (!location.endsWith("/")) { location = location + "/"; } - location = location + "/**/*" + this.suffix; + location = location + "**/*" + this.suffix; } return location; } private StubMapping mapping(Resource resource) throws IOException { - return Json.read(StreamUtils.copyToString(resource.getInputStream(), - Charset.defaultCharset()), StubMapping.class); + return Json.read(StreamUtils.copyToString(resource.getInputStream(), Charset.defaultCharset()), + StubMapping.class); } private DefaultResponseCreator response(ResponseDefinition response) { - return withStatus(HttpStatus.valueOf(response.getStatus())) - .body(response.getBody()).contentType(contentType(response)) - .headers(responseHeaders(response)); + return withStatus(HttpStatus.valueOf(response.getStatus())).body(response.getBody()) + .contentType(contentType(response)).headers(responseHeaders(response)); } private void requestHeaders(ResponseActions expect, RequestPattern request) { 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 4b4236597b..6a8706c56c 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 @@ -22,6 +22,16 @@ public class WiremockMockServerApplicationTests { server.verify(); } + @Test + public void simpleGetWithContentType() throws Exception { + MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) // + .baseUrl("http://example.org") // + .stubs("classpath:/mappings/resource-with-content-type.json").build(); + assertThat(this.restTemplate.getForObject("http://example.org/resource", String.class)) + .isEqualTo("Hello World"); + server.verify(); + } + @Test public void simplePost() throws Exception { MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) // @@ -50,4 +60,13 @@ public class WiremockMockServerApplicationTests { .isEqualTo("Hello World"); } + @Test + public void simpleGetWithAllStubsInDirectoryWithPeriod() throws Exception { + WireMockRestServiceServer.with(this.restTemplate) // + .baseUrl("http://example.org") // + .stubs("classpath:/io.stubs/mappings").ignoreExpectOrder(true).build(); + assertThat(this.restTemplate.getForObject("http://example.org/resource", String.class)) + .isEqualTo("Hello World"); + } + } diff --git a/spring-cloud-contract-wiremock/src/test/resources/io.stubs/mappings/resource.json b/spring-cloud-contract-wiremock/src/test/resources/io.stubs/mappings/resource.json new file mode 100644 index 0000000000..41a9d4e920 --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/resources/io.stubs/mappings/resource.json @@ -0,0 +1,10 @@ +{ + "request" : { + "urlPath" : "/resource", + "method" : "GET" + }, + "response" : { + "status" : 200, + "body" : "Hello World" + } +} \ No newline at end of file diff --git a/spring-cloud-contract-wiremock/src/test/resources/mappings/resource-with-content-type.json b/spring-cloud-contract-wiremock/src/test/resources/mappings/resource-with-content-type.json new file mode 100644 index 0000000000..8c82923a4a --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/resources/mappings/resource-with-content-type.json @@ -0,0 +1,15 @@ +{ + "request" : { + "urlPath" : "/resource", + "method" : "GET" + }, + "response" : { + "status" : 200, + "body" : "Hello World", + "headers" : { + "ETag" : "\"356b53e9c9e124a7772d88f087a58838\"", + "Content-Type" : "application/json", + "Cache-Control" : "max-age=604800" + } + } +} \ No newline at end of file