Skip to content

Commit

Permalink
Support cookies with the same name with Reactor Netty
Browse files Browse the repository at this point in the history
Ignore the related test with Undertow due to a bug in
their cookies handling.

Closes gh-28490
  • Loading branch information
sdeleuze committed Dec 6, 2023
1 parent b53ffa3 commit 8fe2c78
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
* <p>This class is based on {@link ReactorServerHttpRequest}.
*
* @author Violeta Georgieva
* @author Sebastien Deleuze
* @since 6.0
*/
class ReactorNetty2ServerHttpRequest extends AbstractServerHttpRequest {
Expand Down Expand Up @@ -144,8 +145,8 @@ private static String resolveRequestUri(HttpServerRequest request) {
@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public ReactorServerHttpRequest(HttpServerRequest request, NettyDataBufferFactor
@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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -30,11 +30,14 @@
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 {

Expand Down Expand Up @@ -81,6 +84,23 @@ public void basicTest(HttpServer httpServer) throws Exception {
.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<>();
Expand Down

0 comments on commit 8fe2c78

Please sign in to comment.