View Javadoc

1   /*
2    * $Id: AuthenticationAgainstMultipleProvidersTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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  
11  package org.mule.module.acegi;
12  
13  import org.mule.tck.FunctionalTestCase;
14  
15  import org.apache.commons.httpclient.Credentials;
16  import org.apache.commons.httpclient.HttpClient;
17  import org.apache.commons.httpclient.HttpStatus;
18  import org.apache.commons.httpclient.UsernamePasswordCredentials;
19  import org.apache.commons.httpclient.auth.AuthScope;
20  import org.apache.commons.httpclient.methods.PostMethod;
21  import org.apache.commons.httpclient.methods.StringRequestEntity;
22  
23  public class AuthenticationAgainstMultipleProvidersTestCase extends FunctionalTestCase
24  {
25  
26      protected String getConfigResources()
27      {
28          return "mule-multiple-providers-config.xml";
29      }
30  
31      public void testProvider1() throws Exception
32      {
33          HttpClient httpClient = new HttpClient();
34          Credentials credentials = new UsernamePasswordCredentials("admin1", "admin1");
35          httpClient.getState().setCredentials(AuthScope.ANY, credentials);
36          httpClient.getParams().setAuthenticationPreemptive(true);
37          
38          PostMethod postMethod = new PostMethod("http://localhost:4445");
39          postMethod.setDoAuthentication(true);
40          postMethod.setRequestEntity(new StringRequestEntity("hello", "text/html", "UTF-8"));
41                                  
42          assertEquals(HttpStatus.SC_OK, httpClient.executeMethod(postMethod));
43          assertEquals("hello", postMethod.getResponseBodyAsString());                  
44          
45          credentials = new UsernamePasswordCredentials("asdf", "asdf");
46          httpClient.getState().setCredentials(AuthScope.ANY, credentials);
47          assertEquals(HttpStatus.SC_UNAUTHORIZED, httpClient.executeMethod(postMethod)); 
48          
49          credentials = new UsernamePasswordCredentials("admin2", "admin2");
50          httpClient.getState().setCredentials(AuthScope.ANY, credentials);
51          assertEquals(HttpStatus.SC_UNAUTHORIZED, httpClient.executeMethod(postMethod)); 
52      }    
53      
54      public void testProvider2() throws Exception
55      {
56          HttpClient httpClient = new HttpClient();
57          Credentials credentials = new UsernamePasswordCredentials("admin2", "admin2");
58          httpClient.getState().setCredentials(AuthScope.ANY, credentials);
59          httpClient.getParams().setAuthenticationPreemptive(true);
60          
61          PostMethod postMethod = new PostMethod("http://localhost:4446");
62          postMethod.setDoAuthentication(true);
63          postMethod.setRequestEntity(new StringRequestEntity("hello", "text/html", "UTF-8"));
64                                  
65          assertEquals(HttpStatus.SC_OK, httpClient.executeMethod(postMethod));
66          assertEquals("hello", postMethod.getResponseBodyAsString());    
67          
68          credentials = new UsernamePasswordCredentials("asdf", "asdf");
69          httpClient.getState().setCredentials(AuthScope.ANY, credentials);
70          assertEquals(HttpStatus.SC_UNAUTHORIZED, httpClient.executeMethod(postMethod)); 
71          
72          credentials = new UsernamePasswordCredentials("admin", "admin");
73          httpClient.getState().setCredentials(AuthScope.ANY, credentials);
74          assertEquals(HttpStatus.SC_UNAUTHORIZED, httpClient.executeMethod(postMethod)); 
75      }
76      
77      public void testMultipleProviders() throws Exception
78      {
79          HttpClient httpClient = new HttpClient();
80          Credentials credentials = new UsernamePasswordCredentials("admin1", "admin1");
81          httpClient.getState().setCredentials(AuthScope.ANY, credentials);
82          httpClient.getParams().setAuthenticationPreemptive(true);
83          
84          PostMethod postMethod = new PostMethod("http://localhost:4447");
85          postMethod.setDoAuthentication(true);
86          postMethod.setRequestEntity(new StringRequestEntity("hello", "text/html", "UTF-8"));
87                                  
88          assertEquals(HttpStatus.SC_OK, httpClient.executeMethod(postMethod));
89          assertEquals("hello", postMethod.getResponseBodyAsString());                  
90          
91          credentials = new UsernamePasswordCredentials("asdf", "asdf");
92          httpClient.getState().setCredentials(AuthScope.ANY, credentials);
93          assertEquals(HttpStatus.SC_UNAUTHORIZED, httpClient.executeMethod(postMethod)); 
94          
95          credentials = new UsernamePasswordCredentials("admin2", "admin2");
96          httpClient.getState().setCredentials(AuthScope.ANY, credentials);
97          assertEquals(HttpStatus.SC_OK, httpClient.executeMethod(postMethod));
98          assertEquals("hello", postMethod.getResponseBodyAsString());
99      }
100     
101 }
102 
103