001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.context.httpinvoker;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.Authentication;
021:
022: import org.acegisecurity.context.SecurityContextHolder;
023: import org.acegisecurity.context.httpinvoker.AuthenticationSimpleHttpInvokerRequestExecutor;
024:
025: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
026:
027: import java.io.IOException;
028:
029: import java.net.HttpURLConnection;
030: import java.net.URL;
031:
032: import java.util.HashMap;
033: import java.util.Map;
034:
035: /**
036: * Tests {@link AuthenticationSimpleHttpInvokerRequestExecutor}.
037: *
038: * @author Ben Alex
039: * @version $Id: AuthenticationSimpleHttpInvokerRequestExecutorTests.java 1496 2006-05-23 13:38:33Z benalex $
040: */
041: public class AuthenticationSimpleHttpInvokerRequestExecutorTests extends
042: TestCase {
043: //~ Constructors ===================================================================================================
044:
045: public AuthenticationSimpleHttpInvokerRequestExecutorTests() {
046: super ();
047: }
048:
049: public AuthenticationSimpleHttpInvokerRequestExecutorTests(
050: String arg0) {
051: super (arg0);
052: }
053:
054: //~ Methods ========================================================================================================
055:
056: public static void main(String[] args) {
057: junit.textui.TestRunner
058: .run(AuthenticationSimpleHttpInvokerRequestExecutorTests.class);
059: }
060:
061: public void testNormalOperation() throws Exception {
062: // Setup client-side context
063: Authentication clientSideAuthentication = new UsernamePasswordAuthenticationToken(
064: "Aladdin", "open sesame");
065: SecurityContextHolder.getContext().setAuthentication(
066: clientSideAuthentication);
067:
068: // Create a connection and ensure our executor sets its
069: // properties correctly
070: AuthenticationSimpleHttpInvokerRequestExecutor executor = new AuthenticationSimpleHttpInvokerRequestExecutor();
071: HttpURLConnection conn = new MockHttpURLConnection(new URL(
072: "http://localhost/"));
073: executor.prepareConnection(conn, 10);
074:
075: // Check connection properties
076: // See http://www.faqs.org/rfcs/rfc1945.html section 11.1 for example
077: // we are comparing against
078: assertEquals("Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==", conn
079: .getRequestProperty("Authorization"));
080:
081: SecurityContextHolder.getContext().setAuthentication(null);
082: }
083:
084: public void testNullContextHolderIsNull() throws Exception {
085: SecurityContextHolder.getContext().setAuthentication(null);
086:
087: // Create a connection and ensure our executor sets its
088: // properties correctly
089: AuthenticationSimpleHttpInvokerRequestExecutor executor = new AuthenticationSimpleHttpInvokerRequestExecutor();
090: HttpURLConnection conn = new MockHttpURLConnection(new URL(
091: "http://localhost/"));
092: executor.prepareConnection(conn, 10);
093:
094: // Check connection properties (shouldn't be an Authorization header)
095: assertNull(conn.getRequestProperty("Authorization"));
096: }
097:
098: //~ Inner Classes ==================================================================================================
099:
100: private class MockHttpURLConnection extends HttpURLConnection {
101: private Map requestProperties = new HashMap();
102:
103: public MockHttpURLConnection(URL u) {
104: super (u);
105: }
106:
107: public void connect() throws IOException {
108: throw new UnsupportedOperationException(
109: "mock not implemented");
110: }
111:
112: public void disconnect() {
113: throw new UnsupportedOperationException(
114: "mock not implemented");
115: }
116:
117: public String getRequestProperty(String key) {
118: return (String) requestProperties.get(key);
119: }
120:
121: public void setRequestProperty(String key, String value) {
122: requestProperties.put(key, value);
123: }
124:
125: public boolean usingProxy() {
126: throw new UnsupportedOperationException(
127: "mock not implemented");
128: }
129: }
130: }
|