[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.

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

pascalschumacher 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 ae7893b  CAMEL-14640: Upgrade to http client 4.x for testing.
ae7893b is described below

commit ae7893b49d88fe57bb95333f69808ad256be2c35
Author: Pascal Schumacher <[hidden email]>
AuthorDate: Wed Mar 25 23:08:49 2020 +0100

    CAMEL-14640: Upgrade to http client 4.x for testing.
   
    Use try-with-resources blocks to close HttpClients and HttpResponses.
---
 .../netty/http/NettyHttpBridgeEncodedPathTest.java | 17 +++++-----
 .../netty/http/NettyHttpHeaderCaseTest.java        | 26 ++++++++--------
 .../netty/http/NettyHttpMapHeadersFalseTest.java   | 25 +++++++--------
 .../netty/http/NettyHttpMethodRestrictTest.java    | 36 ++++++++++------------
 .../netty/http/NettyHttpMuteExceptionTest.java     | 26 ++++++++--------
 .../netty/http/NettyHttpTraceDisabledTest.java     | 32 +++++++++----------
 6 files changed, 77 insertions(+), 85 deletions(-)

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 16c8bfd..a8d3140 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,7 +22,7 @@ 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.http.HttpResponse;
+import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
@@ -50,15 +50,14 @@ 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
-        CloseableHttpClient client = HttpClients.createDefault();
+        try (CloseableHttpClient client = HttpClients.createDefault()) {
+            HttpGet httpGet = new HttpGet("http://localhost:" + port4 + "/nettyTestRouteC/" + path);
 
-        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();
+            try (CloseableHttpResponse response = client.execute(httpGet)) {
+                assertEquals("Get a wrong response status", 200, response.getStatusLine().getStatusCode());
+                assertMockEndpointsSatisfied();
+            }
+        }
     }
 
     @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 62f6e02..b588228 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,7 +20,7 @@ import java.util.LinkedHashMap;
 import java.util.Map;
 
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
@@ -31,22 +31,22 @@ public class NettyHttpHeaderCaseTest extends BaseNettyTest {
 
     @Test
     public void testHttpHeaderCase() throws Exception {
-        CloseableHttpClient client = HttpClients.createDefault();
+        try (CloseableHttpClient client = HttpClients.createDefault()) {
 
-        HttpPost method = new HttpPost("http://localhost:" + getPort() + "/myapp/mytest");
+            HttpPost method = new HttpPost("http://localhost:" + getPort() + "/myapp/mytest");
 
-        method.addHeader("clientHeader", "fooBAR");
-        method.addHeader("OTHER", "123");
-        method.addHeader("beer", "Carlsberg");
+            method.addHeader("clientHeader", "fooBAR");
+            method.addHeader("OTHER", "123");
+            method.addHeader("beer", "Carlsberg");
 
-        HttpResponse response = client.execute(method);
-        String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
+            try (CloseableHttpResponse 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();
+                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/NettyHttpMapHeadersFalseTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpMapHeadersFalseTest.java
index 78508cf..8ab84aa 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,7 +17,7 @@
 package org.apache.camel.component.netty.http;
 
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
@@ -28,19 +28,20 @@ public class NettyHttpMapHeadersFalseTest extends BaseNettyTest {
 
     @Test
     public void testHttpHeaderCase() throws Exception {
-        CloseableHttpClient client = HttpClients.createDefault();
-        HttpPost method = new HttpPost("http://localhost:" + getPort() + "/myapp/mytest");
+        try (CloseableHttpClient client = HttpClients.createDefault()) {
+            HttpPost method = new HttpPost("http://localhost:" + getPort() + "/myapp/mytest");
 
-        method.addHeader("clientHeader", "fooBAR");
-        method.addHeader("OTHER", "123");
-        method.addHeader("beer", "Carlsberg");
+            method.addHeader("clientHeader", "fooBAR");
+            method.addHeader("OTHER", "123");
+            method.addHeader("beer", "Carlsberg");
 
-        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());
+            try (CloseableHttpResponse 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());
+            }
+        }
     }
 
     @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 8472e9c..cdd6986 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,7 +18,7 @@ package org.apache.camel.component.netty.http;
 
 import org.apache.camel.Message;
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.entity.StringEntity;
@@ -35,31 +35,27 @@ public class NettyHttpMethodRestrictTest extends BaseNettyTest {
 
     @Test
     public void testProperHttpMethod() throws Exception {
-        CloseableHttpClient client = HttpClients.createDefault();
+        try (CloseableHttpClient client = HttpClients.createDefault()) {
+            HttpPost httpPost = new HttpPost(getUrl());
+            httpPost.setEntity(new StringEntity("This is a test"));
 
-        HttpPost httpPost = new HttpPost(getUrl());
-        httpPost.setEntity(new StringEntity("This is a test"));
+            try (CloseableHttpResponse response = client.execute(httpPost)) {
+                assertEquals("Get a wrong response status", 200, response.getStatusLine().getStatusCode());
 
-        HttpResponse response = client.execute(httpPost);
-
-        assertEquals("Get a wrong response status", 200, response.getStatusLine().getStatusCode());
-
-        String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
-        assertEquals("Get a wrong result", "This is a test response", responseString);
-
-        client.close();
+                String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
+                assertEquals("Get a wrong result", "This is a test response", responseString);
+            }
+        }
     }
 
     @Test
     public void testImproperHttpMethod() throws Exception {
-        CloseableHttpClient client = HttpClients.createDefault();
-
-        HttpGet httpGet = new HttpGet(getUrl());
-        HttpResponse response = client.execute(httpGet);
-
-        assertEquals("Get a wrong response status", 405, response.getStatusLine().getStatusCode());
-
-        client.close();
+        try (CloseableHttpClient client = HttpClients.createDefault()) {
+            HttpGet httpGet = new HttpGet(getUrl());
+            try (CloseableHttpResponse response = client.execute(httpGet)) {
+                assertEquals("Get a wrong response status", 405, response.getStatusLine().getStatusCode());
+            }
+        }
     }
 
     @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 653c39e..2ae4ed5 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,7 +17,7 @@
 package org.apache.camel.component.netty.http;
 
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
@@ -28,19 +28,17 @@ public class NettyHttpMuteExceptionTest extends BaseNettyTest {
 
     @Test
     public void testMuteException() throws Exception {
-        CloseableHttpClient client = HttpClients.createDefault();
-
-        HttpGet get = new HttpGet("http://localhost:" + getPort() + "/foo");
-        get.addHeader("Accept", "application/text");
-
-        HttpResponse response = client.execute(get);
-
-        String body = EntityUtils.toString(response.getEntity(), "UTF-8");
-        assertNotNull(body);
-        assertEquals("Exception", body);
-        assertEquals(500, response.getStatusLine().getStatusCode());
-
-        client.close();
+        try (CloseableHttpClient client = HttpClients.createDefault()) {
+            HttpGet get = new HttpGet("http://localhost:" + getPort() + "/foo");
+            get.addHeader("Accept", "application/text");
+
+            try (CloseableHttpResponse response = client.execute(get)) {
+                String body = EntityUtils.toString(response.getEntity(), "UTF-8");
+                assertNotNull(body);
+                assertEquals("Exception", body);
+                assertEquals(500, response.getStatusLine().getStatusCode());
+            }
+        }
     }
 
     @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 2b88ca8..a4ff019 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,7 +17,7 @@
 package org.apache.camel.component.netty.http;
 
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpTrace;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
@@ -30,28 +30,26 @@ public class NettyHttpTraceDisabledTest extends BaseNettyTest {
 
     @Test
     public void testTraceDisabled() throws Exception {
-        CloseableHttpClient client = HttpClients.createDefault();
-        HttpTrace trace = new HttpTrace("http://localhost:" + portTraceOff + "/myservice");
+        try (CloseableHttpClient client = HttpClients.createDefault()) {
+            HttpTrace trace = new HttpTrace("http://localhost:" + portTraceOff + "/myservice");
 
-        HttpResponse response = client.execute(trace);
-
-        // TRACE shouldn't be allowed by default
-        assertEquals(405, response.getStatusLine().getStatusCode());
-
-        client.close();
+            try (CloseableHttpResponse response = client.execute(trace)) {
+                // TRACE shouldn't be allowed by default
+                assertEquals(405, response.getStatusLine().getStatusCode());
+            }
+        }
     }
 
     @Test
     public void testTraceEnabled() throws Exception {
-        CloseableHttpClient client = HttpClients.createDefault();
-        HttpTrace trace = new HttpTrace("http://localhost:" + portTraceOn + "/myservice");
+        try (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());
-
-        client.close();
+            try (CloseableHttpResponse response = client.execute(trace)) {
+                // TRACE is allowed
+                assertEquals(200, response.getStatusLine().getStatusCode());
+            }
+        }
     }
 
     @Override