Add support for explicit priority in mock server stubs
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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) //
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{"message":"Hello World"}
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"priority": 100,
|
||||
"request" : {
|
||||
"urlPath" : "/resource",
|
||||
"method" : "GET"
|
||||
},
|
||||
"response" : {
|
||||
"status" : 200,
|
||||
"bodyFileName" : "hello.json"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"priority" : 500,
|
||||
"request" : {
|
||||
"urlPath" : "/resource",
|
||||
"method" : "GET"
|
||||
},
|
||||
"response" : {
|
||||
"status" : 200,
|
||||
"body" : "Hello High"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"priority" : 1000,
|
||||
"request" : {
|
||||
"urlPath" : "/resource",
|
||||
"method" : "GET"
|
||||
},
|
||||
"response" : {
|
||||
"status" : 200,
|
||||
"body" : "Hello Low"
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
{
|
||||
"priority": 1,
|
||||
"request" : {
|
||||
"urlPath" : "/resource",
|
||||
"method" : "GET"
|
||||
|
||||
Reference in New Issue
Block a user