Browse Source

Support cookies with the same name with Reactor Netty

Ignore the related test with Undertow due to a bug in
their cookies handling.

Closes gh-28490
pull/31778/head
Sébastien Deleuze 2 years ago
parent
commit
8fe2c780df
  1. 5
      spring-web/src/main/java/org/springframework/http/server/reactive/ReactorNetty2ServerHttpRequest.java
  2. 4
      spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java
  3. 22
      spring-web/src/test/java/org/springframework/http/server/reactive/CookieIntegrationTests.java

5
spring-web/src/main/java/org/springframework/http/server/reactive/ReactorNetty2ServerHttpRequest.java

@ -50,6 +50,7 @@ import org.springframework.util.MultiValueMap; @@ -50,6 +50,7 @@ import org.springframework.util.MultiValueMap;
* <p>This class is based on {@link ReactorServerHttpRequest}.
*
* @author Violeta Georgieva
* @author Sebastien Deleuze
* @since 6.0
*/
class ReactorNetty2ServerHttpRequest extends AbstractServerHttpRequest {
@ -144,8 +145,8 @@ class ReactorNetty2ServerHttpRequest extends AbstractServerHttpRequest { @@ -144,8 +145,8 @@ class ReactorNetty2ServerHttpRequest extends AbstractServerHttpRequest {
@Override
protected MultiValueMap<String, HttpCookie> initCookies() {
MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
for (CharSequence name : this.request.cookies().keySet()) {
for (HttpCookiePair cookie : this.request.cookies().get(name)) {
for (CharSequence name : this.request.allCookies().keySet()) {
for (HttpCookiePair cookie : this.request.allCookies().get(name)) {
CharSequence cookieValue = cookie.value();
HttpCookie httpCookie = new HttpCookie(name.toString(), cookieValue != null ? cookieValue.toString() : null);
cookies.add(name.toString(), httpCookie);

4
spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java

@ -75,8 +75,8 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest { @@ -75,8 +75,8 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest {
@Override
protected MultiValueMap<String, HttpCookie> initCookies() {
MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
for (CharSequence name : this.request.cookies().keySet()) {
for (Cookie cookie : this.request.cookies().get(name)) {
for (CharSequence name : this.request.allCookies().keySet()) {
for (Cookie cookie : this.request.allCookies().get(name)) {
HttpCookie httpCookie = new HttpCookie(name.toString(), cookie.value());
cookies.add(name.toString(), httpCookie);
}

22
spring-web/src/test/java/org/springframework/http/server/reactive/CookieIntegrationTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -30,11 +30,14 @@ import org.springframework.http.ResponseEntity; @@ -30,11 +30,14 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.testfixture.http.server.reactive.bootstrap.AbstractHttpHandlerIntegrationTests;
import org.springframework.web.testfixture.http.server.reactive.bootstrap.HttpServer;
import org.springframework.web.testfixture.http.server.reactive.bootstrap.UndertowHttpServer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
/**
* @author Rossen Stoyanchev
* @author Sebastien Deleuze
*/
public class CookieIntegrationTests extends AbstractHttpHandlerIntegrationTests {
@ -81,6 +84,23 @@ public class CookieIntegrationTests extends AbstractHttpHandlerIntegrationTests @@ -81,6 +84,23 @@ public class CookieIntegrationTests extends AbstractHttpHandlerIntegrationTests
.containsExactlyInAnyOrder("path=/", "domain=example.com");
}
@ParameterizedHttpServerTest
public void cookiesWithSameNameTest(HttpServer httpServer) throws Exception {
assumeFalse(httpServer instanceof UndertowHttpServer, "Bug in Undertow in Cookies with same name handling");
startServer(httpServer);
URI url = new URI("http://localhost:" + port);
String header = "SID=31d4d96e407aad42; lang=en-US; lang=zh-CN";
new RestTemplate().exchange(
RequestEntity.get(url).header("Cookie", header).build(), Void.class);
Map<String, List<HttpCookie>> requestCookies = this.cookieHandler.requestCookies;
assertThat(requestCookies.size()).isEqualTo(2);
assertThat(requestCookies.get("SID")).extracting(HttpCookie::getValue).containsExactly("31d4d96e407aad42");
assertThat(requestCookies.get("lang")).extracting(HttpCookie::getValue).containsExactly("en-US", "zh-CN");
}
// No client side HttpCookie support yet
private List<String> splitCookie(String value) {
List<String> list = new ArrayList<>();

Loading…
Cancel
Save