1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.context.httpinvoker;
17  
18  import junit.framework.TestCase;
19  
20  import org.acegisecurity.Authentication;
21  import org.acegisecurity.context.SecurityContextHolder;
22  import org.acegisecurity.context.httpinvoker.AuthenticationSimpleHttpInvokerRequestExecutor;
23  import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
24  
25  import java.io.IOException;
26  
27  import java.net.HttpURLConnection;
28  import java.net.URL;
29  
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  
34  /***
35   * Tests {@link AuthenticationSimpleHttpInvokerRequestExecutor}.
36   *
37   * @author Ben Alex
38   * @version $Id: AuthenticationSimpleHttpInvokerRequestExecutorTests.java,v 1.4 2005/11/17 00:56:49 benalex Exp $
39   */
40  public class AuthenticationSimpleHttpInvokerRequestExecutorTests
41      extends TestCase {
42      //~ Constructors ===========================================================
43  
44      public AuthenticationSimpleHttpInvokerRequestExecutorTests() {
45          super();
46      }
47  
48      public AuthenticationSimpleHttpInvokerRequestExecutorTests(String arg0) {
49          super(arg0);
50      }
51  
52      //~ Methods ================================================================
53  
54      public static void main(String[] args) {
55          junit.textui.TestRunner.run(AuthenticationSimpleHttpInvokerRequestExecutorTests.class);
56      }
57  
58      public void testNormalOperation() throws Exception {
59          // Setup client-side context
60          Authentication clientSideAuthentication = new UsernamePasswordAuthenticationToken("Aladdin",
61                  "open sesame");
62          SecurityContextHolder.getContext().setAuthentication(clientSideAuthentication);
63  
64          // Create a connection and ensure our executor sets its
65          // properties correctly
66          AuthenticationSimpleHttpInvokerRequestExecutor executor = new AuthenticationSimpleHttpInvokerRequestExecutor();
67          HttpURLConnection conn = new MockHttpURLConnection(new URL(
68                      "http://localhost/"));
69          executor.prepareConnection(conn, 10);
70  
71          // Check connection properties
72          // See http://www.faqs.org/rfcs/rfc1945.html section 11.1 for example
73          // we are comparing against
74          assertEquals("Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
75              conn.getRequestProperty("Authorization"));
76  
77          SecurityContextHolder.getContext().setAuthentication(null);
78      }
79  
80      public void testNullContextHolderIsNull() throws Exception {
81          SecurityContextHolder.getContext().setAuthentication(null);
82  
83          // Create a connection and ensure our executor sets its
84          // properties correctly
85          AuthenticationSimpleHttpInvokerRequestExecutor executor = new AuthenticationSimpleHttpInvokerRequestExecutor();
86          HttpURLConnection conn = new MockHttpURLConnection(new URL(
87                      "http://localhost/"));
88          executor.prepareConnection(conn, 10);
89  
90          // Check connection properties (shouldn't be an Authorization header)
91          assertNull(conn.getRequestProperty("Authorization"));
92      }
93  
94      //~ Inner Classes ==========================================================
95  
96      private class MockHttpURLConnection extends HttpURLConnection {
97          private Map requestProperties = new HashMap();
98  
99          public MockHttpURLConnection(URL u) {
100             super(u);
101         }
102 
103         public void setRequestProperty(String key, String value) {
104             requestProperties.put(key, value);
105         }
106 
107         public String getRequestProperty(String key) {
108             return (String) requestProperties.get(key);
109         }
110 
111         public void connect() throws IOException {
112             throw new UnsupportedOperationException("mock not implemented");
113         }
114 
115         public void disconnect() {
116             throw new UnsupportedOperationException("mock not implemented");
117         }
118 
119         public boolean usingProxy() {
120             throw new UnsupportedOperationException("mock not implemented");
121         }
122     }
123 }