View Javadoc

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