1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package org.apache.commons.httpclient;
32
33 import org.apache.commons.httpclient.auth.AuthScope;
34
35 import junit.framework.*;
36
37 /***
38 *
39 * Simple tests for {@link HttpState}.
40 *
41 * @author Rodney Waldhoff
42 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
43 * @author Sean C. Sullivan
44 * @author Oleg Kalnichevski
45 *
46 * @version $Id: TestHttpState.java 155418 2005-02-26 13:01:52Z dirkv $
47 *
48 */
49 public class TestHttpState extends TestCase {
50
51 public final static Credentials CREDS1 =
52 new UsernamePasswordCredentials("user1", "pass1");
53 public final static Credentials CREDS2 =
54 new UsernamePasswordCredentials("user2", "pass2");
55
56 public final static AuthScope SCOPE1 =
57 new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm1");
58 public final static AuthScope SCOPE2 =
59 new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm2");
60 public final static AuthScope BOGUS =
61 new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "bogus");
62 public final static AuthScope DEFSCOPE =
63 new AuthScope("host", AuthScope.ANY_PORT, "realm");
64
65
66
67 public TestHttpState(String testName) {
68 super(testName);
69 }
70
71
72 public static void main(String args[]) {
73 String[] testCaseName = { TestHttpState.class.getName() };
74 junit.textui.TestRunner.main(testCaseName);
75 }
76
77
78
79 public static Test suite() {
80 return new TestSuite(TestHttpState.class);
81 }
82
83
84
85
86 public void testHttpStateCredentials() {
87 HttpState state = new HttpState();
88 state.setCredentials(SCOPE1, CREDS1);
89 state.setCredentials(SCOPE2, CREDS2);
90 assertEquals(CREDS1, state.getCredentials(SCOPE1));
91 assertEquals(CREDS2, state.getCredentials(SCOPE2));
92 }
93
94 public void testToString()
95 {
96 HttpState state = new HttpState();
97 assertNotNull(state.toString());
98
99 state.addCookie(new Cookie("foo", "bar", "yeah"));
100 assertNotNull(state.toString());
101
102 state.addCookie(new Cookie("flub", "duck", "yuck"));
103 assertNotNull(state.toString());
104
105 state.setCredentials(SCOPE1, CREDS1);
106 assertNotNull(state.toString());
107
108 state.setProxyCredentials(SCOPE2, CREDS2);
109 assertNotNull(state.toString());
110 }
111
112 public void testHttpStateNoCredentials() {
113 HttpState state = new HttpState();
114 assertEquals(null, state.getCredentials(BOGUS));
115 }
116
117 public void testHttpStateDefaultCredentials() {
118 HttpState state = new HttpState();
119 state.setCredentials(AuthScope.ANY, CREDS1);
120 state.setCredentials(SCOPE2, CREDS2);
121 assertEquals(CREDS1, state.getCredentials(BOGUS));
122 }
123
124 public void testHttpStateProxyCredentials() {
125 HttpState state = new HttpState();
126 state.setProxyCredentials(SCOPE1, CREDS1);
127 state.setProxyCredentials(SCOPE2, CREDS2);
128 assertEquals(CREDS1, state.getProxyCredentials(SCOPE1));
129 assertEquals(CREDS2, state.getProxyCredentials(SCOPE2));
130 }
131
132 public void testHttpStateProxyNoCredentials() {
133 HttpState state = new HttpState();
134 assertEquals(null, state.getProxyCredentials(BOGUS));
135 }
136
137 public void testHttpStateProxyDefaultCredentials() {
138 HttpState state = new HttpState();
139 state.setProxyCredentials(AuthScope.ANY, CREDS1);
140 state.setProxyCredentials(SCOPE2, CREDS2);
141 assertEquals(CREDS1, state.getProxyCredentials(BOGUS));
142 }
143
144
145
146 public void testDefaultCredentials() throws Exception {
147 HttpState state = new HttpState();
148 Credentials expected = new UsernamePasswordCredentials("name", "pass");
149 state.setCredentials(AuthScope.ANY, expected);
150 Credentials got = state.getCredentials(DEFSCOPE);
151 assertEquals(got, expected);
152 }
153
154 public void testRealmCredentials() throws Exception {
155 HttpState state = new HttpState();
156 Credentials expected = new UsernamePasswordCredentials("name", "pass");
157 state.setCredentials(DEFSCOPE, expected);
158 Credentials got = state.getCredentials(DEFSCOPE);
159 assertEquals(expected, got);
160 }
161
162 public void testHostCredentials() throws Exception {
163 HttpState state = new HttpState();
164 Credentials expected = new UsernamePasswordCredentials("name", "pass");
165 state.setCredentials(
166 new AuthScope("host", AuthScope.ANY_PORT, AuthScope.ANY_REALM), expected);
167 Credentials got = state.getCredentials(DEFSCOPE);
168 assertEquals(expected, got);
169 }
170
171 public void testWrongHostCredentials() throws Exception {
172 HttpState state = new HttpState();
173 Credentials expected = new UsernamePasswordCredentials("name", "pass");
174 state.setCredentials(
175 new AuthScope("host1", AuthScope.ANY_PORT, "realm"), expected);
176 Credentials got = state.getCredentials(
177 new AuthScope("host2", AuthScope.ANY_PORT, "realm"));
178 assertNotSame(expected, got);
179 }
180
181 public void testWrongRealmCredentials() throws Exception {
182 HttpState state = new HttpState();
183 Credentials cred = new UsernamePasswordCredentials("name", "pass");
184 state.setCredentials(
185 new AuthScope("host", AuthScope.ANY_PORT, "realm1"), cred);
186 Credentials got = state.getCredentials(
187 new AuthScope("host", AuthScope.ANY_PORT, "realm2"));
188 assertNotSame(cred, got);
189 }
190
191
192
193 public void testScopeMatching() {
194 AuthScope authscope1 = new AuthScope("somehost", 80, "somerealm", "somescheme");
195 AuthScope authscope2 = new AuthScope("someotherhost", 80, "somerealm", "somescheme");
196 assertTrue(authscope1.match(authscope2) < 0);
197
198 int m1 = authscope1.match(
199 new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, "somescheme"));
200 int m2 = authscope1.match(
201 new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "somerealm", AuthScope.ANY_SCHEME));
202 assertTrue(m2 > m1);
203
204 m1 = authscope1.match(
205 new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, "somescheme"));
206 m2 = authscope1.match(
207 new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "somerealm", AuthScope.ANY_SCHEME));
208 assertTrue(m2 > m1);
209
210 m1 = authscope1.match(
211 new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "somerealm", "somescheme"));
212 m2 = authscope1.match(
213 new AuthScope(AuthScope.ANY_HOST, 80, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME));
214 assertTrue(m2 > m1);
215
216 m1 = authscope1.match(
217 new AuthScope(AuthScope.ANY_HOST, 80, "somerealm", "somescheme"));
218 m2 = authscope1.match(
219 new AuthScope("somehost", AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME));
220 assertTrue(m2 > m1);
221
222 m1 = authscope1.match(AuthScope.ANY);
223 m2 = authscope1.match(
224 new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, "somescheme"));
225 assertTrue(m2 > m1);
226 }
227
228 public void testCredentialsMatching() {
229 Credentials creds1 = new UsernamePasswordCredentials("name1", "pass1");
230 Credentials creds2 = new UsernamePasswordCredentials("name2", "pass2");
231 Credentials creds3 = new UsernamePasswordCredentials("name3", "pass3");
232
233 AuthScope scope1 = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
234 AuthScope scope2 = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "somerealm");
235 AuthScope scope3 = new AuthScope("somehost", AuthScope.ANY_PORT, AuthScope.ANY_REALM);
236
237 HttpState state = new HttpState();
238 state.setCredentials(scope1, creds1);
239 state.setCredentials(scope2, creds2);
240 state.setCredentials(scope3, creds3);
241
242 Credentials got = state.getCredentials(
243 new AuthScope("someotherhost", 80, "someotherrealm", "basic"));
244 Credentials expected = creds1;
245 assertEquals(expected, got);
246
247 got = state.getCredentials(
248 new AuthScope("someotherhost", 80, "somerealm", "basic"));
249 expected = creds2;
250 assertEquals(expected, got);
251
252 got = state.getCredentials(
253 new AuthScope("somehost", 80, "someotherrealm", "basic"));
254 expected = creds3;
255 assertEquals(expected, got);
256 }
257 }