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 2020/06/28 17:31:19 UTC

[cxf] 01/02: Adding JAX-WS integration test for DefaultBasicAuthSupplier

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

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

commit 1c67d067ec967b907f56e1c5e0c268dc73452300
Author: reta <dr...@gmail.com>
AuthorDate: Sun Jun 28 11:35:28 2020 -0400

    Adding JAX-WS integration test for DefaultBasicAuthSupplier
    
    (cherry picked from commit 7a13bfff60babe60ebfcdb45b5838eee6947e191)
---
 .../cxf/systest/jaxws/JaxwsAsyncFailOverTest.java  |  6 ++
 ...ncFailOverTest.java => JaxwsBasicAuthTest.java} | 86 ++++++++++++++--------
 2 files changed, 63 insertions(+), 29 deletions(-)

diff --git a/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/JaxwsAsyncFailOverTest.java b/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/JaxwsAsyncFailOverTest.java
index 70dc8c8..df8e9d5 100644
--- a/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/JaxwsAsyncFailOverTest.java
+++ b/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/JaxwsAsyncFailOverTest.java
@@ -34,6 +34,7 @@ import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
 import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
 import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
 
+import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
@@ -79,6 +80,11 @@ public class JaxwsAsyncFailOverTest  extends AbstractBusClientServerTestBase {
         assertTrue("server did not launch correctly", launchServer(Server.class, true));
     }
 
+    @AfterClass
+    public static void stopServers() throws Exception {
+        stopAllServers();
+    }
+
     @Test
     public void testUseFailOverOnClient() throws Exception {
         List<String> serviceList = new ArrayList<>();
diff --git a/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/JaxwsAsyncFailOverTest.java b/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/JaxwsBasicAuthTest.java
similarity index 52%
copy from systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/JaxwsAsyncFailOverTest.java
copy to systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/JaxwsBasicAuthTest.java
index 70dc8c8..0a24474 100644
--- a/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/JaxwsAsyncFailOverTest.java
+++ b/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/JaxwsBasicAuthTest.java
@@ -19,29 +19,37 @@
 
 package org.apache.cxf.systest.jaxws;
 
-import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
+import javax.annotation.Resource;
 import javax.jws.WebService;
-import javax.xml.ws.Response;
+import javax.xml.ws.BindingProvider;
+import javax.xml.ws.WebServiceContext;
+import javax.xml.ws.WebServiceException;
+import javax.xml.ws.handler.MessageContext;
 
-import org.apache.cxf.clustering.FailoverFeature;
-import org.apache.cxf.clustering.RandomStrategy;
+import org.apache.cxf.endpoint.Client;
+import org.apache.cxf.frontend.ClientProxy;
 import org.apache.cxf.greeter_control.AbstractGreeterImpl;
 import org.apache.cxf.greeter_control.Greeter;
-import org.apache.cxf.greeter_control.types.GreetMeResponse;
+import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
 import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
 import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
+import org.apache.cxf.transport.http.HTTPConduit;
+import org.apache.cxf.transport.http.auth.DefaultBasicAuthSupplier;
 
+import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
-public class JaxwsAsyncFailOverTest  extends AbstractBusClientServerTestBase {
-    static final String PORT = allocatePort(ServerNoBodyParts.class, 1);
-    static final String PORT2 = allocatePort(ServerNoBodyParts.class, 2);
+public class JaxwsBasicAuthTest extends AbstractBusClientServerTestBase {
+    static final String PORT = allocatePort(JaxwsBasicAuthTest.class);
 
     public static class Server extends AbstractBusTestServerBase {
 
@@ -64,12 +72,34 @@ public class JaxwsAsyncFailOverTest  extends AbstractBusClientServerTestBase {
             }
         }
 
-        @WebService(serviceName = "BasicGreeterService",
+        @WebService(serviceName = "ProtectedGreeterService",
                     portName = "GreeterPort",
                     endpointInterface = "org.apache.cxf.greeter_control.Greeter",
                     targetNamespace = "http://cxf.apache.org/greeter_control",
                     wsdlLocation = "testutils/greeter_control.wsdl")
         public class GreeterImpl extends AbstractGreeterImpl {
+            @Resource private WebServiceContext context;
+            
+            @Override
+            public String greetMe(String arg) {
+                final MessageContext messageContext = context.getMessageContext();
+                
+                final Map<String, List<String>> headers =
+                    CastUtils.cast((Map<?, ?>)messageContext.get(MessageContext.HTTP_REQUEST_HEADERS));
+
+                if (headers == null) {
+                    throw new WebServiceException("Not authorized");
+                }
+
+                final String authorization = headers.get("Authorization").get(0);
+                final String expected = DefaultBasicAuthSupplier.getBasicAuthHeader("user", "test", true);
+                
+                if (!expected.equals(authorization)) {
+                    throw new WebServiceException("Not authorized");
+                }
+
+                return "CXF is protected: " + arg;
+            }
         }
     }
 
@@ -78,34 +108,32 @@ public class JaxwsAsyncFailOverTest  extends AbstractBusClientServerTestBase {
     public static void startServers() throws Exception {
         assertTrue("server did not launch correctly", launchServer(Server.class, true));
     }
+    
+    @AfterClass
+    public static void stopServers() throws Exception {
+        stopAllServers();
+    }
 
     @Test
-    public void testUseFailOverOnClient() throws Exception {
-        List<String> serviceList = new ArrayList<>();
-        serviceList.add("http://localhost:" + PORT + "/SoapContext/GreeterPort");
-
-        RandomStrategy strategy = new RandomStrategy();
-        strategy.setAlternateAddresses(serviceList);
-
-        FailoverFeature ff = new FailoverFeature();
-        ff.setStrategy(strategy);
-
+    public void testUseBasicAuthFromClient() throws Exception {
         // setup the feature by using JAXWS front-end API
         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
         // set a fake address to kick off the failover feature
-        factory.setAddress("http://localhost:" + PORT2 + "/SoapContext/GreeterPort");
-        factory.getFeatures().add(ff);
+        factory.setAddress("http://localhost:" + PORT + "/SoapContext/GreeterPort");
         factory.setServiceClass(Greeter.class);
         Greeter proxy = factory.create(Greeter.class);
 
-        Response<GreetMeResponse>  response = proxy.greetMeAsync("cxf");
-        int waitCount = 0;
-        while (!response.isDone() && waitCount < 15) {
-            Thread.sleep(1000);
-            waitCount++;
-        }
-        assertTrue("Response still not received.", response.isDone());
-
+        Client clientProxy = ClientProxy.getClient(proxy);
+        HTTPConduit conduit = (HTTPConduit) clientProxy.getConduit();
+        conduit.getAuthorization().setAuthorizationType("Basic");
+        conduit.getAuthorization().setUserName("user");
+        conduit.getAuthorization().setPassword("test");
+        
+        final BindingProvider bindingProvider = (BindingProvider) proxy;
+        bindingProvider.getRequestContext().put("encode.basicauth.with.iso8859", true);
+
+        String response = proxy.greetMe("cxf");
+        assertThat("CXF is protected: cxf", equalTo(response));
     }
 
 }