Skip to content

Commit 93c07e7

Browse files
committed
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
1 parent 35dd307 commit 93c07e7

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ public final MockHttpServletRequest buildRequest(ServletContext servletContext)
574574
}
575575

576576
request.setMethod(this.method.name());
577+
577578
for (String name : this.headers.keySet()) {
578579
for (Object value : this.headers.get(name)) {
579580
request.addHeader(name, value);
@@ -604,16 +605,20 @@ public final MockHttpServletRequest buildRequest(ServletContext servletContext)
604605

605606
request.setContentType(this.contentType);
606607
request.setContent(this.content);
607-
request.setCookies(this.cookies.toArray(new Cookie[this.cookies.size()]));
608+
request.setCharacterEncoding(this.characterEncoding);
609+
610+
if (!ObjectUtils.isEmpty(this.cookies)) {
611+
request.setCookies(this.cookies.toArray(new Cookie[this.cookies.size()]));
612+
}
608613

609614
if (this.locale != null) {
610615
request.addPreferredLocale(this.locale);
611616
}
612-
request.setCharacterEncoding(this.characterEncoding);
613617

614618
if (this.secure != null) {
615619
request.setSecure(this.secure);
616620
}
621+
617622
request.setUserPrincipal(this.principal);
618623

619624
for (String name : this.attributes.keySet()) {

spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import java.util.Locale;
2929
import java.util.Map;
3030

31+
import javax.servlet.http.Cookie;
32+
3133
import org.junit.Test;
3234

3335
import org.springframework.util.StreamUtils;
@@ -200,6 +202,26 @@ public void removeAllParameters() {
200202
assertEquals(0, request.getParameterMap().size());
201203
}
202204

205+
@Test
206+
public void cookies() {
207+
Cookie cookie1 = new Cookie("foo", "bar");
208+
Cookie cookie2 = new Cookie("baz", "qux");
209+
request.setCookies(cookie1, cookie2);
210+
211+
Cookie[] cookies = request.getCookies();
212+
213+
assertEquals(2, cookies.length);
214+
assertEquals("foo", cookies[0].getName());
215+
assertEquals("bar", cookies[0].getValue());
216+
assertEquals("baz", cookies[1].getName());
217+
assertEquals("qux", cookies[1].getValue());
218+
}
219+
220+
@Test
221+
public void noCookies() {
222+
assertNull(request.getCookies());
223+
}
224+
203225
@Test
204226
public void defaultLocale() {
205227
Locale originalDefaultLocale = Locale.getDefault();

spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@
2929

3030
import org.junit.Before;
3131
import org.junit.Test;
32+
3233
import org.springframework.http.HttpHeaders;
3334
import org.springframework.http.HttpMethod;
3435
import org.springframework.http.MediaType;
@@ -42,21 +43,22 @@
4243
import static org.junit.Assert.*;
4344

4445
/**
45-
* Tests building a MockHttpServletRequest with {@link MockHttpServletRequestBuilder}.
46+
* Unit tests for building a {@link MockHttpServletRequest} with
47+
* {@link MockHttpServletRequestBuilder}.
4648
*
4749
* @author Rossen Stoyanchev
50+
* @author Sam Brannen
4851
*/
4952
public class MockHttpServletRequestBuilderTests {
5053

51-
private MockHttpServletRequestBuilder builder;
54+
private final ServletContext servletContext = new MockServletContext();
5255

53-
private ServletContext servletContext;
56+
private MockHttpServletRequestBuilder builder;
5457

5558

5659
@Before
5760
public void setUp() {
5861
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo/bar");
59-
servletContext = new MockServletContext();
6062
}
6163

6264
@Test
@@ -354,6 +356,12 @@ public void cookie() {
354356
assertEquals("qux", cookies[1].getValue());
355357
}
356358

359+
@Test
360+
public void noCookies() {
361+
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
362+
assertNull(request.getCookies());
363+
}
364+
357365
@Test
358366
public void locale() {
359367
Locale locale = new Locale("nl", "nl");

0 commit comments

Comments
 (0)