Merge branch '5.1.x'

This commit is contained in:
Rossen Stoyanchev
2019-09-25 17:28:24 +01:00
7 changed files with 154 additions and 63 deletions

View File

@@ -16,11 +16,12 @@
package org.springframework.http;
import java.time.Duration;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* Unit tests for {@link ResponseCookie}.
@@ -29,37 +30,40 @@ import static org.assertj.core.api.Assertions.assertThat;
public class ResponseCookieTests {
@Test
public void defaultValues() {
public void basic() {
assertThat(ResponseCookie.from("id", null).build().toString()).isEqualTo("id=");
assertThat(ResponseCookie.from("id", "1fWa").build().toString()).isEqualTo("id=1fWa");
ResponseCookie cookie = ResponseCookie.from("id", "1fWa")
.domain("abc").path("/path").maxAge(0).httpOnly(true).secure(true).sameSite("None")
.build();
assertThat(cookie.toString()).isEqualTo("id=1fWa; Path=/path; Domain=abc; " +
"Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT; " +
"Secure; HttpOnly; SameSite=None");
}
@Test
public void httpOnlyStrictSecureWithDomainAndPath() {
assertThat(ResponseCookie.from("id", "1fWa").domain("spring.io").path("/projects")
.httpOnly(true).secure(true).sameSite("strict").build().toString()).isEqualTo("id=1fWa; Path=/projects; Domain=spring.io; Secure; HttpOnly; SameSite=strict");
public void nameChecks() {
Arrays.asList("id", "i.d.", "i-d", "+id", "i*d", "i$d", "#id")
.forEach(name -> ResponseCookie.from(name, "value").build());
Arrays.asList("\"id\"", "id\t", "i\td", "i d", "i;d", "{id}", "[id]", "\"", "id\u0091")
.forEach(name -> assertThatThrownBy(() -> ResponseCookie.from(name, "value").build())
.hasMessageContaining("RFC2616 token"));
}
@Test
public void maxAge() {
public void valueChecks() {
Duration maxAge = Duration.ofDays(365);
String expires = HttpHeaders.formatDate(System.currentTimeMillis() + maxAge.toMillis());
expires = expires.substring(0, expires.indexOf(":") + 1);
Arrays.asList("1fWa", "", null, "1f=Wa", "1f-Wa", "1f/Wa", "1.f.W.a.")
.forEach(value -> ResponseCookie.from("id", value).build());
assertThat(ResponseCookie.from("id", "1fWa").maxAge(maxAge).build().toString())
.startsWith("id=1fWa; Max-Age=31536000; Expires=" + expires)
.endsWith(" GMT");
assertThat(ResponseCookie.from("id", "1fWa").maxAge(maxAge.getSeconds()).build().toString())
.startsWith("id=1fWa; Max-Age=31536000; Expires=" + expires)
.endsWith(" GMT");
}
@Test
public void maxAge0() {
assertThat(ResponseCookie.from("id", "1fWa").maxAge(Duration.ofSeconds(0)).build().toString()).isEqualTo("id=1fWa; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT");
assertThat(ResponseCookie.from("id", "1fWa").maxAge(0).build().toString()).isEqualTo("id=1fWa; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT");
Arrays.asList("1f\tWa", "\t", "1f Wa", "1f;Wa", "\"1fWa", "1f\\Wa", "1f\"Wa", "\"", "1fWa\u0005", "1f\u0091Wa")
.forEach(value -> assertThatThrownBy(() -> ResponseCookie.from("id", value).build())
.hasMessageContaining("RFC2616 cookie value"));
}
}

View File

@@ -18,7 +18,7 @@ package org.springframework.mock.http.server.reactive.test;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
@@ -32,6 +32,7 @@ import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseCookie;
import org.springframework.http.server.reactive.AbstractServerHttpResponse;
import org.springframework.util.Assert;
import org.springframework.util.MimeType;
@@ -101,8 +102,11 @@ public class MockServerHttpResponse extends AbstractServerHttpResponse {
@Override
protected void applyCookies() {
getCookies().values().stream().flatMap(Collection::stream)
.forEach(cookie -> getHeaders().add(HttpHeaders.SET_COOKIE, cookie.toString()));
for (List<ResponseCookie> cookies : getCookies().values()) {
for (ResponseCookie cookie : cookies) {
getHeaders().add(HttpHeaders.SET_COOKIE, cookie.toString());
}
}
}
@Override