Return null instead of empty cookies array in Spring MVC Test

Prior to this commit, MockHttpServletRequestBuilder always supplied an
array of cookies to the MockHttpServletRequest that it built, even if
the array was empty.

However, this violates the contract of HttpServletRequest. According to
the Servlet API, the getCookies() method "returns null if no cookies
were sent."

This commit ensures that MockHttpServletRequestBuilder no longer
configures an empty array of cookies in the mock request that it builds.

Issue: SPR-13314
This commit is contained in:
Sam Brannen
2015-08-07 01:00:12 +02:00
parent 35dd3078ef
commit 93c07e76bc
3 changed files with 42 additions and 7 deletions

View File

@@ -574,6 +574,7 @@ public class MockHttpServletRequestBuilder
}
request.setMethod(this.method.name());
for (String name : this.headers.keySet()) {
for (Object value : this.headers.get(name)) {
request.addHeader(name, value);
@@ -604,16 +605,20 @@ public class MockHttpServletRequestBuilder
request.setContentType(this.contentType);
request.setContent(this.content);
request.setCookies(this.cookies.toArray(new Cookie[this.cookies.size()]));
request.setCharacterEncoding(this.characterEncoding);
if (!ObjectUtils.isEmpty(this.cookies)) {
request.setCookies(this.cookies.toArray(new Cookie[this.cookies.size()]));
}
if (this.locale != null) {
request.addPreferredLocale(this.locale);
}
request.setCharacterEncoding(this.characterEncoding);
if (this.secure != null) {
request.setSecure(this.secure);
}
request.setUserPrincipal(this.principal);
for (String name : this.attributes.keySet()) {