[camel] branch master updated: CAMEL-14640: Upgrade to http client 4.x for testing.

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[camel] branch master updated: CAMEL-14640: Upgrade to http client 4.x for testing.

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 8a8aa51  CAMEL-14640: Upgrade to http client 4.x for testing.
8a8aa51 is described below

commit 8a8aa51677a4749e3b7d11ec66e6eeecedb1fa23
Author: Claus Ibsen <[hidden email]>
AuthorDate: Wed Mar 25 20:41:24 2020 +0100

    CAMEL-14640: Upgrade to http client 4.x for testing.
---
 components/camel-netty-http/pom.xml                |  5 --
 .../netty/http/NettyHttpBridgeEncodedPathTest.java | 17 +++---
 .../netty/http/NettyHttpHeaderCaseTest.java        | 30 ++++++-----
 .../netty/http/NettyHttpHeaderMaxSizeTest.java     | 62 ----------------------
 .../netty/http/NettyHttpMapHeadersFalseTest.java   | 27 +++++-----
 .../netty/http/NettyHttpMethodRestrictTest.java    | 39 ++++++++------
 .../netty/http/NettyHttpMuteExceptionTest.java     | 22 +++++---
 .../netty/http/NettyHttpTraceDisabledTest.java     | 32 ++++++-----
 8 files changed, 100 insertions(+), 134 deletions(-)

diff --git a/components/camel-netty-http/pom.xml b/components/camel-netty-http/pom.xml
index c83dc55..2bdeb08 100644
--- a/components/camel-netty-http/pom.xml
+++ b/components/camel-netty-http/pom.xml
@@ -75,11 +75,6 @@
             <scope>test</scope>
         </dependency>
         <dependency>
-            <groupId>commons-httpclient</groupId>
-            <artifactId>commons-httpclient</artifactId>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <scope>test</scope>
diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpBridgeEncodedPathTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpBridgeEncodedPathTest.java
index efee78e..16c8bfd 100644
--- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpBridgeEncodedPathTest.java
+++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpBridgeEncodedPathTest.java
@@ -22,8 +22,10 @@ import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
 import org.junit.Test;
 
 public class NettyHttpBridgeEncodedPathTest extends BaseNettyTest {
@@ -48,12 +50,15 @@ public class NettyHttpBridgeEncodedPathTest extends BaseNettyTest {
         mock.message(0).header(Exchange.HTTP_RAW_QUERY).isNull();
 
         // cannot use template as it automatically decodes some chars in the path
-        HttpClient httpClient = new HttpClient();
-        GetMethod httpGet = new GetMethod("http://localhost:" + port4 + "/nettyTestRouteC/" + path);
-        int status = httpClient.executeMethod(httpGet);
+        CloseableHttpClient client = HttpClients.createDefault();
 
-        assertEquals("Get a wrong response status", 200, status);
+        HttpGet httpGet = new HttpGet("http://localhost:" + port4 + "/nettyTestRouteC/" + path);
+        HttpResponse response = client.execute(httpGet);
+
+        assertEquals("Get a wrong response status", 200, response.getStatusLine().getStatusCode());
         assertMockEndpointsSatisfied();
+
+        client.close();
     }
 
     @Override
diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpHeaderCaseTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpHeaderCaseTest.java
index f67e1d8..62f6e02 100644
--- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpHeaderCaseTest.java
+++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpHeaderCaseTest.java
@@ -20,27 +20,33 @@ import java.util.LinkedHashMap;
 import java.util.Map;
 
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpMethod;
-import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
 import org.junit.Test;
 
 public class NettyHttpHeaderCaseTest extends BaseNettyTest {
 
     @Test
     public void testHttpHeaderCase() throws Exception {
-        HttpClient client = new HttpClient();
-        HttpMethod method = new PostMethod("http://localhost:" + getPort() + "/myapp/mytest");
+        CloseableHttpClient client = HttpClients.createDefault();
 
-        method.setRequestHeader("clientHeader", "fooBAR");
-        method.setRequestHeader("OTHER", "123");
-        method.setRequestHeader("beer", "Carlsberg");
+        HttpPost method = new HttpPost("http://localhost:" + getPort() + "/myapp/mytest");
 
-        client.executeMethod(method);
+        method.addHeader("clientHeader", "fooBAR");
+        method.addHeader("OTHER", "123");
+        method.addHeader("beer", "Carlsberg");
 
-        assertEquals("Bye World", method.getResponseBodyAsString());
-        assertEquals("aBc123", method.getResponseHeader("MyCaseHeader").getValue());
-        assertEquals("456DEf", method.getResponseHeader("otherCaseHeader").getValue());
+        HttpResponse response = client.execute(method);
+        String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
+
+        assertEquals("Bye World", responseString);
+        assertEquals("aBc123", response.getFirstHeader("MyCaseHeader").getValue());
+        assertEquals("456DEf", response.getFirstHeader("otherCaseHeader").getValue());
+
+        client.close();
     }
 
     @Override
diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpHeaderMaxSizeTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpHeaderMaxSizeTest.java
deleted file mode 100644
index 68e59b0..0000000
--- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpHeaderMaxSizeTest.java
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * 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.netty.http;
-
-import org.apache.camel.builder.RouteBuilder;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpMethod;
-import org.apache.commons.httpclient.methods.PostMethod;
-import org.junit.Test;
-
-public class NettyHttpHeaderMaxSizeTest extends BaseNettyTest {
-
-    @Test
-    public void testHttpHeaderMaxSizeOk() throws Exception {
-        HttpClient client = new HttpClient();
-        HttpMethod method = new PostMethod("http://localhost:" + getPort() + "/myapp/mytest");
-
-        method.setRequestHeader("name", "you");
-
-        client.executeMethod(method);
-
-        assertEquals(200, method.getStatusCode());
-        assertEquals("Bye World", method.getResponseBodyAsString());
-    }
-
-    @Test
-    public void testHttpHeaderMaxSizeFail() throws Exception {
-        HttpClient client = new HttpClient();
-        HttpMethod method = new PostMethod("http://localhost:" + getPort() + "/myapp/mytest");
-
-        method.setRequestHeader("name", "12345678901234567890123456789012345678901234567890123456789012345678901234567890");
-
-        client.executeMethod(method);
-
-        assertEquals(400, method.getStatusCode());
-    }
-
-    @Override
-    protected RouteBuilder createRouteBuilder() throws Exception {
-        return new RouteBuilder() {
-            public void configure() throws Exception {
-                from("netty-http:http://localhost:{{port}}/myapp/mytest?maxHeaderSize=100")
-                        .transform().constant("Bye World");
-            }
-        };
-    }
-
-}
diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpMapHeadersFalseTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpMapHeadersFalseTest.java
index 7b17510..78508cf 100644
--- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpMapHeadersFalseTest.java
+++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpMapHeadersFalseTest.java
@@ -17,27 +17,30 @@
 package org.apache.camel.component.netty.http;
 
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpMethod;
-import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
 import org.junit.Test;
 
 public class NettyHttpMapHeadersFalseTest extends BaseNettyTest {
 
     @Test
     public void testHttpHeaderCase() throws Exception {
-        HttpClient client = new HttpClient();
-        HttpMethod method = new PostMethod("http://localhost:" + getPort() + "/myapp/mytest");
+        CloseableHttpClient client = HttpClients.createDefault();
+        HttpPost method = new HttpPost("http://localhost:" + getPort() + "/myapp/mytest");
 
-        method.setRequestHeader("clientHeader", "fooBAR");
-        method.setRequestHeader("OTHER", "123");
-        method.setRequestHeader("beer", "Carlsberg");
+        method.addHeader("clientHeader", "fooBAR");
+        method.addHeader("OTHER", "123");
+        method.addHeader("beer", "Carlsberg");
 
-        client.executeMethod(method);
+        HttpResponse response = client.execute(method);
 
-        assertEquals("Bye World", method.getResponseBodyAsString());
-        assertEquals("aBc123", method.getResponseHeader("MyCaseHeader").getValue());
-        assertEquals("456DEf", method.getResponseHeader("otherCaseHeader").getValue());
+        String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
+        assertEquals("Bye World", responseString);
+        assertEquals("aBc123", response.getFirstHeader("MyCaseHeader").getValue());
+        assertEquals("456DEf", response.getFirstHeader("otherCaseHeader").getValue());
     }
 
     @Override
diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpMethodRestrictTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpMethodRestrictTest.java
index ad56cef..8472e9c 100644
--- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpMethodRestrictTest.java
+++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpMethodRestrictTest.java
@@ -18,10 +18,13 @@ package org.apache.camel.component.netty.http;
 
 import org.apache.camel.Message;
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.methods.GetMethod;
-import org.apache.commons.httpclient.methods.PostMethod;
-import org.apache.commons.httpclient.methods.StringRequestEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
 import org.junit.Test;
 
 public class NettyHttpMethodRestrictTest extends BaseNettyTest {
@@ -32,27 +35,31 @@ public class NettyHttpMethodRestrictTest extends BaseNettyTest {
 
     @Test
     public void testProperHttpMethod() throws Exception {
-        HttpClient httpClient = new HttpClient();
-        PostMethod httpPost = new PostMethod(getUrl());
+        CloseableHttpClient client = HttpClients.createDefault();
 
-        StringRequestEntity reqEntity = new StringRequestEntity("This is a test", null, null);
-        httpPost.setRequestEntity(reqEntity);
+        HttpPost httpPost = new HttpPost(getUrl());
+        httpPost.setEntity(new StringEntity("This is a test"));
 
-        int status = httpClient.executeMethod(httpPost);
+        HttpResponse response = client.execute(httpPost);
 
-        assertEquals("Get a wrong response status", 200, status);
+        assertEquals("Get a wrong response status", 200, response.getStatusLine().getStatusCode());
 
-        String result = httpPost.getResponseBodyAsString();
-        assertEquals("Get a wrong result", "This is a test response", result);
+        String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
+        assertEquals("Get a wrong result", "This is a test response", responseString);
+
+        client.close();
     }
 
     @Test
     public void testImproperHttpMethod() throws Exception {
-        HttpClient httpClient = new HttpClient();
-        GetMethod httpGet = new GetMethod(getUrl());
-        int status = httpClient.executeMethod(httpGet);
+        CloseableHttpClient client = HttpClients.createDefault();
+
+        HttpGet httpGet = new HttpGet(getUrl());
+        HttpResponse response = client.execute(httpGet);
+
+        assertEquals("Get a wrong response status", 405, response.getStatusLine().getStatusCode());
 
-        assertEquals("Get a wrong response status", 405, status);
+        client.close();
     }
 
     @Override
diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpMuteExceptionTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpMuteExceptionTest.java
index 3473a4b..653c39e 100644
--- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpMuteExceptionTest.java
+++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpMuteExceptionTest.java
@@ -17,24 +17,30 @@
 package org.apache.camel.component.netty.http;
 
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
 import org.junit.Test;
 
 public class NettyHttpMuteExceptionTest extends BaseNettyTest {
 
     @Test
     public void testMuteException() throws Exception {
+        CloseableHttpClient client = HttpClients.createDefault();
 
-        HttpClient client = new HttpClient();
-        GetMethod get = new GetMethod("http://localhost:" + getPort() + "/foo");
-        get.setRequestHeader("Accept", "application/text");
-        client.executeMethod(get);
+        HttpGet get = new HttpGet("http://localhost:" + getPort() + "/foo");
+        get.addHeader("Accept", "application/text");
 
-        String body = get.getResponseBodyAsString();
+        HttpResponse response = client.execute(get);
+
+        String body = EntityUtils.toString(response.getEntity(), "UTF-8");
         assertNotNull(body);
         assertEquals("Exception", body);
-        assertEquals(500, get.getStatusCode());
+        assertEquals(500, response.getStatusLine().getStatusCode());
+
+        client.close();
     }
 
     @Override
diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpTraceDisabledTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpTraceDisabledTest.java
index d95be0a..2b88ca8 100644
--- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpTraceDisabledTest.java
+++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpTraceDisabledTest.java
@@ -17,8 +17,10 @@
 package org.apache.camel.component.netty.http;
 
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.methods.TraceMethod;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpTrace;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
 import org.junit.Test;
 
 public class NettyHttpTraceDisabledTest extends BaseNettyTest {
@@ -28,24 +30,28 @@ public class NettyHttpTraceDisabledTest extends BaseNettyTest {
 
     @Test
     public void testTraceDisabled() throws Exception {
-        HttpClient httpclient = new HttpClient();
-        TraceMethod trace = new TraceMethod("http://localhost:" + portTraceOff + "/myservice");
-        httpclient.executeMethod(trace);
+        CloseableHttpClient client = HttpClients.createDefault();
+        HttpTrace trace = new HttpTrace("http://localhost:" + portTraceOff + "/myservice");
+
+        HttpResponse response = client.execute(trace);
 
         // TRACE shouldn't be allowed by default
-        assertTrue(trace.getStatusCode() == 405);
-        trace.releaseConnection();
+        assertEquals(405, response.getStatusLine().getStatusCode());
+
+        client.close();
     }
 
     @Test
     public void testTraceEnabled() throws Exception {
-        HttpClient httpclient = new HttpClient();
-        TraceMethod trace = new TraceMethod("http://localhost:" + portTraceOn + "/myservice");
-        httpclient.executeMethod(trace);
+        CloseableHttpClient client = HttpClients.createDefault();
+        HttpTrace trace = new HttpTrace("http://localhost:" + portTraceOn + "/myservice");
+
+        HttpResponse response = client.execute(trace);
+
+        // TRACE is allowed
+        assertEquals(200, response.getStatusLine().getStatusCode());
 
-        // TRACE is now allowed
-        assertTrue(trace.getStatusCode() == 200);
-        trace.releaseConnection();
+        client.close();
     }
 
     @Override