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