Skip to content

Commit 5292f4a

Browse files
authored
Add tests proving we match on request bodies (wiremock#2367)
For methods that do not traditionally accept bodies like GET and DELETE
1 parent 6520dea commit 5292f4a

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

src/test/java/com/github/tomakehurst/wiremock/StubbingAcceptanceTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,44 @@ public void matchesFormParamWithKeyInArrayStyle() {
642642
assertThat(response.statusCode(), is(200));
643643
}
644644

645+
@Test
646+
public void matchesFormParamForGet() {
647+
stubFor(
648+
get(urlPathEqualTo("/form"))
649+
.withFormParam("key", equalTo("value"))
650+
.willReturn(aResponse().withStatus(200)));
651+
652+
WireMockResponse response =
653+
testClient.getWithBody(
654+
"/form",
655+
"key=value",
656+
"application/x-www-form-urlencoded",
657+
TestHttpHeader.withHeader("Content-Type", "application/x-www-form-urlencoded"));
658+
assertThat(response.statusCode(), is(200));
659+
660+
response = testClient.get("/form");
661+
assertThat(response.statusCode(), is(404));
662+
}
663+
664+
@Test
665+
public void matchesFormParamForDelete() {
666+
stubFor(
667+
delete(urlPathEqualTo("/form"))
668+
.withFormParam("key", equalTo("value"))
669+
.willReturn(aResponse().withStatus(200)));
670+
671+
WireMockResponse response =
672+
testClient.deleteWithBody(
673+
"/form",
674+
"key=value",
675+
"application/x-www-form-urlencoded",
676+
TestHttpHeader.withHeader("Content-Type", "application/x-www-form-urlencoded"));
677+
assertThat(response.statusCode(), is(200));
678+
679+
response = testClient.delete("/form");
680+
assertThat(response.statusCode(), is(404));
681+
}
682+
645683
@Test
646684
public void copesWithEmptyRequestHeaderValueWhenMatchingOnEqualTo() {
647685
stubFor(

src/test/java/com/github/tomakehurst/wiremock/testsupport/WireMockTestClient.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@
4747
import org.apache.hc.client5.http.protocol.HttpClientContext;
4848
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
4949
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder;
50+
import org.apache.hc.core5.http.ClassicHttpRequest;
5051
import org.apache.hc.core5.http.ClassicHttpResponse;
5152
import org.apache.hc.core5.http.ContentType;
5253
import org.apache.hc.core5.http.HttpEntity;
5354
import org.apache.hc.core5.http.HttpHost;
5455
import org.apache.hc.core5.http.config.CharCodingConfig;
5556
import org.apache.hc.core5.http.io.entity.InputStreamEntity;
5657
import org.apache.hc.core5.http.io.entity.StringEntity;
58+
import org.apache.hc.core5.http.io.support.ClassicRequestBuilder;
5759
import org.apache.hc.core5.ssl.SSLContexts;
5860

5961
public class WireMockTestClient {
@@ -107,6 +109,11 @@ public WireMockResponse get(String url, TestHttpHeader... headers) {
107109
return executeMethodAndConvertExceptions(httpRequest, headers);
108110
}
109111

112+
public WireMockResponse getWithBody(
113+
String url, String body, String contentType, TestHttpHeader... headers) {
114+
return requestWithBody("GET", url, body, contentType, headers);
115+
}
116+
110117
public WireMockResponse getViaProxy(String url) {
111118
return getViaProxy(url, port);
112119
}
@@ -171,7 +178,7 @@ public WireMockResponse patchWithBody(
171178
}
172179

173180
private WireMockResponse requestWithBody(
174-
HttpUriRequestBase request, String body, String contentType, TestHttpHeader... headers) {
181+
ClassicHttpRequest request, String body, String contentType, TestHttpHeader... headers) {
175182
request.setEntity(new StringEntity(body, ContentType.create(contentType, "utf-8")));
176183
return executeMethodAndConvertExceptions(request, headers);
177184
}
@@ -236,11 +243,23 @@ public WireMockResponse delete(String url) {
236243
return executeMethodAndConvertExceptions(httpDelete);
237244
}
238245

246+
public WireMockResponse deleteWithBody(
247+
String url, String body, String contentType, TestHttpHeader... headers) {
248+
return requestWithBody("DELETE", url, body, contentType, headers);
249+
}
250+
239251
public WireMockResponse options(String url, TestHttpHeader... headers) {
240252
HttpOptions httpOptions = new HttpOptions(mockServiceUrlFor(url));
241253
return executeMethodAndConvertExceptions(httpOptions, headers);
242254
}
243255

256+
private WireMockResponse requestWithBody(
257+
String method, String url, String body, String contentType, TestHttpHeader[] headers) {
258+
String actualUrl = URI.create(url).isAbsolute() ? url : mockServiceUrlFor(url);
259+
ClassicHttpRequest httpRequest = ClassicRequestBuilder.create(method).setUri(actualUrl).build();
260+
return requestWithBody(httpRequest, body, contentType, headers);
261+
}
262+
244263
public void addResponse(String responseSpecJson) {
245264
addResponse(responseSpecJson, "utf-8");
246265
}
@@ -318,7 +337,7 @@ private int postEmptyBodyAndReturnStatus(String url) {
318337
}
319338

320339
private WireMockResponse executeMethodAndConvertExceptions(
321-
HttpUriRequest httpRequest, TestHttpHeader... headers) {
340+
ClassicHttpRequest httpRequest, TestHttpHeader... headers) {
322341
try {
323342
for (TestHttpHeader header : headers) {
324343
httpRequest.addHeader(header.getName(), header.getValue());

0 commit comments

Comments
 (0)