You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2021/09/21 01:40:51 UTC

[cxf] 01/01: Added test case for generic WebClient flow

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

reta pushed a commit to branch CXF-8597
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit c63fcc30d326afd5acfd030d4a691a35e620c542
Author: Andriy Redko <dr...@gmail.com>
AuthorDate: Mon Sep 20 21:40:24 2021 -0400

    Added test case for generic WebClient flow
---
 .../jaxrs/ClientHttpConnectionOutInterceptor.java  | 71 ++++++++++++++++++++++
 .../systest/jaxrs/JAXRSRequestDispatcherTest.java  | 38 ++++++++++++
 2 files changed, 109 insertions(+)

diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/ClientHttpConnectionOutInterceptor.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/ClientHttpConnectionOutInterceptor.java
new file mode 100644
index 0000000..66d8413
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/ClientHttpConnectionOutInterceptor.java
@@ -0,0 +1,71 @@
+/**
+ * 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.cxf.systest.jaxrs;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.phase.AbstractPhaseInterceptor;
+import org.apache.cxf.phase.Phase;
+
+class ClientHttpConnectionOutInterceptor extends AbstractPhaseInterceptor<Message> {
+    private Collection<HttpURLConnection> connections = new ArrayList<>();
+
+    ClientHttpConnectionOutInterceptor() {
+        super(Phase.SEND_ENDING);
+    }
+
+    @Override
+    public void handleMessage(Message message) throws Fault {
+        final HttpURLConnection connection = (HttpURLConnection) message.get("http.connection");
+        synchronized (connections) {
+            connections.add(connection);
+        }
+    }
+
+    public boolean checkAllClosed() {
+        synchronized (connections) {
+            if (connections.isEmpty()) {
+                return false;
+            }
+            
+            return !connections
+                .stream()
+                .anyMatch(this::hasUnclosedInputStream);
+        }
+    }
+    
+    private boolean hasUnclosedInputStream(HttpURLConnection connection) {
+        try {
+            final InputStream inputStream = connection.getInputStream();
+            inputStream.read(new byte [] {}); /* 0 bytes to read */
+            return true;
+        } catch (IOException ex) {
+            // The HttpInputStream throws an IOException in case the input stream is already
+            // closed (since we actually read nothing).
+            return !ex.getMessage().equals("stream is closed");
+        }
+    }
+}
\ No newline at end of file
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSRequestDispatcherTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSRequestDispatcherTest.java
index 7002a30..5055d9d 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSRequestDispatcherTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSRequestDispatcherTest.java
@@ -19,6 +19,7 @@
 
 package org.apache.cxf.systest.jaxrs;
 
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -33,8 +34,11 @@ import org.junit.BeforeClass;
 import org.junit.Ignore;
 import org.junit.Test;
 
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
 public class JAXRSRequestDispatcherTest extends AbstractBusClientServerTestBase {
@@ -60,6 +64,7 @@ public class JAXRSRequestDispatcherTest extends AbstractBusClientServerTestBase
     }
 
     private void doTestGetBookHTML(String endpointAddress) throws Exception {
+
         WebClient client = WebClient.create(endpointAddress)
             .accept(MediaType.TEXT_HTML);
 
@@ -143,6 +148,38 @@ public class JAXRSRequestDispatcherTest extends AbstractBusClientServerTestBase
         assertEquals("Welcome", welcome);
     }
 
+    @Test
+    public void testGetBookHTMLFromEnsureResponseStreamIsUnclosed() throws Exception {
+        final ClientHttpConnectionOutInterceptor interceptor = new ClientHttpConnectionOutInterceptor();
+
+        String endpointAddress = "http://localhost:" + PORT + "/the/bookstore4/books/html/123";
+        WebClient client = WebClient.create(endpointAddress).accept(MediaType.TEXT_HTML);
+        WebClient.getConfig(client).getOutInterceptors().add(interceptor);
+
+        XMLSource source = client.get(XMLSource.class);
+        Map<String, String> namespaces = new HashMap<>();
+        namespaces.put("xhtml", "http://www.w3.org/1999/xhtml");
+        namespaces.put("books", "http://www.w3.org/books");
+        String value = source.getValue("xhtml:html/xhtml:body/xhtml:ul/books:bookTag", namespaces);
+        assertEquals("CXF Rocks", value);
+        
+        assertThat(interceptor.checkAllClosed(), is(false));
+    }
+    
+    @Test
+    public void testGetBookHTMLFromEnsureResponseStreamIsAutoClosed() throws Exception {
+        final ClientHttpConnectionOutInterceptor interceptor = new ClientHttpConnectionOutInterceptor();
+        final Map<String, Object> properties = Collections.singletonMap("response.stream.auto.close", true);
+
+        String endpointAddress = "http://localhost:" + PORT + "/the/bookstore4/books/html/123";
+        WebClient client = WebClient.create(endpointAddress, properties).accept(MediaType.TEXT_HTML);
+        WebClient.getConfig(client).getOutInterceptors().add(interceptor);
+
+        final String source = client.get(String.class);
+        assertThat(source, containsString("CXF Rocks"));
+        assertThat(interceptor.checkAllClosed(), is(true));
+    }
+    
     private void doTestGetBookHTMLFromWelcomeList(String address) throws Exception {
         WebClient client = WebClient.create(address)
             .accept(MediaType.TEXT_HTML);
@@ -154,4 +191,5 @@ public class JAXRSRequestDispatcherTest extends AbstractBusClientServerTestBase
         String value = source.getValue("xhtml:html/xhtml:body/xhtml:ul/books:bookTag", namespaces);
         assertEquals("Welcome to CXF", value);
     }
+    
 }