Add all headers if they are matched with "equalTo"
Still TODO: other matcher types (regex etc.) See gh-105
This commit is contained in:
@@ -42,6 +42,7 @@ 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.header;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
|
||||
|
||||
@@ -150,14 +151,15 @@ public class WireMockRestServiceServer {
|
||||
try {
|
||||
for (Resource resource : this.resolver.getResources(pattern(location))) {
|
||||
StubMapping mapping = mapping(resource);
|
||||
ResponseActions expect = server.expect(
|
||||
requestTo(request(mapping.getRequest())));
|
||||
ResponseActions expect = server
|
||||
.expect(requestTo(request(mapping.getRequest())));
|
||||
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;
|
||||
@@ -178,13 +180,14 @@ public class WireMockRestServiceServer {
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -192,6 +195,9 @@ public class WireMockRestServiceServer {
|
||||
for (final String header : request.getHeaders().keySet()) {
|
||||
final MultiValuePattern pattern = request.getHeaders().get(header);
|
||||
// TODO: match the headers
|
||||
if ("equalTo".equals(pattern.getName())) {
|
||||
expect.andExpect(header(header, pattern.getExpected()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package org.springframework.cloud.contract.wiremock;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.test.web.client.MockRestServiceServer;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class WiremockMockServerApplicationTests {
|
||||
@@ -17,8 +19,8 @@ public class WiremockMockServerApplicationTests {
|
||||
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
|
||||
.baseUrl("http://example.org") //
|
||||
.stubs("classpath:/mappings/resource.json").build();
|
||||
assertThat(this.restTemplate.getForObject("http://example.org/resource", String.class))
|
||||
.isEqualTo("Hello World");
|
||||
assertThat(this.restTemplate.getForObject("http://example.org/resource",
|
||||
String.class)).isEqualTo("Hello World");
|
||||
server.verify();
|
||||
}
|
||||
|
||||
@@ -37,8 +39,8 @@ public class WiremockMockServerApplicationTests {
|
||||
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");
|
||||
assertThat(this.restTemplate.getForObject("http://example.org/resource",
|
||||
String.class)).isEqualTo("Hello World");
|
||||
server.verify();
|
||||
}
|
||||
|
||||
@@ -47,8 +49,8 @@ public class WiremockMockServerApplicationTests {
|
||||
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
|
||||
.baseUrl("http://example.org") //
|
||||
.stubs("classpath:/mappings/resource-without-content-type.json").build();
|
||||
assertThat(this.restTemplate.getForObject("http://example.org/resource", String.class))
|
||||
.isEqualTo("Hello World");
|
||||
assertThat(this.restTemplate.getForObject("http://example.org/resource",
|
||||
String.class)).isEqualTo("Hello World");
|
||||
server.verify();
|
||||
}
|
||||
|
||||
@@ -57,8 +59,8 @@ public class WiremockMockServerApplicationTests {
|
||||
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
|
||||
.baseUrl("http://example.org") //
|
||||
.stubs("classpath:/mappings/poster.json").build();
|
||||
assertThat(this.restTemplate.postForObject("http://example.org/poster", "greeting",
|
||||
String.class)).isEqualTo("Hello World");
|
||||
assertThat(this.restTemplate.postForObject("http://example.org/poster",
|
||||
"greeting", String.class)).isEqualTo("Hello World");
|
||||
server.verify();
|
||||
}
|
||||
|
||||
@@ -67,8 +69,10 @@ public class WiremockMockServerApplicationTests {
|
||||
WireMockRestServiceServer.with(this.restTemplate) //
|
||||
.baseUrl("http://example.org") //
|
||||
.stubs("classpath:/mappings/*.json").ignoreExpectOrder(true).build();
|
||||
assertThat(this.restTemplate.exchange("http://example.org/poster", HttpMethod.POST,
|
||||
RequestEntity.EMPTY, String.class).getBody()).isEqualTo("Accepted World");
|
||||
assertThat(this.restTemplate
|
||||
.exchange(RequestEntity.post(new URI("http://example.org/poster"))
|
||||
.accept(MediaType.TEXT_PLAIN).build(), String.class)
|
||||
.getBody()).isEqualTo("Accepted World");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -76,8 +80,8 @@ public class WiremockMockServerApplicationTests {
|
||||
WireMockRestServiceServer.with(this.restTemplate) //
|
||||
.baseUrl("http://example.org") //
|
||||
.stubs("classpath:/mappings").ignoreExpectOrder(true).build();
|
||||
assertThat(this.restTemplate.getForObject("http://example.org/resource", String.class))
|
||||
.isEqualTo("Hello World");
|
||||
assertThat(this.restTemplate.getForObject("http://example.org/resource",
|
||||
String.class)).isEqualTo("Hello World");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -85,8 +89,8 @@ public class WiremockMockServerApplicationTests {
|
||||
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");
|
||||
assertThat(this.restTemplate.getForObject("http://example.org/resource",
|
||||
String.class)).isEqualTo("Hello World");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user