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:
@@ -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()) {
|
||||
|
||||
Reference in New Issue
Block a user