This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-3.20.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.20.x by this push: new ceec89b462c CAMEL-18840: camel-http - Make followRedirects=true|false easier to use and no need for tweaking other options. ceec89b462c is described below commit ceec89b462cae6682c404bce88b9782b9418059a Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Dec 27 12:24:10 2022 +0100 CAMEL-18840: camel-http - Make followRedirects=true|false easier to use and no need for tweaking other options. --- .../apache/camel/component/http/HttpComponent.java | 7 +- .../camel/component/http/FollowRedirectTest.java | 86 ++++++++++++++++++++++ .../camel/component/http/HttpRedirectTest.java | 4 +- 3 files changed, 93 insertions(+), 4 deletions(-) diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java index 69a5f071fcb..a727bdb8423 100644 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java @@ -472,7 +472,10 @@ public class HttpComponent extends HttpCommonComponent implements RestProducerFa // validate that we could resolve all httpClient. parameters as this component is lenient validateParameters(uri, httpClientOptions, null); - if (redirectHandlingDisabled) { + // endpoint parameter can override component level + boolean fr = getParameter(parameters, "followRedirects", Boolean.class, followRedirects); + + if (redirectHandlingDisabled || !fr) { clientBuilder.disableRedirectHandling(); } if (automaticRetriesDisabled) { @@ -493,7 +496,7 @@ public class HttpComponent extends HttpCommonComponent implements RestProducerFa if (defaultUserAgentDisabled) { clientBuilder.disableDefaultUserAgent(); } - if (followRedirects) { + if (fr) { clientBuilder.setRedirectStrategy(new LaxRedirectStrategy()); } diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/FollowRedirectTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/FollowRedirectTest.java new file mode 100644 index 00000000000..0e604c98ec6 --- /dev/null +++ b/components/camel-http/src/test/java/org/apache/camel/component/http/FollowRedirectTest.java @@ -0,0 +1,86 @@ +/* + * 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.camel.component.http.handler.BasicValidationHandler; +import org.apache.http.impl.bootstrap.HttpServer; +import org.apache.http.impl.bootstrap.ServerBootstrap; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.apache.camel.component.http.HttpMethods.GET; + +public class FollowRedirectTest 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("/someplaceelse", new BasicValidationHandler(GET.name(), null, null, "Bye World")) + .registerHandler("/redirect", (request, response, context) -> { + response.setHeader("Location", "someplaceelse"); + response.setStatusCode(303); + }).create(); + localServer.start(); + + super.setUp(); + } + + @Test + public void testFollowRedirect() { + HttpComponent http = context.getComponent("http", HttpComponent.class); + http.setFollowRedirects(true); + + String uri = "http://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/redirect"; + Exchange out = fluentTemplate.to(uri).send(); + + Assertions.assertEquals(200, out.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE)); + String body = out.getMessage().getBody(String.class); + Assertions.assertEquals("Bye World", body); + } + + @Test + public void testFollowRedirectDisabled() { + HttpComponent http = context.getComponent("http", HttpComponent.class); + http.setFollowRedirects(false); + + String uri = "http://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + + "/redirect?throwExceptionOnFailure=false"; + Exchange out = fluentTemplate.to(uri).send(); + + Assertions.assertEquals(303, out.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE)); + } + + @Test + public void testFollowRedirectHandlingDisabled() { + HttpComponent http = context.getComponent("http", HttpComponent.class); + http.setRedirectHandlingDisabled(false); + + String uri = "http://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + + "/redirect?throwExceptionOnFailure=false"; + Exchange out = fluentTemplate.to(uri).send(); + + Assertions.assertEquals(303, out.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE)); + } + +} diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRedirectTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRedirectTest.java index 23b70729d88..9831232909b 100644 --- a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRedirectTest.java +++ b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRedirectTest.java @@ -70,7 +70,7 @@ public class HttpRedirectTest extends BaseHttpTest { } @Test - public void httpRedirect() throws Exception { + public void httpRedirectFalse() throws Exception { String uri = "http://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/test?httpClient.redirectsEnabled=false&httpClient.socketTimeout=60000&httpClient.connectTimeout=60000" @@ -93,7 +93,7 @@ public class HttpRedirectTest extends BaseHttpTest { String uri = "http://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/test?httpClient.socketTimeout=60000&httpClient.connectTimeout=60000" - + "&httpClient.staleConnectionCheckEnabled=false"; + + "&httpClient.staleConnectionCheckEnabled=false&followRedirects=true"; Exchange out = template.request(uri, exchange -> { // no data });