serveEventListeners = new ArrayList<>();
private Metadata metadata = new Metadata();
@@ -76,6 +84,10 @@ class BasicMappingBuilder implements ScenarioMappingBuilder {
this.requestPatternBuilder = new RequestPatternBuilder(customRequestMatcherName, parameters);
}
+ private static Parameters resolveParameters(P parameters) {
+ return parameters instanceof Parameters ? (Parameters) parameters : Parameters.of(parameters);
+ }
+
@Override
public BasicMappingBuilder willReturn(ResponseDefinitionBuilder responseDefBuilder) {
this.responseDefBuilder = responseDefBuilder;
@@ -112,6 +124,18 @@ public BasicMappingBuilder withHeader(String key, StringValuePattern headerPatte
return this;
}
+ @Override
+ public ScenarioMappingBuilder withHeader(String key, MultiValuePattern headerPattern) {
+ this.requestPatternBuilder.withHeader(key, headerPattern);
+ return this;
+ }
+
+ @Override
+ public MappingBuilder withPathParam(String name, StringValuePattern pattern) {
+ this.requestPatternBuilder.withPathParam(name, pattern);
+ return this;
+ }
+
@Override
public BasicMappingBuilder withCookie(String name, StringValuePattern cookieValuePattern) {
this.requestPatternBuilder.withCookie(name, cookieValuePattern);
@@ -124,6 +148,24 @@ public BasicMappingBuilder withQueryParam(String key, StringValuePattern queryPa
return this;
}
+ @Override
+ public ScenarioMappingBuilder withQueryParam(String key, MultiValuePattern queryParamPattern) {
+ this.requestPatternBuilder.withQueryParam(key, queryParamPattern);
+ return this;
+ }
+
+ @Override
+ public ScenarioMappingBuilder withFormParam(String key, StringValuePattern formParamPattern) {
+ this.requestPatternBuilder.withFormParam(key, formParamPattern);
+ return this;
+ }
+
+ @Override
+ public ScenarioMappingBuilder withFormParam(String key, MultiValuePattern formParamPattern) {
+ this.requestPatternBuilder.withFormParam(key, formParamPattern);
+ return this;
+ }
+
@Override
public ScenarioMappingBuilder withQueryParams(Map queryParams) {
for (Map.Entry entry : queryParams.entrySet()) {
@@ -199,6 +241,20 @@ public BasicMappingBuilder withPostServeAction(String extensionName, P param
return this;
}
+ @Override
+ public
MappingBuilder withServeEventListener(Set requestPhases, String extensionName,
+ P parameters) {
+ this.serveEventListeners
+ .add(new ServeEventListenerDefinition(extensionName, requestPhases, resolveParameters(parameters)));
+ return this;
+ }
+
+ @Override
+ public MappingBuilder withServeEventListener(String extensionName, P parameters) {
+ this.serveEventListeners.add(new ServeEventListenerDefinition(extensionName, resolveParameters(parameters)));
+ return this;
+ }
+
@Override
public ScenarioMappingBuilder withMetadata(Map map) {
this.metadata = new Metadata(map);
diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/ContractExchangeHandler.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/ContractExchangeHandler.java
index 3dd7a49937..2cfc149119 100644
--- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/ContractExchangeHandler.java
+++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/ContractExchangeHandler.java
@@ -18,7 +18,7 @@
import java.io.IOException;
import java.lang.reflect.Field;
-import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -26,6 +26,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
@@ -34,12 +35,12 @@
import com.github.tomakehurst.wiremock.http.Body;
import com.github.tomakehurst.wiremock.http.ContentTypeHeader;
import com.github.tomakehurst.wiremock.http.Cookie;
+import com.github.tomakehurst.wiremock.http.FormParameter;
import com.github.tomakehurst.wiremock.http.HttpHeader;
import com.github.tomakehurst.wiremock.http.QueryParameter;
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.http.RequestMethod;
import org.apache.commons.codec.binary.Base64;
-import wiremock.com.google.common.base.Optional;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@@ -56,7 +57,6 @@
/**
* @author Dave Syer
- *
*/
public class ContractExchangeHandler extends WireMockVerifyHelper, ContractExchangeHandler>
implements Consumer> {
@@ -115,7 +115,7 @@ class WireMockHttpRequestAdapter implements Request {
private static final boolean SERVLET_API_PRESENT = ClassUtils.isPresent("jakarta.servlet.http.Part", null);
- private EntityExchangeResult> result;
+ private final EntityExchangeResult> result;
WireMockHttpRequestAdapter(EntityExchangeResult> result) {
this.result = result;
@@ -227,6 +227,25 @@ else if (name.startsWith(key + "=")) {
return new QueryParameter(key, values);
}
+ @Override
+ public FormParameter formParameter(String key) {
+ for (String pair : this.getBodyAsString().split("&")) {
+ String[] split = pair.split("=");
+ return new FormParameter(split[0], List.of(split[1]));
+ }
+ return FormParameter.absent(key);
+ }
+
+ @Override
+ public Map formParameters() {
+ var value = new HashMap();
+ for (String pair : this.getBodyAsString().split("&")) {
+ String[] split = pair.split("=");
+ value.put(split[0], new FormParameter(split[0], List.of(split[1])));
+ }
+ return value;
+ }
+
@Override
public byte[] getBody() {
return this.result.getRequestBodyContent();
@@ -234,7 +253,7 @@ public byte[] getBody() {
@Override
public String getBodyAsString() {
- return new String(this.result.getRequestBodyContent(), Charset.forName("UTF-8"));
+ return new String(this.result.getRequestBodyContent(), StandardCharsets.UTF_8);
}
@Override
@@ -323,7 +342,7 @@ public boolean isBrowserProxyRequest() {
@Override
public Optional getOriginalRequest() {
- return Optional.absent();
+ return Optional.empty();
}
@Override
diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/ContractResultHandler.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/ContractResultHandler.java
index b8b5b5bfa1..fc56906b0a 100644
--- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/ContractResultHandler.java
+++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/ContractResultHandler.java
@@ -19,12 +19,14 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
@@ -32,12 +34,12 @@
import com.github.tomakehurst.wiremock.http.Body;
import com.github.tomakehurst.wiremock.http.ContentTypeHeader;
import com.github.tomakehurst.wiremock.http.Cookie;
+import com.github.tomakehurst.wiremock.http.FormParameter;
import com.github.tomakehurst.wiremock.http.HttpHeader;
import com.github.tomakehurst.wiremock.http.HttpHeaders;
import com.github.tomakehurst.wiremock.http.QueryParameter;
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.http.RequestMethod;
-import wiremock.com.google.common.base.Optional;
import wiremock.org.apache.commons.io.IOUtils;
import org.springframework.http.MediaType;
@@ -193,6 +195,18 @@ public QueryParameter queryParameter(String key) {
return new QueryParameter(key, Collections.singletonList(result.getRequest().getParameter(key)));
}
+ @Override
+ public FormParameter formParameter(String key) {
+ return new FormParameter(key, Arrays.stream(result.getRequest().getParameterValues(key)).toList());
+ }
+
+ @Override
+ public Map formParameters() {
+ return result.getRequest().getParameterMap().entrySet().stream()
+ .collect(Collectors.toMap(Map.Entry::getKey,
+ (value) -> new FormParameter(value.getKey(), Arrays.asList(value.getValue()))));
+ }
+
@Override
public byte[] getBody() {
return result.getRequest().getContentAsByteArray();
@@ -270,7 +284,7 @@ public boolean isBrowserProxyRequest() {
@Override
public Optional getOriginalRequest() {
- return Optional.absent();
+ return Optional.empty();
}
@Override
diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/WiremockServerRestDocsApplicationTests.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/WiremockServerRestDocsApplicationTests.java
index b27169e9b6..52c5af1e6c 100644
--- a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/WiremockServerRestDocsApplicationTests.java
+++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/WiremockServerRestDocsApplicationTests.java
@@ -20,7 +20,6 @@
import java.nio.file.Files;
import java.util.Map;
-import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.matching.MultiValuePattern;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import jakarta.servlet.http.HttpServletRequest;
@@ -85,10 +84,9 @@ public void queryParamsAreFetchedFromStubs() throws Exception {
BDDAssertions.then(file).exists();
StubMapping stubMapping = StubMapping.buildFrom(new String(Files.readAllBytes(file.toPath())));
Map queryParameters = stubMapping.getRequest().getQueryParameters();
- BDDAssertions.then(queryParameters.get("callback").getValuePattern())
- .isEqualTo(WireMock.equalTo("a_function_name"));
- BDDAssertions.then(queryParameters.get("foo").getValuePattern()).isEqualTo(WireMock.equalTo("foo"));
- BDDAssertions.then(queryParameters.get("bar").getValuePattern()).isEqualTo(WireMock.equalTo("bar"));
+ BDDAssertions.then(queryParameters.get("callback").getExpected()).isEqualTo("a_function_name");
+ BDDAssertions.then(queryParameters.get("foo").getExpected()).isEqualTo("foo");
+ BDDAssertions.then(queryParameters.get("bar").getExpected()).isEqualTo("bar");
}
@Test