1   /*
2    * $Id: HttpFilterFunctionalTestCase.java 10789 2008-02-12 20:04:43Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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("/index.html", get.getResponseBodyAsString());
42          }
43          finally
44          {
45              get.releaseConnection();
46          }
47      }
48  
49      public void testAuthenticationFailureBadCredentials() throws Exception
50      {
51          doRequest(null, "localhost", "anonX", "anonX", "http://localhost:4567/index.html", true, false, 401);
52      }
53  
54      public void testAuthenticationAuthorised() throws Exception
55      {
56          doRequest(null, "localhost", "anon", "anon", "http://localhost:4567/index.html", false, true, 200);
57      }
58  
59      public void testAuthenticationAuthorisedWithHandshake() throws Exception
60      {
61          doRequest(null, "localhost", "anon", "anon", "http://localhost:4567/index.html", true, false, 200);
62      }
63  
64      public void testAuthenticationAuthorisedWithHandshakeAndBadRealm() throws Exception
65      {
66          doRequest("blah", "localhost", "anon", "anon", "http://localhost:4567/index.html", true, false, 401);
67      }
68  
69      public void testAuthenticationAuthorisedWithHandshakeAndRealm() throws Exception
70      {
71          doRequest("mule-realm", "localhost", "ross", "ross", "http://localhost:4567/index.html", true, false,
72              200);
73      }
74  
75      private void doRequest(String realm,
76                             String host,
77                             String user,
78                             String pass,
79                             String url,
80                             boolean handshake,
81                             boolean preemtive,
82                             int result) throws Exception
83      {
84          HttpClient client = new HttpClient();
85          client.getParams().setAuthenticationPreemptive(preemtive);
86          client.getState().setCredentials(new AuthScope(host, -1, realm),
87              new UsernamePasswordCredentials(user, pass));
88          GetMethod get = new GetMethod(url);
89          get.setDoAuthentication(handshake);
90  
91          try
92          {
93              int status = client.executeMethod(get);
94              assertEquals(result, status);
95          }
96          finally
97          {
98              get.releaseConnection();
99          }
100     }
101 
102 }