Fix mock server when path is empty

When the path is empty "/" is a better default than "null".
This commit is contained in:
Dave Syer
2016-10-11 13:57:36 +02:00
parent b17238ff4e
commit 870e42f565
3 changed files with 39 additions and 10 deletions

View File

@@ -16,15 +16,6 @@
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;
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;
@@ -38,6 +29,12 @@ import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.github.tomakehurst.wiremock.common.Json;
import com.github.tomakehurst.wiremock.http.HttpHeader;
import com.github.tomakehurst.wiremock.http.ResponseDefinition;
@@ -45,6 +42,9 @@ 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
@@ -150,7 +150,8 @@ 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(request(mapping.getRequest())));
requestHeaders(expect, mapping.getRequest());
expect.andRespond(response(mapping.getResponse()));
}
@@ -162,6 +163,10 @@ public class WireMockRestServiceServer {
return server;
}
private String request(RequestPattern request) {
return this.baseUrl + (request.getUrlPath() == null ? "/" : request.getUrlPath());
}
private String pattern(String location) {
if (!StringUtils.getFilename(location).contains(".") && !location.contains("*")) {
if (!location.endsWith("/")) {

View File

@@ -22,6 +22,16 @@ public class WiremockMockServerApplicationTests {
server.verify();
}
@Test
public void simpleGetWithEmptyPath() throws Exception {
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
.baseUrl("http://example.org") //
.stubs("classpath:/mappings/resource-with-empty-path.json").build();
assertThat(this.restTemplate.getForObject("http://example.org/", String.class))
.isEqualTo("Hello World");
server.verify();
}
@Test
public void simpleGetWithContentType() throws Exception {
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //

View File

@@ -0,0 +1,14 @@
{
"request" : {
"method" : "GET"
},
"response" : {
"status" : 200,
"body" : "Hello World",
"headers" : {
"ETag" : "\"356b53e9c9e124a7772d88f087a58838\"",
"Content-Type" : "application/json",
"Cache-Control" : "max-age=604800"
}
}
}