diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilder.java index 5dc12a2118..94cc1ea865 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilder.java @@ -106,7 +106,7 @@ public class MockMvcWebClientBuilder extends MockMvcWebConnectionBuilderSupport< */ public MockMvcWebClientBuilder withDelegate(WebClient webClient) { Assert.notNull(webClient, "WebClient must not be null"); - webClient.setWebConnection(createConnection(webClient.getWebConnection())); + webClient.setWebConnection(createConnection(webClient)); this.webClient = webClient; return this; } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnection.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnection.java index d7a6fe6087..184d0b7ec7 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnection.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnection.java @@ -63,6 +63,38 @@ public final class MockMvcWebConnection implements WebConnection { private WebClient webClient; + /** + * Create a new instance that assumes the context path of the application + * is {@code ""} (i.e., the root context). + *
For example, the URL {@code http://localhost/test/this} would use + * {@code ""} as the context path. + * @param mockMvc the {@code MockMvc} instance to use; never {@code null} + * @param webClient the {@link WebClient} to use. never {@code null} + */ + public MockMvcWebConnection(MockMvc mockMvc, WebClient webClient) { + this(mockMvc, webClient, ""); + } + + /** + * Create a new instance with the specified context path. + *
The path may be {@code null} in which case the first path segment + * of the URL is turned into the contextPath. Otherwise it must conform + * to {@link javax.servlet.http.HttpServletRequest#getContextPath()} + * which states that it can be an empty string and otherwise must start + * with a "/" character and not end with a "/" character. + * @param mockMvc the {@code MockMvc} instance to use; never {@code null} + * @param webClient the {@link WebClient} to use. never {@code null} + * @param contextPath the contextPath to use + */ + public MockMvcWebConnection(MockMvc mockMvc, WebClient webClient, String contextPath) { + Assert.notNull(mockMvc, "MockMvc must not be null"); + Assert.notNull(webClient, "WebClient must not be null"); + validateContextPath(contextPath); + + this.webClient = webClient; + this.mockMvc = mockMvc; + this.contextPath = contextPath; + } /** * Create a new instance that assumes the context path of the application @@ -70,7 +102,9 @@ public final class MockMvcWebConnection implements WebConnection { *
For example, the URL {@code http://localhost/test/this} would use
* {@code ""} as the context path.
* @param mockMvc the {@code MockMvc} instance to use; never {@code null}
+ * @deprecated Use {@link #MockMvcWebConnection(MockMvc, WebClient)}
*/
+ @Deprecated
public MockMvcWebConnection(MockMvc mockMvc) {
this(mockMvc, "");
}
@@ -84,17 +118,13 @@ public final class MockMvcWebConnection implements WebConnection {
* with a "/" character and not end with a "/" character.
* @param mockMvc the {@code MockMvc} instance to use; never {@code null}
* @param contextPath the contextPath to use
+ * @deprecated use {@link #MockMvcWebConnection(MockMvc, WebClient, String)}
*/
+ @Deprecated
public MockMvcWebConnection(MockMvc mockMvc, String contextPath) {
- Assert.notNull(mockMvc, "MockMvc must not be null");
- validateContextPath(contextPath);
-
- this.webClient = new WebClient();
- this.mockMvc = mockMvc;
- this.contextPath = contextPath;
+ this(mockMvc, new WebClient(), contextPath);
}
-
public void setWebClient(WebClient webClient) {
Assert.notNull(webClient, "WebClient must not be null");
this.webClient = webClient;
diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionBuilderSupport.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionBuilderSupport.java
index 6575e6a26e..d60d2d4d1b 100644
--- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionBuilderSupport.java
+++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionBuilderSupport.java
@@ -19,6 +19,7 @@ package org.springframework.test.web.servlet.htmlunit;
import java.util.ArrayList;
import java.util.List;
+import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebConnection;
import org.springframework.test.web.servlet.MockMvc;
@@ -145,10 +146,46 @@ public abstract class MockMvcWebConnectionBuilderSupport