1
2
3
4
5
6
7
8
9
10
11
12
13
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
43
44 public AuthenticationSimpleHttpInvokerRequestExecutorTests() {
45 super();
46 }
47
48 public AuthenticationSimpleHttpInvokerRequestExecutorTests(String arg0) {
49 super(arg0);
50 }
51
52
53
54 public static void main(String[] args) {
55 junit.textui.TestRunner.run(AuthenticationSimpleHttpInvokerRequestExecutorTests.class);
56 }
57
58 public void testNormalOperation() throws Exception {
59
60 Authentication clientSideAuthentication = new UsernamePasswordAuthenticationToken("Aladdin",
61 "open sesame");
62 SecurityContextHolder.getContext().setAuthentication(clientSideAuthentication);
63
64
65
66 AuthenticationSimpleHttpInvokerRequestExecutor executor = new AuthenticationSimpleHttpInvokerRequestExecutor();
67 HttpURLConnection conn = new MockHttpURLConnection(new URL(
68 "http://localhost/"));
69 executor.prepareConnection(conn, 10);
70
71
72
73
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
84
85 AuthenticationSimpleHttpInvokerRequestExecutor executor = new AuthenticationSimpleHttpInvokerRequestExecutor();
86 HttpURLConnection conn = new MockHttpURLConnection(new URL(
87 "http://localhost/"));
88 executor.prepareConnection(conn, 10);
89
90
91 assertNull(conn.getRequestProperty("Authorization"));
92 }
93
94
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 }