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

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new f14d4520a7e [CAMEL-20434]: Fix 411 content length required when 
request body is null (#13157)
f14d4520a7e is described below

commit f14d4520a7e8dce56218bd244eea4a54c051df8c
Author: Vishal Bihani <[email protected]>
AuthorDate: Sun Feb 18 16:14:50 2024 +0530

    [CAMEL-20434]: Fix 411 content length required when request body is null 
(#13157)
    
    * Added NullEntity for null request body
    
    * Removed star imports
---
 .../apache/camel/component/http/HttpProducer.java  |  3 +-
 .../component/http/HttpProducerNullBodyTest.java   | 75 ++++++++++++++++++++++
 2 files changed, 77 insertions(+), 1 deletion(-)

diff --git 
a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
 
b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
index 65a8cd77859..4bd16f77a21 100644
--- 
a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
+++ 
b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
@@ -75,6 +75,7 @@ import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
 import org.apache.hc.core5.http.io.entity.EntityUtils;
 import org.apache.hc.core5.http.io.entity.FileEntity;
 import org.apache.hc.core5.http.io.entity.InputStreamEntity;
+import org.apache.hc.core5.http.io.entity.NullEntity;
 import org.apache.hc.core5.http.io.entity.StringEntity;
 import org.apache.hc.core5.http.protocol.BasicHttpContext;
 import org.apache.hc.core5.http.protocol.HttpContext;
@@ -692,7 +693,7 @@ public class HttpProducer extends DefaultProducer {
         Object body = in.getBody();
         try {
             if (body == null) {
-                return null;
+                return NullEntity.INSTANCE;
             } else if (body instanceof HttpEntity entity) {
                 answer = entity;
                 // special optimized for using these 3 type converters for 
common message payload types
diff --git 
a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProducerNullBodyTest.java
 
b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProducerNullBodyTest.java
new file mode 100644
index 00000000000..fbfe40d5916
--- /dev/null
+++ 
b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProducerNullBodyTest.java
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.http;
+
+import org.apache.camel.Exchange;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
+import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public class HttpProducerNullBodyTest extends BaseHttpTest {
+
+    private HttpServer localServer;
+
+    private String endpointUrl;
+
+    @BeforeEach
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+
+        localServer = 
ServerBootstrap.bootstrap().setHttpProcessor(getBasicHttpProcessor())
+                
.setConnectionReuseStrategy(getConnectionReuseStrategy()).setResponseFactory(getHttpResponseFactory())
+                .setSslContext(getSSLContext())
+                .register("/null-body", (request, response, context) -> {
+                    String contentLength = 
request.getFirstHeader(Exchange.CONTENT_LENGTH).getValue();
+
+                    assertEquals("0", contentLength);
+
+                    response.setCode(HttpStatus.SC_OK);
+                }).create();
+        localServer.start();
+
+        endpointUrl = "http://localhost:"; + localServer.getLocalPort();
+    }
+
+    @AfterEach
+    @Override
+    public void tearDown() throws Exception {
+        super.tearDown();
+
+        if (localServer != null) {
+            localServer.stop();
+        }
+    }
+
+    @Test
+    public void testContentLengthWithNullBody() {
+        Exchange out = template.request(endpointUrl + "/null-body", exchange 
-> {
+            exchange.getIn().setHeader(Exchange.HTTP_METHOD, "POST");
+            exchange.getIn().setBody(null);
+        });
+
+        assertNotNull(out);
+        assertFalse(out.isFailed(), "Should not fail");
+    }
+}

Reply via email to