1   /* Copyright 2004, 2005, 2006 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  
22  import org.acegisecurity.context.SecurityContextHolder;
23  import org.acegisecurity.context.httpinvoker.AuthenticationSimpleHttpInvokerRequestExecutor;
24  
25  import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
26  
27  import java.io.IOException;
28  
29  import java.net.HttpURLConnection;
30  import java.net.URL;
31  
32  import java.util.HashMap;
33  import java.util.Map;
34  
35  
36  /**
37   * Tests {@link AuthenticationSimpleHttpInvokerRequestExecutor}.
38   *
39   * @author Ben Alex
40   * @version $Id: AuthenticationSimpleHttpInvokerRequestExecutorTests.java 1496 2006-05-23 13:38:33Z benalex $
41   */
42  public class AuthenticationSimpleHttpInvokerRequestExecutorTests extends TestCase {
43      //~ Constructors ===================================================================================================
44  
45      public AuthenticationSimpleHttpInvokerRequestExecutorTests() {
46          super();
47      }
48  
49      public AuthenticationSimpleHttpInvokerRequestExecutorTests(String arg0) {
50          super(arg0);
51      }
52  
53      //~ Methods ========================================================================================================
54  
55      public static void main(String[] args) {
56          junit.textui.TestRunner.run(AuthenticationSimpleHttpInvokerRequestExecutorTests.class);
57      }
58  
59      public void testNormalOperation() throws Exception {
60          // Setup client-side context
61          Authentication clientSideAuthentication = new UsernamePasswordAuthenticationToken("Aladdin", "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("http://localhost/"));
68          executor.prepareConnection(conn, 10);
69  
70          // Check connection properties
71          // See http://www.faqs.org/rfcs/rfc1945.html section 11.1 for example
72          // we are comparing against
73          assertEquals("Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==", conn.getRequestProperty("Authorization"));
74  
75          SecurityContextHolder.getContext().setAuthentication(null);
76      }
77  
78      public void testNullContextHolderIsNull() throws Exception {
79          SecurityContextHolder.getContext().setAuthentication(null);
80  
81          // Create a connection and ensure our executor sets its
82          // properties correctly
83          AuthenticationSimpleHttpInvokerRequestExecutor executor = new AuthenticationSimpleHttpInvokerRequestExecutor();
84          HttpURLConnection conn = new MockHttpURLConnection(new URL("http://localhost/"));
85          executor.prepareConnection(conn, 10);
86  
87          // Check connection properties (shouldn't be an Authorization header)
88          assertNull(conn.getRequestProperty("Authorization"));
89      }
90  
91      //~ Inner Classes ==================================================================================================
92  
93      private class MockHttpURLConnection extends HttpURLConnection {
94          private Map requestProperties = new HashMap();
95  
96          public MockHttpURLConnection(URL u) {
97              super(u);
98          }
99  
100         public void connect() throws IOException {
101             throw new UnsupportedOperationException("mock not implemented");
102         }
103 
104         public void disconnect() {
105             throw new UnsupportedOperationException("mock not implemented");
106         }
107 
108         public String getRequestProperty(String key) {
109             return (String) requestProperties.get(key);
110         }
111 
112         public void setRequestProperty(String key, String value) {
113             requestProperties.put(key, value);
114         }
115 
116         public boolean usingProxy() {
117             throw new UnsupportedOperationException("mock not implemented");
118         }
119     }
120 }