You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2022/03/10 14:37:07 UTC
[camel] branch main updated: CAMEL-17773: http components using toD fix when contet-path is empty. Thanks to Artem St for unit tests.
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 b9a5d21 CAMEL-17773: http components using toD fix when contet-path is empty. Thanks to Artem St for unit tests.
b9a5d21 is described below
commit b9a5d21b6b0d89d520eecada5c7cc80a8f3479c6
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Mar 10 15:36:01 2022 +0100
CAMEL-17773: http components using toD fix when contet-path is empty. Thanks to Artem St for unit tests.
---
.../camel/http/base/HttpSendDynamicAware.java | 28 ++++----
.../http/common/HttpSendDynamicAwareTest.java | 16 +++++
.../http/HttpSendDynamicAwareEmptyPathTest.java | 75 ++++++++++++++++++++++
3 files changed, 107 insertions(+), 12 deletions(-)
diff --git a/components/camel-http-base/src/main/java/org/apache/camel/http/base/HttpSendDynamicAware.java b/components/camel-http-base/src/main/java/org/apache/camel/http/base/HttpSendDynamicAware.java
index 123f5a9..b9675cc 100644
--- a/components/camel-http-base/src/main/java/org/apache/camel/http/base/HttpSendDynamicAware.java
+++ b/components/camel-http-base/src/main/java/org/apache/camel/http/base/HttpSendDynamicAware.java
@@ -159,28 +159,32 @@ public class HttpSendDynamicAware extends SendDynamicAwareSupport {
String host = parse.getHost();
String path = parse.getPath();
String authority = parse.getAuthority();
+
+ // we want host to include port
+ int port = parse.getPort();
+ if (port > 0 && port != 80 && port != 443) {
+ host += ":" + port;
+ }
+
// if the path is just a trailing slash then skip it (eg it must be longer than just the slash itself)
if (path != null && path.length() > 1) {
- int port = parse.getPort();
- if (port > 0 && port != 80 && port != 443) {
- host += ":" + port;
- }
// remove double slash for path
while (path.startsWith("//")) {
path = path.substring(1);
}
- if (!httpComponent) {
- // include scheme for components that are not camel-http
- String scheme = parse.getScheme();
- if (scheme != null) {
- host = scheme + "://" + host;
- }
+ }
+
+ // include scheme for components that are not camel-http
+ if (!httpComponent) {
+ String scheme = parse.getScheme();
+ if (scheme != null) {
+ host = scheme + "://" + host;
}
- return new String[] { host, path, authority };
}
+
+ return new String[] { host, path, authority };
} catch (URISyntaxException e) {
// ignore
- return new String[] { u, null, null };
}
// no context path
diff --git a/components/camel-http-common/src/test/java/org/apache/camel/http/common/HttpSendDynamicAwareTest.java b/components/camel-http-common/src/test/java/org/apache/camel/http/common/HttpSendDynamicAwareTest.java
index af00b10..1593f4f 100644
--- a/components/camel-http-common/src/test/java/org/apache/camel/http/common/HttpSendDynamicAwareTest.java
+++ b/components/camel-http-common/src/test/java/org/apache/camel/http/common/HttpSendDynamicAwareTest.java
@@ -80,4 +80,20 @@ public class HttpSendDynamicAwareTest {
assertEquals("localhost:8443", result[0], "Parse should add port if https and port other than 443 specified");
}
+ @Test
+ public void testHttpsUndefinedPortEmptyPathParseUri() {
+ this.httpSendDynamicAware.setScheme("https");
+ DynamicAwareEntry entry = new DynamicAwareEntry("https://localhost:80/", null, null, null);
+ String[] result = httpSendDynamicAware.parseUri(entry);
+ assertEquals("localhost", result[0], "Parse should not add port if https and not specified");
+ }
+
+ @Test
+ public void testHttpsDefinedPortEmptyPathParseUri() {
+ this.httpSendDynamicAware.setScheme("https");
+ DynamicAwareEntry entry = new DynamicAwareEntry("https://localhost:1234/", null, null, null);
+ String[] result = httpSendDynamicAware.parseUri(entry);
+ assertEquals("localhost:1234", result[0], "Parse should not add port if https and not specified");
+ }
+
}
diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpSendDynamicAwareEmptyPathTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpSendDynamicAwareEmptyPathTest.java
new file mode 100644
index 0000000..27b368f
--- /dev/null
+++ b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpSendDynamicAwareEmptyPathTest.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.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.http.handler.DrinkValidationHandler;
+import org.apache.http.impl.bootstrap.HttpServer;
+import org.apache.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.apache.camel.component.http.HttpMethods.GET;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class HttpSendDynamicAwareEmptyPathTest extends BaseHttpTest {
+
+ private HttpServer localServer;
+
+ @BeforeEach
+ @Override
+ public void setUp() throws Exception {
+ localServer = ServerBootstrap.bootstrap().setHttpProcessor(getBasicHttpProcessor())
+ .setConnectionReuseStrategy(getConnectionReuseStrategy()).setResponseFactory(getHttpResponseFactory())
+ .setExpectationVerifier(getHttpExpectationVerifier()).setSslContext(getSSLContext())
+ .registerHandler("/", new DrinkValidationHandler(GET.name(), null, null, "drink")).create();
+ localServer.start();
+
+ super.setUp();
+ }
+
+ @AfterEach
+ @Override
+ public void tearDown() throws Exception {
+ super.tearDown();
+
+ if (localServer != null) {
+ localServer.stop();
+ }
+ }
+
+ @Override
+ protected RoutesBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:moes")
+ .toD("http://localhost:" + localServer.getLocalPort()
+ + "?throwExceptionOnFailure=false&drink=${header.drink}");
+ }
+ };
+ }
+
+ @Test
+ public void testEmptyPath() throws Exception {
+ String out = fluentTemplate.to("direct:moes").withHeader("drink", "beer").request(String.class);
+ assertEquals("Drinking beer", out);
+ }
+
+}