Skip to content

Commit 9ec0873

Browse files
author
Rob Winch
committed
MockWebResponseBuilder defaults cookie domain
Previously MockWebResponseBuilder would use the cookie domain of the cookie received from MockMvc response. This caused problems because the cookie domain can be null, but HtmlUnit forbids a null domain. This commit defaults the domain of the cookie to the domain of the WebRequest. Issues SPR-14169
1 parent b2c9c8a commit 9ec0873

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.gargoylesoftware.htmlunit.WebResponseData;
3030
import com.gargoylesoftware.htmlunit.util.NameValuePair;
3131

32+
import org.apache.http.impl.cookie.BasicClientCookie;
3233
import org.springframework.http.HttpStatus;
3334
import org.springframework.mock.web.MockHttpServletResponse;
3435
import org.springframework.util.Assert;
@@ -115,9 +116,16 @@ private String valueOfCookie(Cookie cookie) {
115116
if (cookie.getMaxAge() > -1) {
116117
expires = new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000);
117118
}
118-
return new com.gargoylesoftware.htmlunit.util.Cookie(
119-
cookie.getDomain(), cookie.getName(), cookie.getValue(),
120-
cookie.getPath(), expires, cookie.getSecure(), cookie.isHttpOnly()).toString();
119+
BasicClientCookie result = new BasicClientCookie(cookie.getName(), cookie.getValue());
120+
result.setDomain(cookie.getDomain());
121+
result.setComment(cookie.getComment());
122+
result.setExpiryDate(expires);
123+
result.setPath(cookie.getPath());
124+
result.setSecure(cookie.getSecure());
125+
if(cookie.isHttpOnly()) {
126+
result.setAttribute("httponly", "true");
127+
}
128+
return new com.gargoylesoftware.htmlunit.util.Cookie(result).toString();
121129
}
122130

123131
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,20 @@ public void buildResponseHeaders() throws Exception {
119119
assertThat(header.getValue(), endsWith(";secure;httpOnly"));
120120
}
121121

122+
// SPR-14169
123+
@Test
124+
public void buildResponseHeadersNullDomainDefaulted() throws Exception {
125+
Cookie cookie = new Cookie("cookieA", "valueA");
126+
this.response.addCookie(cookie);
127+
WebResponse webResponse = this.responseBuilder.build();
128+
129+
List<NameValuePair> responseHeaders = webResponse.getResponseHeaders();
130+
assertThat(responseHeaders.size(), equalTo(1));
131+
NameValuePair header = responseHeaders.get(0);
132+
assertThat(header.getName(), equalTo("Set-Cookie"));
133+
assertThat(header.getValue(), equalTo("cookieA=valueA"));
134+
}
135+
122136
@Test
123137
public void buildStatus() throws Exception {
124138
WebResponse webResponse = this.responseBuilder.build();

0 commit comments

Comments
 (0)