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
This commit is contained in:
Rob Winch 2016-04-25 16:35:56 -05:00
parent b2c9c8abcf
commit 9ec0873604
2 changed files with 25 additions and 3 deletions

View File

@ -29,6 +29,7 @@ import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.WebResponseData;
import com.gargoylesoftware.htmlunit.util.NameValuePair;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.springframework.http.HttpStatus;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.util.Assert;
@ -115,9 +116,16 @@ final class MockWebResponseBuilder {
if (cookie.getMaxAge() > -1) {
expires = new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000);
}
return new com.gargoylesoftware.htmlunit.util.Cookie(
cookie.getDomain(), cookie.getName(), cookie.getValue(),
cookie.getPath(), expires, cookie.getSecure(), cookie.isHttpOnly()).toString();
BasicClientCookie result = new BasicClientCookie(cookie.getName(), cookie.getValue());
result.setDomain(cookie.getDomain());
result.setComment(cookie.getComment());
result.setExpiryDate(expires);
result.setPath(cookie.getPath());
result.setSecure(cookie.getSecure());
if(cookie.isHttpOnly()) {
result.setAttribute("httponly", "true");
}
return new com.gargoylesoftware.htmlunit.util.Cookie(result).toString();
}
}

View File

@ -119,6 +119,20 @@ public class MockWebResponseBuilderTests {
assertThat(header.getValue(), endsWith(";secure;httpOnly"));
}
// SPR-14169
@Test
public void buildResponseHeadersNullDomainDefaulted() throws Exception {
Cookie cookie = new Cookie("cookieA", "valueA");
this.response.addCookie(cookie);
WebResponse webResponse = this.responseBuilder.build();
List<NameValuePair> responseHeaders = webResponse.getResponseHeaders();
assertThat(responseHeaders.size(), equalTo(1));
NameValuePair header = responseHeaders.get(0);
assertThat(header.getName(), equalTo("Set-Cookie"));
assertThat(header.getValue(), equalTo("cookieA=valueA"));
}
@Test
public void buildStatus() throws Exception {
WebResponse webResponse = this.responseBuilder.build();