Skip to content

Commit f5a5a81

Browse files
committed
Support response cookies in HtmlUnit integration
Issue: SPR-14051
1 parent 92dd4eb commit f5a5a81

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
* use this file except in compliance with the License. You may obtain a copy of
@@ -19,8 +19,11 @@
1919
import java.io.IOException;
2020
import java.util.ArrayList;
2121
import java.util.Collection;
22+
import java.util.Date;
2223
import java.util.List;
2324

25+
import javax.servlet.http.Cookie;
26+
2427
import com.gargoylesoftware.htmlunit.WebRequest;
2528
import com.gargoylesoftware.htmlunit.WebResponse;
2629
import com.gargoylesoftware.htmlunit.WebResponseData;
@@ -34,6 +37,7 @@
3437
/**
3538
* @author Rob Winch
3639
* @author Sam Brannen
40+
* @author Rossen Stoyanchev
3741
* @since 4.2
3842
*/
3943
final class MockWebResponseBuilder {
@@ -98,7 +102,20 @@ private List<NameValuePair> responseHeaders() {
98102
if (location != null) {
99103
responseHeaders.add(new NameValuePair("Location", location));
100104
}
105+
for (Cookie cookie : this.response.getCookies()) {
106+
responseHeaders.add(new NameValuePair("Set-Cookie", valueOfCookie(cookie)));
107+
}
101108
return responseHeaders;
102109
}
103110

111+
private String valueOfCookie(Cookie cookie) {
112+
Date expires = null;
113+
if (cookie.getMaxAge() > -1) {
114+
expires = new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000);
115+
}
116+
return new com.gargoylesoftware.htmlunit.util.Cookie(
117+
cookie.getDomain(), cookie.getName(), cookie.getValue(),
118+
cookie.getPath(), expires, cookie.getSecure(), cookie.isHttpOnly()).toString();
119+
}
120+
104121
}

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
* use this file except in compliance with the License. You may obtain a copy of
@@ -19,6 +19,8 @@
1919
import java.net.URL;
2020
import java.util.List;
2121

22+
import javax.servlet.http.Cookie;
23+
2224
import org.junit.Before;
2325
import org.junit.Test;
2426
import org.springframework.mock.web.MockHttpServletResponse;
@@ -27,7 +29,9 @@
2729
import com.gargoylesoftware.htmlunit.WebResponse;
2830
import com.gargoylesoftware.htmlunit.util.NameValuePair;
2931

32+
import static org.hamcrest.CoreMatchers.endsWith;
3033
import static org.hamcrest.CoreMatchers.equalTo;
34+
import static org.hamcrest.CoreMatchers.startsWith;
3135
import static org.junit.Assert.assertThat;
3236

3337
/**
@@ -94,16 +98,27 @@ public void buildContentType() throws Exception {
9498
public void buildResponseHeaders() throws Exception {
9599
this.response.addHeader("Content-Type", "text/html");
96100
this.response.addHeader("X-Test", "value");
101+
Cookie cookie = new Cookie("cookieA", "valueA");
102+
cookie.setDomain("domain");
103+
cookie.setPath("/path");
104+
cookie.setMaxAge(1800);
105+
cookie.setSecure(true);
106+
cookie.setHttpOnly(true);
107+
this.response.addCookie(cookie);
97108
WebResponse webResponse = this.responseBuilder.build();
98109

99110
List<NameValuePair> responseHeaders = webResponse.getResponseHeaders();
100-
assertThat(responseHeaders.size(), equalTo(2));
111+
assertThat(responseHeaders.size(), equalTo(3));
101112
NameValuePair header = responseHeaders.get(0);
102113
assertThat(header.getName(), equalTo("Content-Type"));
103114
assertThat(header.getValue(), equalTo("text/html"));
104115
header = responseHeaders.get(1);
105116
assertThat(header.getName(), equalTo("X-Test"));
106117
assertThat(header.getValue(), equalTo("value"));
118+
header = responseHeaders.get(2);
119+
assertThat(header.getName(), equalTo("Set-Cookie"));
120+
assertThat(header.getValue(), startsWith("cookieA=valueA;domain=domain;path=/path;expires="));
121+
assertThat(header.getValue(), endsWith(";secure;httpOnly"));
107122
}
108123

109124
@Test

0 commit comments

Comments
 (0)