Added missing method verification

without this change the only criterion for matching of requests was URL / headers / content. The method check was missing
with this change we verify if the HTTP method matches too

fixes #163
This commit is contained in:
Marcin Grzejszczak
2016-12-29 14:10:13 +01:00
parent cb803320cd
commit e4d8e1f6e2
2 changed files with 31 additions and 6 deletions

View File

@@ -16,10 +16,6 @@
package org.springframework.cloud.contract.wiremock;
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;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
@@ -33,6 +29,7 @@ import org.hamcrest.Description;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.web.client.MockRestServiceServer;
@@ -51,6 +48,11 @@ 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.method;
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
@@ -189,6 +191,7 @@ public class WireMockRestServiceServer {
}
for (StubMapping mapping : mappings) {
ResponseActions expect = server.expect(requestTo(request(mapping.getRequest())));
expect.andExpect(method(HttpMethod.valueOf(mapping.getRequest().getMethod().getName())));
requestHeaders(expect, mapping.getRequest());
expect.andRespond(response(mapping.getResponse()));
}

View File

@@ -1,14 +1,16 @@
package org.springframework.cloud.contract.wiremock;
import java.net.URI;
import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
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;
import static org.assertj.core.api.Assertions.fail;
public class WiremockMockServerApplicationTests {
@@ -24,6 +26,26 @@ public class WiremockMockServerApplicationTests {
server.verify();
}
@Test
public void simplePutShouldFail() throws Exception {
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
.baseUrl("http://example.org") //
.stubs("classpath:/mappings/resource.json")
.build();
RequestEntity<Void> postRequest = RequestEntity.post(URI.create("http://example.org/resource"))
.accept(MediaType.TEXT_PLAIN)
.build();
ResponseEntity<String> response;
try {
response = this.restTemplate.exchange(postRequest, String.class);
} catch (AssertionError e) {
response = null;
}
if (null != response) {
fail("There was a response for POST request: " + postRequest + "\n\tresponse: " + response);
}
}
@Test
public void simpleGetWithBodyFile() throws Exception {
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //