Skip to content

Commit 43faabd

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 a4a71a8 commit 43faabd

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;
@@ -440,7 +442,11 @@ public Object merge(Object parent) {
440442
if (parent == null) {
441443
return this;
442444
}
443-
if (parent instanceof RequestBuilder) {
445+
if (parent instanceof MockHttpServletRequestBuilder) {
446+
MockHttpServletRequestBuilder copiedParent = MockMvcRequestBuilders.get("/");
447+
copiedParent.merge(parent);
448+
this.parentBuilder = copiedParent;
449+
} else if (parent instanceof RequestBuilder) {
444450
this.parentBuilder = (RequestBuilder) parent;
445451
}
446452
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
@@ -894,6 +894,20 @@ public void mergeRequestAttribute() throws Exception {
894894
assertThat(mockMvc.perform(requestBuilder).andReturn().getRequest().getAttribute(attrName), equalTo(attrValue));
895895
}
896896

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

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

0 commit comments

Comments
 (0)