Add test and correct double "/" in fail path
A double "/" wrks with the file system but not in a jar file apparently, so the test isn't perfect yet. Also improves javadocs for stubs().
This commit is contained in:
@@ -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
|
||||
* <code>**/*.json</code> appended, where ".json" is the value of the
|
||||
* {@link #suffix(String) suffix}). Examples:
|
||||
*
|
||||
* <pre>
|
||||
* classpath:/mappings/foo.json
|
||||
* classpath:/mappings/*.json
|
||||
* classpath:META-INF/com.example/stubs/1.0.0/mappings/**/*.json
|
||||
* file:src/test/resources/stubs
|
||||
* </pre>
|
||||
*
|
||||
* @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) {
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"request" : {
|
||||
"urlPath" : "/resource",
|
||||
"method" : "GET"
|
||||
},
|
||||
"response" : {
|
||||
"status" : 200,
|
||||
"body" : "Hello World"
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user