View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.cxf;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.module.client.MuleClient;
11  import org.mule.tck.junit4.FunctionalTestCase;
12  import org.mule.tck.junit4.rule.DynamicPort;
13  import org.mule.transport.http.HttpConnector;
14  
15  import org.apache.commons.httpclient.Credentials;
16  import org.apache.commons.httpclient.HttpClient;
17  import org.apache.commons.httpclient.UsernamePasswordCredentials;
18  import org.apache.commons.httpclient.auth.AuthScope;
19  import org.apache.commons.httpclient.methods.PostMethod;
20  import org.apache.commons.httpclient.methods.StringRequestEntity;
21  import org.junit.Rule;
22  import org.junit.Test;
23  
24  import static org.junit.Assert.assertEquals;
25  
26  
27  public class HttpSecurityTestCase extends FunctionalTestCase 
28  {
29      
30      private static String soapRequest = 
31          "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:unk=\"http://unknown.namespace/\">" +
32             "<soapenv:Header/>" +
33             "<soapenv:Body>" +
34                "<unk:echo>" +         
35                   "<arg0>asdf</arg0>" +
36                "</unk:echo>" +
37             "</soapenv:Body>" +
38          "</soapenv:Envelope>";
39  
40      @Rule
41      public DynamicPort dynamicPort1 = new DynamicPort("port1");
42  
43      @Rule
44      public DynamicPort dynamicPort2 = new DynamicPort("port2");
45  
46      @Override
47      protected String getConfigResources()
48      {
49          return "http-security-conf.xml";
50      }
51  
52      /**
53       * This test doesn't work in Maven because Mule can't load the keystores from the jars
54       * @throws Exception
55       */
56      @Test
57      public void testBasicAuth() throws Exception
58      {
59          HttpClient client = new HttpClient();
60          Credentials credentials = new UsernamePasswordCredentials("admin", "admin");
61          client.getState().setCredentials(AuthScope.ANY, credentials);
62          client.getParams().setAuthenticationPreemptive(true);
63  
64          PostMethod method = new PostMethod("https://localhost:" + dynamicPort2.getNumber() + "/services/Echo");
65          method.setDoAuthentication(true);
66          StringRequestEntity requestEntity = new StringRequestEntity(soapRequest, "text/plain", "UTF-8");
67          method.setRequestEntity(requestEntity);
68          
69          int result = client.executeMethod(method);
70  
71          assertEquals(200, result);
72          System.out.println(method.getResponseBodyAsString());
73  
74          credentials = new UsernamePasswordCredentials("admin", "adminasd");
75          client.getState().setCredentials(AuthScope.ANY, credentials);
76          client.getParams().setAuthenticationPreemptive(true);
77  
78          result = client.executeMethod(method);
79  
80          assertEquals(401, result);
81      }
82  
83      @Test
84      public void testBasicAuthWithCxfClient() throws Exception
85      {
86          MuleClient client = new MuleClient(muleContext);
87  
88          MuleMessage result = client.send("cxf:http://admin:admin@localhost:" + dynamicPort1.getNumber() + "/services/Echo?method=echo", "Hello", null);
89  
90          final int status = result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0);
91          assertEquals(200, status);
92      }
93  
94  }