Browse Source

Merge branch '5.1.x'

pull/23098/head
Sam Brannen 6 years ago
parent
commit
61bf45c86f
  1. 19
      spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java
  2. 20
      spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java
  3. 19
      spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java

19
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.Map;
import java.util.Set; import java.util.Set;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.stream.Collectors;
import javax.servlet.AsyncContext; import javax.servlet.AsyncContext;
import javax.servlet.DispatcherType; import javax.servlet.DispatcherType;
import javax.servlet.RequestDispatcher; import javax.servlet.RequestDispatcher;
@ -58,6 +59,7 @@ import javax.servlet.http.Part;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.LinkedCaseInsensitiveMap; import org.springframework.util.LinkedCaseInsensitiveMap;
@ -973,14 +975,20 @@ public class MockHttpServletRequest implements HttpServletRequest {
public void setCookies(@Nullable Cookie... cookies) { public void setCookies(@Nullable Cookie... cookies) {
this.cookies = (ObjectUtils.isEmpty(cookies) ? null : cookies); this.cookies = (ObjectUtils.isEmpty(cookies) ? null : cookies);
this.headers.remove(HttpHeaders.COOKIE); if (this.cookies == null) {
if (this.cookies != null) { removeHeader(HttpHeaders.COOKIE);
Arrays.stream(this.cookies) }
.map(c -> c.getName() + '=' + (c.getValue() == null ? "" : c.getValue())) else {
.forEach(value -> doAddHeaderValue(HttpHeaders.COOKIE, value, false)); 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 @Override
@Nullable @Nullable
public Cookie[] getCookies() { public Cookie[] getCookies() {
@ -1297,6 +1305,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
* Otherwise it simply returns the current session id. * Otherwise it simply returns the current session id.
* @since 4.0.3 * @since 4.0.3
*/ */
@Override
public String changeSessionId() { public String changeSessionId() {
Assert.isTrue(this.session != null, "The request does not have a session"); Assert.isTrue(this.session != null, "The request does not have a session");
if (this.session instanceof MockHttpSession) { if (this.session instanceof MockHttpSession) {

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

@ -282,12 +282,20 @@ public class MockHttpServletRequestTests {
Cookie[] cookies = request.getCookies(); Cookie[] cookies = request.getCookies();
List<String> cookieHeaders = Collections.list(request.getHeaders("Cookie")); List<String> cookieHeaders = Collections.list(request.getHeaders("Cookie"));
assertThat(cookies.length).isEqualTo(2); assertThat(cookies)
assertThat(cookies[0].getName()).isEqualTo("foo"); .describedAs("Raw cookies stored as is")
assertThat(cookies[0].getValue()).isEqualTo("bar"); .hasSize(2)
assertThat(cookies[1].getName()).isEqualTo("baz"); .satisfies(subject -> {
assertThat(cookies[1].getValue()).isEqualTo("qux"); assertThat(subject[0].getName()).isEqualTo("foo");
assertThat(cookieHeaders).isEqualTo(Arrays.asList("foo=bar", "baz=qux")); 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 @Test

19
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.Map;
import java.util.Set; import java.util.Set;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.stream.Collectors;
import javax.servlet.AsyncContext; import javax.servlet.AsyncContext;
import javax.servlet.DispatcherType; import javax.servlet.DispatcherType;
import javax.servlet.RequestDispatcher; import javax.servlet.RequestDispatcher;
@ -58,6 +59,7 @@ import javax.servlet.http.Part;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.LinkedCaseInsensitiveMap; import org.springframework.util.LinkedCaseInsensitiveMap;
@ -973,14 +975,20 @@ public class MockHttpServletRequest implements HttpServletRequest {
public void setCookies(@Nullable Cookie... cookies) { public void setCookies(@Nullable Cookie... cookies) {
this.cookies = (ObjectUtils.isEmpty(cookies) ? null : cookies); this.cookies = (ObjectUtils.isEmpty(cookies) ? null : cookies);
this.headers.remove(HttpHeaders.COOKIE); if (this.cookies == null) {
if (this.cookies != null) { removeHeader(HttpHeaders.COOKIE);
Arrays.stream(this.cookies) }
.map(c -> c.getName() + '=' + (c.getValue() == null ? "" : c.getValue())) else {
.forEach(value -> doAddHeaderValue(HttpHeaders.COOKIE, value, false)); 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 @Override
@Nullable @Nullable
public Cookie[] getCookies() { public Cookie[] getCookies() {
@ -1297,6 +1305,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
* Otherwise it simply returns the current session id. * Otherwise it simply returns the current session id.
* @since 4.0.3 * @since 4.0.3
*/ */
@Override
public String changeSessionId() { public String changeSessionId() {
Assert.isTrue(this.session != null, "The request does not have a session"); Assert.isTrue(this.session != null, "The request does not have a session");
if (this.session instanceof MockHttpSession) { if (this.session instanceof MockHttpSession) {

Loading…
Cancel
Save