diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java index 94e3a8f6cf..3207576c5b 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java @@ -41,6 +41,7 @@ import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.TimeZone; +import java.util.stream.Collectors; import javax.servlet.AsyncContext; import javax.servlet.DispatcherType; import javax.servlet.RequestDispatcher; @@ -58,6 +59,7 @@ import javax.servlet.http.Part; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; +import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.LinkedCaseInsensitiveMap; @@ -973,14 +975,20 @@ public class MockHttpServletRequest implements HttpServletRequest { public void setCookies(@Nullable Cookie... cookies) { this.cookies = (ObjectUtils.isEmpty(cookies) ? null : cookies); - this.headers.remove(HttpHeaders.COOKIE); - if (this.cookies != null) { - Arrays.stream(this.cookies) - .map(c -> c.getName() + '=' + (c.getValue() == null ? "" : c.getValue())) - .forEach(value -> doAddHeaderValue(HttpHeaders.COOKIE, value, false)); + if (this.cookies == null) { + removeHeader(HttpHeaders.COOKIE); + } + else { + doAddHeaderValue(HttpHeaders.COOKIE, encodeCookies(this.cookies), true); } } + private static String encodeCookies(@NonNull Cookie... cookies) { + return Arrays.stream(cookies) + .map(c -> c.getName() + '=' + (c.getValue() == null ? "" : c.getValue())) + .collect(Collectors.joining("; ")); + } + @Override @Nullable public Cookie[] getCookies() { @@ -1297,6 +1305,7 @@ public class MockHttpServletRequest implements HttpServletRequest { * Otherwise it simply returns the current session id. * @since 4.0.3 */ + @Override public String changeSessionId() { Assert.isTrue(this.session != null, "The request does not have a session"); if (this.session instanceof MockHttpSession) { 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 2cb9c63d91..bebec91f2b 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 @@ -282,12 +282,20 @@ public class MockHttpServletRequestTests { Cookie[] cookies = request.getCookies(); List cookieHeaders = Collections.list(request.getHeaders("Cookie")); - assertThat(cookies.length).isEqualTo(2); - assertThat(cookies[0].getName()).isEqualTo("foo"); - assertThat(cookies[0].getValue()).isEqualTo("bar"); - assertThat(cookies[1].getName()).isEqualTo("baz"); - assertThat(cookies[1].getValue()).isEqualTo("qux"); - assertThat(cookieHeaders).isEqualTo(Arrays.asList("foo=bar", "baz=qux")); + assertThat(cookies) + .describedAs("Raw cookies stored as is") + .hasSize(2) + .satisfies(subject -> { + assertThat(subject[0].getName()).isEqualTo("foo"); + assertThat(subject[0].getValue()).isEqualTo("bar"); + assertThat(subject[1].getName()).isEqualTo("baz"); + assertThat(subject[1].getValue()).isEqualTo("qux"); + }); + + assertThat(cookieHeaders) + .describedAs("Cookies -> Header conversion works as expected per RFC6265") + .hasSize(1) + .hasOnlyOneElementSatisfying(header -> assertThat(header).isEqualTo("foo=bar; baz=qux")); } @Test diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java index fbfa8721f0..f2fb831b5c 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java @@ -41,6 +41,7 @@ import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.TimeZone; +import java.util.stream.Collectors; import javax.servlet.AsyncContext; import javax.servlet.DispatcherType; import javax.servlet.RequestDispatcher; @@ -58,6 +59,7 @@ import javax.servlet.http.Part; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; +import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.LinkedCaseInsensitiveMap; @@ -973,14 +975,20 @@ public class MockHttpServletRequest implements HttpServletRequest { public void setCookies(@Nullable Cookie... cookies) { this.cookies = (ObjectUtils.isEmpty(cookies) ? null : cookies); - this.headers.remove(HttpHeaders.COOKIE); - if (this.cookies != null) { - Arrays.stream(this.cookies) - .map(c -> c.getName() + '=' + (c.getValue() == null ? "" : c.getValue())) - .forEach(value -> doAddHeaderValue(HttpHeaders.COOKIE, value, false)); + if (this.cookies == null) { + removeHeader(HttpHeaders.COOKIE); + } + else { + doAddHeaderValue(HttpHeaders.COOKIE, encodeCookies(this.cookies), true); } } + private static String encodeCookies(@NonNull Cookie... cookies) { + return Arrays.stream(cookies) + .map(c -> c.getName() + '=' + (c.getValue() == null ? "" : c.getValue())) + .collect(Collectors.joining("; ")); + } + @Override @Nullable public Cookie[] getCookies() { @@ -1297,6 +1305,7 @@ public class MockHttpServletRequest implements HttpServletRequest { * Otherwise it simply returns the current session id. * @since 4.0.3 */ + @Override public String changeSessionId() { Assert.isTrue(this.session != null, "The request does not have a session"); if (this.session instanceof MockHttpSession) {