View Javadoc

1   /*
2    * $Id: HttpFilterFunctionalTestCase.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  import org.mule.transport.http.HttpConstants;
15  
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.GetMethod;
20  
21  public class HttpFilterFunctionalTestCase extends FunctionalTestCase
22  {
23  
24      protected String getConfigResources()
25      {
26          return "http-filter-test.xml";
27      }
28  
29      public void testAuthenticationFailureNoContext() throws Exception
30      {
31          HttpClient client = new HttpClient();
32          client.getParams().setAuthenticationPreemptive(true);
33          GetMethod get = new GetMethod("http://localhost:4567/index.html");
34  
35          get.setDoAuthentication(false);
36  
37          try
38          {
39              int status = client.executeMethod(get);
40              assertEquals(HttpConstants.SC_UNAUTHORIZED, status);
41              assertEquals(
42                  "Registered authentication is set to org.mule.module.acegi.filters.http.HttpBasicAuthenticationFilter "
43                                  + "but there was no security context on the session. Authentication denied on endpoint "
44                                  + "http://localhost:4567. Message payload is of type: String",
45                  get.getResponseBodyAsString());
46          }
47          finally
48          {
49              get.releaseConnection();
50          }
51      }
52  
53      public void testAuthenticationFailureBadCredentials() throws Exception
54      {
55          doRequest(null, "localhost", "anonX", "anonX", "http://localhost:4567/index.html", true, false, 401);
56      }
57  
58      public void testAuthenticationAuthorised() throws Exception
59      {
60          doRequest(null, "localhost", "anon", "anon", "http://localhost:4567/index.html", false, true, 200);
61      }
62  
63      public void testAuthenticationAuthorisedWithHandshake() throws Exception
64      {
65          doRequest(null, "localhost", "anon", "anon", "http://localhost:4567/index.html", true, false, 200);
66      }
67  
68      public void testAuthenticationAuthorisedWithHandshakeAndBadRealm() throws Exception
69      {
70          doRequest("blah", "localhost", "anon", "anon", "http://localhost:4567/index.html", true, false, 401);
71      }
72  
73      public void testAuthenticationAuthorisedWithHandshakeAndRealm() throws Exception
74      {
75          doRequest("mule-realm", "localhost", "ross", "ross", "http://localhost:4567/index.html", true, false,
76              200);
77      }
78  
79      private void doRequest(String realm,
80                             String host,
81                             String user,
82                             String pass,
83                             String url,
84                             boolean handshake,
85                             boolean preemtive,
86                             int result) throws Exception
87      {
88          HttpClient client = new HttpClient();
89          client.getParams().setAuthenticationPreemptive(preemtive);
90          client.getState().setCredentials(new AuthScope(host, -1, realm),
91              new UsernamePasswordCredentials(user, pass));
92          GetMethod get = new GetMethod(url);
93          get.setDoAuthentication(handshake);
94  
95          try
96          {
97              int status = client.executeMethod(get);
98              assertEquals(result, status);
99          }
100         finally
101         {
102             get.releaseConnection();
103         }
104     }
105 
106 }