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()) {

View File

@ -28,6 +28,8 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.Cookie;
import org.junit.Test;
import org.springframework.util.StreamUtils;
@ -200,6 +202,26 @@ public class MockHttpServletRequestTests {
assertEquals(0, request.getParameterMap().size());
}
@Test
public void cookies() {
Cookie cookie1 = new Cookie("foo", "bar");
Cookie cookie2 = new Cookie("baz", "qux");
request.setCookies(cookie1, cookie2);
Cookie[] cookies = request.getCookies();
assertEquals(2, cookies.length);
assertEquals("foo", cookies[0].getName());
assertEquals("bar", cookies[0].getValue());
assertEquals("baz", cookies[1].getName());
assertEquals("qux", cookies[1].getValue());
}
@Test
public void noCookies() {
assertNull(request.getCookies());
}
@Test
public void defaultLocale() {
Locale originalDefaultLocale = Locale.getDefault();

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@ -29,6 +29,7 @@ import javax.servlet.http.Cookie;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
@ -42,21 +43,22 @@ import org.springframework.web.servlet.support.SessionFlashMapManager;
import static org.junit.Assert.*;
/**
* Tests building a MockHttpServletRequest with {@link MockHttpServletRequestBuilder}.
* Unit tests for building a {@link MockHttpServletRequest} with
* {@link MockHttpServletRequestBuilder}.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
*/
public class MockHttpServletRequestBuilderTests {
private MockHttpServletRequestBuilder builder;
private final ServletContext servletContext = new MockServletContext();
private ServletContext servletContext;
private MockHttpServletRequestBuilder builder;
@Before
public void setUp() {
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo/bar");
servletContext = new MockServletContext();
}
@Test
@ -354,6 +356,12 @@ public class MockHttpServletRequestBuilderTests {
assertEquals("qux", cookies[1].getValue());
}
@Test
public void noCookies() {
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
assertNull(request.getCookies());
}
@Test
public void locale() {
Locale locale = new Locale("nl", "nl");