Support response cookies in HtmlUnit integration

Issue: SPR-14051
This commit is contained in:
Rossen Stoyanchev
2016-03-16 15:26:06 -04:00
parent 92dd4eb3ef
commit f5a5a81e95
2 changed files with 35 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
@@ -19,8 +19,11 @@ package org.springframework.test.web.servlet.htmlunit;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.servlet.http.Cookie;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.WebResponseData;
@@ -34,6 +37,7 @@ import org.springframework.util.StringUtils;
/**
* @author Rob Winch
* @author Sam Brannen
* @author Rossen Stoyanchev
* @since 4.2
*/
final class MockWebResponseBuilder {
@@ -98,7 +102,20 @@ final class MockWebResponseBuilder {
if (location != null) {
responseHeaders.add(new NameValuePair("Location", location));
}
for (Cookie cookie : this.response.getCookies()) {
responseHeaders.add(new NameValuePair("Set-Cookie", valueOfCookie(cookie)));
}
return responseHeaders;
}
private String valueOfCookie(Cookie cookie) {
Date expires = null;
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();
}
}