MockMvcWebConnection stores cookies from response

Previously MockMvcWebConnection did not update the cookie manager with the
cookies from MockHttpServletResponse. This meant that newly added cookies
are not saved to the cookie manager and thus are not presented in the next
request.

This commit ensures that MockMvcWebConnection stores the response cookies
in the cookie manager.

Issue: SPR-14265
This commit is contained in:
Rob Winch
2016-05-11 00:15:53 -05:00
committed by Rossen Stoyanchev
parent 92f1b69e8c
commit 54f84cbd97
3 changed files with 77 additions and 6 deletions

View File

@@ -17,13 +17,16 @@
package org.springframework.test.web.servlet.htmlunit;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.gargoylesoftware.htmlunit.CookieManager;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebConnection;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.util.Cookie;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockHttpSession;
@@ -142,10 +145,32 @@ public final class MockMvcWebConnection implements WebConnection {
httpServletResponse = getResponse(requestBuilder);
forwardedUrl = httpServletResponse.getForwardedUrl();
}
storeCookies(webRequest, httpServletResponse.getCookies());
return new MockWebResponseBuilder(startTime, webRequest, httpServletResponse).build();
}
private void storeCookies(WebRequest webRequest, javax.servlet.http.Cookie[] cookies) {
if (cookies == null) {
return;
}
Date now = new Date();
CookieManager cookieManager = webClient.getCookieManager();
for (javax.servlet.http.Cookie cookie : cookies) {
if (cookie.getDomain() == null) {
cookie.setDomain(webRequest.getUrl().getHost());
}
Cookie toManage = MockWebResponseBuilder.createCookie(cookie);
Date expires = toManage.getExpires();
if (expires == null || expires.after(now)) {
cookieManager.addCookie(toManage);
}
else {
cookieManager.removeCookie(toManage);
}
}
}
private MockHttpServletResponse getResponse(RequestBuilder requestBuilder) throws IOException {
ResultActions resultActions;
try {
@@ -182,5 +207,4 @@ public final class MockMvcWebConnection implements WebConnection {
throw new IllegalArgumentException("contextPath '" + contextPath + "' must not end with '/'.");
}
}
}

View File

@@ -112,6 +112,10 @@ final class MockWebResponseBuilder {
}
private String valueOfCookie(Cookie cookie) {
return createCookie(cookie).toString();
}
static com.gargoylesoftware.htmlunit.util.Cookie createCookie(Cookie cookie) {
Date expires = null;
if (cookie.getMaxAge() > -1) {
expires = new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000);
@@ -125,7 +129,6 @@ final class MockWebResponseBuilder {
if(cookie.isHttpOnly()) {
result.setAttribute("httponly", "true");
}
return new com.gargoylesoftware.htmlunit.util.Cookie(result).toString();
return new com.gargoylesoftware.htmlunit.util.Cookie(result);
}
}