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; @@ -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; @@ -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 { @@ -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 { @@ -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) {

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

@ -282,12 +282,20 @@ public class MockHttpServletRequestTests { @@ -282,12 +282,20 @@ public class MockHttpServletRequestTests {
Cookie[] cookies = request.getCookies();
List<String> 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

19
spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java

@ -41,6 +41,7 @@ import java.util.Locale; @@ -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; @@ -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 { @@ -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 { @@ -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) {

Loading…
Cancel
Save