Skip to content

Commit 966baea

Browse files
author
Rob Winch
committed
Fix HtmlUnitRequestBuilder merge
Previously invoking HtmlUnitRequestBuilder merge caused the pathInfo of the parent to be corrupted. This could additional invocations with the same parent. This fix ensures that the parent is no longer directly used. Instead, we create a copy of the parent by merging the parent that was passed in with the copy. Fixes SPR-14584
1 parent 1ac8e1c commit 966baea

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
import org.springframework.mock.web.MockHttpSession;
4646
import org.springframework.test.web.servlet.RequestBuilder;
4747
import org.springframework.test.web.servlet.SmartRequestBuilder;
48+
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
49+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
4850
import org.springframework.test.web.servlet.request.RequestPostProcessor;
4951
import org.springframework.util.Assert;
5052
import org.springframework.util.ObjectUtils;
@@ -435,7 +437,11 @@ public Object merge(Object parent) {
435437
if (parent == null) {
436438
return this;
437439
}
438-
if (parent instanceof RequestBuilder) {
440+
if (parent instanceof MockHttpServletRequestBuilder) {
441+
MockHttpServletRequestBuilder copiedParent = MockMvcRequestBuilders.get("/");
442+
copiedParent.merge(parent);
443+
this.parentBuilder = copiedParent;
444+
} else if (parent instanceof RequestBuilder) {
439445
this.parentBuilder = (RequestBuilder) parent;
440446
}
441447
if (parent instanceof SmartRequestBuilder) {

spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,20 @@ public void mergeRequestAttribute() throws Exception {
895895
assertThat(mockMvc.perform(requestBuilder).andReturn().getRequest().getAttribute(attrName), equalTo(attrValue));
896896
}
897897

898+
@Test // SPR-14584
899+
public void mergeDoesNotCorruptPathInfoOnParent() throws Exception {
900+
String pathInfo = "/foo/bar";
901+
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new HelloController())
902+
.defaultRequest(get("/"))
903+
.build();
904+
905+
assertThat(mockMvc.perform(get(pathInfo)).andReturn().getRequest().getPathInfo(), equalTo(pathInfo));
906+
907+
mockMvc.perform(requestBuilder);
908+
909+
assertThat(mockMvc.perform(get(pathInfo)).andReturn().getRequest().getPathInfo(), equalTo(pathInfo));
910+
}
911+
898912

899913
private void assertSingleSessionCookie(String expected) {
900914
com.gargoylesoftware.htmlunit.util.Cookie jsessionidCookie = webClient.getCookieManager().getCookie("JSESSIONID");

0 commit comments

Comments
 (0)