This is an automated email from the ASF dual-hosted git repository.

fmariani pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-spring-boot.git

commit 214165a508c116f4c7b33b4e5656b20aea7499b1
Author: Nikita Konovalov <nkono...@redhat.com>
AuthorDate: Fri Jan 17 16:08:18 2025 +0100

    CSB-5888: added cookie tests
---
 .../springboot/SpringBootPlatformHttpConsumer.java | 14 +++--
 .../SpringBootPlatformHttpCookiesTest.java         | 67 +++++++++++++++-------
 2 files changed, 54 insertions(+), 27 deletions(-)

diff --git 
a/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpConsumer.java
 
b/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpConsumer.java
index 285926cdfd3..3da18e89aa4 100644
--- 
a/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpConsumer.java
+++ 
b/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpConsumer.java
@@ -39,6 +39,7 @@ import org.apache.camel.http.common.HttpHelper;
 import org.apache.camel.support.DefaultConsumer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.web.bind.annotation.CookieValue;
 import org.springframework.web.bind.annotation.ResponseBody;
 
 public class SpringBootPlatformHttpConsumer extends DefaultConsumer implements 
PlatformHttpConsumer, Suspendable, SuspendableService {
@@ -217,14 +218,17 @@ public class SpringBootPlatformHttpConsumer extends 
DefaultConsumer implements P
         }
 
         @Override
-        public String getCookieValue(String cookieName) {
+        public String getCookieValue(@CookieValue String cookieName) {
             Cookie[] cookie = request.getCookies();
-            for (Cookie c : cookie) {
-                if (c.getName().equals(cookieName)) {
-                    return c.getValue();
+            // ensure cookies are not null
+            if (cookie != null) {
+                for (Cookie c : cookie) {
+                    if (c.getName().equals(cookieName)) {
+                        return c.getValue();
+                    }
                 }
             }
-            return null;
+            return "Cookie not found";
         }
     }
 }
diff --git 
a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpCookiesTest.java
 
b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpCookiesTest.java
index 830e22381c1..f161a2e3855 100644
--- 
a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpCookiesTest.java
+++ 
b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpCookiesTest.java
@@ -16,10 +16,14 @@
  */
 package org.apache.camel.component.platform.http.springboot;
 
-import io.restassured.RestAssured;
 import jakarta.servlet.http.Cookie;
+
+import io.restassured.RestAssured;
 import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.platform.http.cookie.CookieConfiguration;
+import org.apache.camel.component.platform.http.cookie.CookieHandler;
 import org.apache.camel.spring.boot.CamelAutoConfiguration;
 import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
 import org.junit.jupiter.api.BeforeEach;
@@ -35,9 +39,8 @@ import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.test.annotation.DirtiesContext;
 
-import java.util.Arrays;
-
 import static io.restassured.RestAssured.given;
+import static io.restassured.matcher.RestAssuredMatchers.detailedCookie;
 import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.startsWith;
 import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -72,25 +75,27 @@ public class SpringBootPlatformHttpCookiesTest {
             return new RouteBuilder() {
                 @Override
                 public void configure() {
-                    from("platform-http:/add")
+                    
getCamelContext().getStreamCachingStrategy().setSpoolEnabled(true);
+                    from("platform-http:/addCookie?useCookieHandler=true")
                             .process(exchange -> {
-                                PlatformHttpMessage message = 
(PlatformHttpMessage) exchange.getMessage();
-                                message.getResponse().addCookie(new 
Cookie("foo", "bar"));
+                                exchange.getProperty(Exchange.COOKIE_HANDLER, 
CookieHandler.class).addCookie("foo", "bar");
                             })
                             .setBody().constant("add");
 
-                    from("platform-http:/remove")
+                    from("platform-http:/removeCookie?useCookieHandler=true")
                             .process(exchange -> {
-                                PlatformHttpMessage message = 
(PlatformHttpMessage) exchange.getMessage();
-
-                                Cookie cookie = 
Arrays.stream(message.getRequest().getCookies()).findFirst().get();
-                                cookie.setMaxAge(0);
-                                cookie.setValue(null);
-
-                                message.getResponse().addCookie(cookie);
+                                exchange.getProperty(Exchange.COOKIE_HANDLER, 
CookieHandler.class).addCookie("foo", "bar");
+                                exchange.getProperty(Exchange.COOKIE_HANDLER, 
CookieHandler.class).removeCookie("foo");
                             })
                             .setBody().constant("remove");
 
+                    from("platform-http:/getCookieValue?useCookieHandler=true")
+                            .process(exchange -> {
+                                exchange.getProperty(Exchange.COOKIE_HANDLER, 
CookieHandler.class).addCookie("foo", "bar");
+                                exchange.getProperty(Exchange.COOKIE_HANDLER, 
CookieHandler.class).getCookieValue("foo");
+                            })
+                            .setBody().constant("get");
+
                     from("platform-http:/replace")
                             .process(exchange -> {
                                 PlatformHttpMessage message = 
(PlatformHttpMessage) exchange.getMessage();
@@ -110,29 +115,47 @@ public class SpringBootPlatformHttpCookiesTest {
     }
 
     @Test
-    public void addCookie() {
+    public void testAddCookie() {
         given()
-                .header("cookie", "foo=bar")
                 .when()
-                .get("/add")
+                .get("/addCookie")
                 .then()
                 .statusCode(200)
-                .header("set-cookie", "foo=bar")
+                .cookie("foo",
+                        detailedCookie()
+                                .value("bar")
+                                .path(CookieConfiguration.DEFAULT_PATH)
+                                .domain((String) null)
+                                
.sameSite(CookieConfiguration.DEFAULT_SAME_SITE.getValue()))
                 .body(equalTo("add"));
     }
 
     @Test
-    public void removeCookie() {
+    public void testRemoveCookie() {
         given()
-                .header("cookie", "foo=bar")
                 .when()
-                .get("/remove")
+                .get("/removeCookie")
                 .then()
                 .statusCode(200)
-                .header("set-cookie", startsWith("foo=; Max-Age=0; Expires="))
+                .cookie("foo",
+                        detailedCookie()
+                                .maxAge(equalTo(0L)))
                 .body(equalTo("remove"));
     }
 
+    @Test
+    public void testGetCookieValue() {
+        given()
+                .when()
+                .get("/getCookieValue")
+                .then()
+                .statusCode(200)
+                .cookie("foo",
+                        detailedCookie()
+                                .value("bar"))
+                .body(equalTo("get"));
+    }
+
     @Test
     public void replaceCookie() {
         given()

Reply via email to