This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit edb2d90e88fd1cff5473629bb3cf06ec771c1ee0 Author: Jan Bednář <m...@janbednar.eu> AuthorDate: Fri Jul 5 15:29:28 2019 +0200 CAMEL-13707: The path should not be empty in combination with http query --- .../component/netty4/http/NettyHttpHelper.java | 36 +++++----- .../netty4/http/NettyHttpGetWithParamTest.java | 1 - .../netty4/http/NettyHttpProducerHeadersTest.java | 84 ++++++++++++++++++++++ 3 files changed, 104 insertions(+), 17 deletions(-) diff --git a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpHelper.java b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpHelper.java index 4170ad7..75acf2e 100644 --- a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpHelper.java +++ b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/NettyHttpHelper.java @@ -31,6 +31,7 @@ import org.apache.camel.Exchange; import org.apache.camel.Message; import org.apache.camel.RuntimeExchangeException; import org.apache.camel.util.IOHelper; +import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.StringHelper; import org.apache.camel.util.URISupport; import org.apache.camel.util.UnsafeUriCharactersEncoder; @@ -204,23 +205,21 @@ public final class NettyHttpHelper { if (path.startsWith("/")) { path = path.substring(1); } - - if (path.length() > 0) { - // inject the dynamic path before the query params, if there are any - int idx = uri.indexOf("?"); - // if there are no query params - if (idx == -1) { - // make sure that there is exactly one "/" between HTTP_URI and HTTP_PATH - uri = uri.endsWith("/") ? uri : uri + "/"; - uri = uri.concat(path); - } else { - // there are query params, so inject the relative path in the right place - String base = uri.substring(0, idx); - base = base.endsWith("/") ? base : base + "/"; - base = base.concat(path); - uri = base.concat(uri.substring(idx)); - } + // inject the dynamic path before the query params, if there are any + int idx = uri.indexOf("?"); + + // if there are no query params + if (idx == -1) { + // make sure that there is exactly one "/" between HTTP_URI and HTTP_PATH + uri = uri.endsWith("/") ? uri : uri + "/"; + uri = uri.concat(path); + } else { + // there are query params, so inject the relative path in the right place + String base = uri.substring(0, idx); + base = base.endsWith("/") ? base : base + "/"; + base = base.concat(path); + uri = base.concat(uri.substring(idx)); } } @@ -258,6 +257,11 @@ public final class NettyHttpHelper { if (queryString != null) { // need to encode query string queryString = UnsafeUriCharactersEncoder.encodeHttpURI(queryString); + if (ObjectHelper.isEmpty(uri.getPath())) { + // If queryString is present, the path cannot be empty - CAMEL-13707 + uri = new URI(url + "/"); + } + uri = URISupport.createURIWithQuery(uri, queryString); } return uri; diff --git a/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/NettyHttpGetWithParamTest.java b/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/NettyHttpGetWithParamTest.java index 71d63ac..aa4b24f 100644 --- a/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/NettyHttpGetWithParamTest.java +++ b/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/NettyHttpGetWithParamTest.java @@ -41,7 +41,6 @@ public class NettyHttpGetWithParamTest extends BaseNettyTest { } @Test - @Ignore("HTTP_QUERY not supported") public void testHttpGetWithParamsViaHeader() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedBodiesReceived("Bye World"); diff --git a/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/NettyHttpProducerHeadersTest.java b/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/NettyHttpProducerHeadersTest.java new file mode 100644 index 0000000..013c420 --- /dev/null +++ b/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/NettyHttpProducerHeadersTest.java @@ -0,0 +1,84 @@ +/* + * 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.netty4.http; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.AvailablePortFinder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Test; + +public class NettyHttpProducerHeadersTest extends CamelTestSupport { + + private int port; + + @Test + public void testWithEmptyPath() { + Map<String, Object> headers = new HashMap<>(); + headers.put(Exchange.HTTP_METHOD, "GET"); + headers.put(Exchange.HTTP_QUERY, "hi=hello"); + String result = template.requestBodyAndHeaders("netty-http:http://localhost:" + port, "", headers, String.class); + Assert.assertEquals("/", result); + } + + @Test + public void testWithSlashPathAndQuery() { + Map<String, Object> headers = new HashMap<>(); + headers.put(Exchange.HTTP_METHOD, "GET"); + headers.put(Exchange.HTTP_PATH, "/"); + headers.put(Exchange.HTTP_QUERY, "hi=hello"); + String result = template.requestBodyAndHeaders("netty-http:http://localhost:" + port, "", headers, String.class); + Assert.assertEquals("/", result); + } + + @Test + public void testWithFilledPathAndQuery() { + Map<String, Object> headers = new HashMap<>(); + headers.put(Exchange.HTTP_METHOD, "GET"); + headers.put(Exchange.HTTP_PATH, "some-path"); + headers.put(Exchange.HTTP_QUERY, "hi=hello"); + String result = template.requestBodyAndHeaders("netty-http:http://localhost:" + port, "", headers, String.class); + Assert.assertEquals("/some-path", result); + } + + @Test + public void testWithNoQuery() { + Map<String, Object> headers = new HashMap<>(); + headers.put(Exchange.HTTP_METHOD, "GET"); + String result = template.requestBodyAndHeaders("netty-http:http://localhost:" + port, "", headers, String.class); + Assert.assertEquals("/", result); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + port = AvailablePortFinder.getNextAvailable(8000); + + from("netty-http:http://localhost:" + port + "?matchOnUriPrefix=true") + .setBody(simple("${header."+Exchange.HTTP_URI+"}")); + } + }; + } +}