Add additional build() step to allow ordered stubs

This commit is contained in:
Dave Syer
2016-07-28 09:32:21 +01:00
parent d82a5917b1
commit bcce5699de
9 changed files with 133 additions and 35 deletions

View File

@@ -29,7 +29,8 @@ public class WiremockForDocsMockServerApplicationTests {
public void contextLoads() throws Exception {
// will read stubs classpath
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate)
.baseUrl("http://example.org").stubs("classpath:/stubs/resource.json");
.baseUrl("http://example.org").stubs("classpath:/stubs/resource.json")
.build();
// We're asserting if WireMock responded properly
assertThat(this.service.go()).isEqualTo("Hello World");
server.verify();

View File

@@ -28,7 +28,7 @@ public class WiremockMockServerApplicationTests {
public void contextLoads() throws Exception {
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
.baseUrl("http://example.org") //
.stubs("classpath:/stubs/resource.json");
.stubs("classpath:/stubs/resource.json").build();
assertThat(this.service.go()).isEqualTo("Hello World");
server.verify();
}

View File

@@ -28,7 +28,7 @@ public class WiremockMockServerApplicationTests {
public void contextLoads() throws Exception {
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
.baseUrl("http://example.org") //
.stubs("classpath:/stubs/**/*.json");
.stubs("classpath:/stubs/**/*.json").build();
assertThat(this.service.go()).isEqualTo("Hello World");
server.verify();
}

View File

@@ -28,7 +28,7 @@ public class WiremockMockServerApplicationTests {
public void contextLoads() throws Exception {
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
.baseUrl("http://example.org") //
.stubs("classpath:/stubs");
.stubs("classpath:/stubs").build();
assertThat(this.service.go()).isEqualTo("Hello World");
server.verify();
}

View File

@@ -16,23 +16,33 @@
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.withSuccess;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.test.web.client.MockRestServiceServer.MockRestServiceServerBuilder;
import org.springframework.test.web.client.ResponseActions;
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.ResponseDefinition;
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.withSuccess;
/**
* 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
@@ -49,10 +59,17 @@ public class WireMockRestServiceServer {
private String baseUrl = "";
private MockRestServiceServer server;
private MockRestServiceServerBuilder builder;
private List<String> locations = new ArrayList<String>();
private WireMockRestServiceServer(RestTemplate restTemplate) {
this.server = MockRestServiceServer.bindTo(restTemplate).build();
this.builder = MockRestServiceServer.bindTo(restTemplate);
}
public WireMockRestServiceServer ignoreExpectOrder(boolean ignoreExpectOrder) {
this.builder.ignoreExpectOrder(ignoreExpectOrder);
return this;
}
public WireMockRestServiceServer suffix(String suffix) {
@@ -65,8 +82,14 @@ public class WireMockRestServiceServer {
return this;
}
public MockRestServiceServer stubs(String... locations) {
for (String location : locations) {
public WireMockRestServiceServer stubs(String... locations) {
this.locations.addAll(Arrays.asList(locations));
return this;
}
public MockRestServiceServer build() {
MockRestServiceServer server = builder.build();
for (String location : this.locations) {
try {
if (!StringUtils.getFilename(location).contains(".")
&& !location.contains("*")) {
@@ -80,11 +103,12 @@ public class WireMockRestServiceServer {
mapping = Json
.read(StreamUtils.copyToString(resource.getInputStream(),
Charset.defaultCharset()), StubMapping.class);
this.server
.expect(requestTo(
this.baseUrl + mapping.getRequest().getUrlPath()))
.andRespond(withSuccess(mapping.getResponse().getBody(),
MediaType.TEXT_PLAIN));
ResponseActions expect = server.expect(
requestTo(this.baseUrl + mapping.getRequest().getUrlPath()));
requestHeaders(expect, mapping.getRequest());
expect.andRespond(withSuccess(mapping.getResponse().getBody(),
contentType(mapping.getResponse()))
.headers(responseHeaders(mapping.getResponse())));
}
}
catch (IOException e) {
@@ -92,7 +116,41 @@ public class WireMockRestServiceServer {
e);
}
}
return this.server;
return server;
}
private void requestHeaders(ResponseActions expect, RequestPattern request) {
if (request.getHeaders() != null) {
for (final String header : request.getHeaders().keySet()) {
final MultiValuePattern pattern = request.getHeaders().get(header);
// TODO: match the headers
}
}
}
private HttpHeaders responseHeaders(ResponseDefinition response) {
HttpHeaders headers = new HttpHeaders();
if (response.getHeaders() != null) {
for (HttpHeader header : response.getHeaders().all()) {
if (!header.keyEquals("Content-Type")) {
for (String value : header.values()) {
headers.add(header.key(), value);
}
}
}
}
return headers;
}
private MediaType contentType(ResponseDefinition response) {
String value = null;
if (response.getHeaders() != null) {
HttpHeader header = response.getHeaders().getHeader("Content-Type");
if (header != null) {
value = header.firstValue();
}
}
return value == null ? MediaType.TEXT_PLAIN : MediaType.valueOf(value);
}
public static WireMockRestServiceServer with(RestTemplate restTemplate) {

View File

@@ -17,7 +17,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = WiremockTestsApplication.class, properties = "app.baseUrl=https://localhost:${wiremock.server.https-port}", webEnvironment = WebEnvironment.NONE)
@DirtiesContext
@AutoConfigureWireMock(httpsPort = 9999)
@AutoConfigureWireMock(httpsPort = 9999, port=0)
public class AutoConfigureWireMockHttpsPortApplicationTests {
@Autowired

View File

@@ -3,33 +3,51 @@ package org.springframework.cloud.contract.wiremock;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate;
@RunWith(SpringRunner.class)
@SpringBootTest(classes=WiremockTestsApplication.class, webEnvironment=WebEnvironment.NONE)
@DirtiesContext
public class WiremockMockServerApplicationTests {
@Autowired
private RestTemplate restTemplate;
@Autowired
private Service service;
private RestTemplate restTemplate = new RestTemplate();
@Test
public void contextLoads() throws Exception {
public void simpleGet() throws Exception {
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
.baseUrl("http://example.org") //
.stubs("classpath:/mappings");
assertThat(this.service.go()).isEqualTo("Hello World");
.stubs("classpath:/mappings/resource.json").build();
assertThat(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) //
.baseUrl("http://example.org") //
.stubs("classpath:/mappings/poster.json").build();
assertThat(restTemplate.postForObject("http://example.org/poster", "greeting",
String.class)).isEqualTo("Hello World");
server.verify();
}
@Test
public void postWithHeader() throws Exception {
WireMockRestServiceServer.with(this.restTemplate) //
.baseUrl("http://example.org") //
.stubs("classpath:/mappings/*.json").ignoreExpectOrder(true).build();
assertThat(restTemplate.exchange("http://example.org/poster", HttpMethod.POST,
RequestEntity.EMPTY, String.class).getBody()).isEqualTo("Accepted World");
}
@Test
public void simpleGetWithAllStubs() throws Exception {
WireMockRestServiceServer.with(this.restTemplate) //
.baseUrl("http://example.org") //
.stubs("classpath:/mappings").ignoreExpectOrder(true).build();
assertThat(restTemplate.getForObject("http://example.org/resource", String.class))
.isEqualTo("Hello World");
}
}

View File

@@ -0,0 +1,11 @@
{
"request" : {
"urlPath" : "/poster",
"method" : "POST",
"headers" : { "Accept" : { "equalTo" : "text/plain" }}
},
"response" : {
"status" : 200,
"body" : "Accepted World"
}
}

View File

@@ -0,0 +1,10 @@
{
"request" : {
"urlPath" : "/poster",
"method" : "POST"
},
"response" : {
"status" : 200,
"body" : "Hello World"
}
}