From 93c07e76bc2d20b495314792938361f561be25fb Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 7 Aug 2015 01:00:12 +0200 Subject: [PATCH] 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 --- .../MockHttpServletRequestBuilder.java | 9 ++++++-- .../mock/web/MockHttpServletRequestTests.java | 22 +++++++++++++++++++ .../MockHttpServletRequestBuilderTests.java | 18 ++++++++++----- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java index ba8f678dc21..2d25a246e86 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java @@ -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()) { diff --git a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java index 322634e205f..5b5643183e7 100644 --- a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java +++ b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java @@ -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(); diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java index 8cc1ae54ae2..e6070809849 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java @@ -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");