001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestHttpState.java,v 1.7 2004/06/23 06:50:25 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: * [Additional notices, if required by prior licensing conditions]
029: *
030: */
031:
032: package org.apache.commons.httpclient;
033:
034: import org.apache.commons.httpclient.auth.AuthScope;
035:
036: import junit.framework.*;
037:
038: /**
039: *
040: * Simple tests for {@link HttpState}.
041: *
042: * @author Rodney Waldhoff
043: * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
044: * @author Sean C. Sullivan
045: * @author Oleg Kalnichevski
046: *
047: * @version $Id: TestHttpState.java 480424 2006-11-29 05:56:49Z bayard $
048: *
049: */
050: public class TestHttpState extends TestCase {
051:
052: public final static Credentials CREDS1 = new UsernamePasswordCredentials(
053: "user1", "pass1");
054: public final static Credentials CREDS2 = new UsernamePasswordCredentials(
055: "user2", "pass2");
056:
057: public final static AuthScope SCOPE1 = new AuthScope(
058: AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm1");
059: public final static AuthScope SCOPE2 = new AuthScope(
060: AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm2");
061: public final static AuthScope BOGUS = new AuthScope(
062: AuthScope.ANY_HOST, AuthScope.ANY_PORT, "bogus");
063: public final static AuthScope DEFSCOPE = new AuthScope("host",
064: AuthScope.ANY_PORT, "realm");
065:
066: // ------------------------------------------------------------ Constructor
067: public TestHttpState(String testName) {
068: super (testName);
069: }
070:
071: // ------------------------------------------------------------------- Main
072: public static void main(String args[]) {
073: String[] testCaseName = { TestHttpState.class.getName() };
074: junit.textui.TestRunner.main(testCaseName);
075: }
076:
077: // ------------------------------------------------------- TestCase Methods
078:
079: public static Test suite() {
080: return new TestSuite(TestHttpState.class);
081: }
082:
083: // ----------------------------------------------------------- Test Methods
084:
085: public void testHttpStateCredentials() {
086: HttpState state = new HttpState();
087: state.setCredentials(SCOPE1, CREDS1);
088: state.setCredentials(SCOPE2, CREDS2);
089: assertEquals(CREDS1, state.getCredentials(SCOPE1));
090: assertEquals(CREDS2, state.getCredentials(SCOPE2));
091: }
092:
093: public void testToString() {
094: HttpState state = new HttpState();
095: assertNotNull(state.toString());
096:
097: state.addCookie(new Cookie("foo", "bar", "yeah"));
098: assertNotNull(state.toString());
099:
100: state.addCookie(new Cookie("flub", "duck", "yuck"));
101: assertNotNull(state.toString());
102:
103: state.setCredentials(SCOPE1, CREDS1);
104: assertNotNull(state.toString());
105:
106: state.setProxyCredentials(SCOPE2, CREDS2);
107: assertNotNull(state.toString());
108: }
109:
110: public void testHttpStateNoCredentials() {
111: HttpState state = new HttpState();
112: assertEquals(null, state.getCredentials(BOGUS));
113: }
114:
115: public void testHttpStateDefaultCredentials() {
116: HttpState state = new HttpState();
117: state.setCredentials(AuthScope.ANY, CREDS1);
118: state.setCredentials(SCOPE2, CREDS2);
119: assertEquals(CREDS1, state.getCredentials(BOGUS));
120: }
121:
122: public void testHttpStateProxyCredentials() {
123: HttpState state = new HttpState();
124: state.setProxyCredentials(SCOPE1, CREDS1);
125: state.setProxyCredentials(SCOPE2, CREDS2);
126: assertEquals(CREDS1, state.getProxyCredentials(SCOPE1));
127: assertEquals(CREDS2, state.getProxyCredentials(SCOPE2));
128: }
129:
130: public void testHttpStateProxyNoCredentials() {
131: HttpState state = new HttpState();
132: assertEquals(null, state.getProxyCredentials(BOGUS));
133: }
134:
135: public void testHttpStateProxyDefaultCredentials() {
136: HttpState state = new HttpState();
137: state.setProxyCredentials(AuthScope.ANY, CREDS1);
138: state.setProxyCredentials(SCOPE2, CREDS2);
139: assertEquals(CREDS1, state.getProxyCredentials(BOGUS));
140: }
141:
142: // --------------------------------- Test Methods for Selecting Credentials
143:
144: public void testDefaultCredentials() throws Exception {
145: HttpState state = new HttpState();
146: Credentials expected = new UsernamePasswordCredentials("name",
147: "pass");
148: state.setCredentials(AuthScope.ANY, expected);
149: Credentials got = state.getCredentials(DEFSCOPE);
150: assertEquals(got, expected);
151: }
152:
153: public void testRealmCredentials() throws Exception {
154: HttpState state = new HttpState();
155: Credentials expected = new UsernamePasswordCredentials("name",
156: "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",
165: "pass");
166: state.setCredentials(new AuthScope("host", AuthScope.ANY_PORT,
167: AuthScope.ANY_REALM), expected);
168: Credentials got = state.getCredentials(DEFSCOPE);
169: assertEquals(expected, got);
170: }
171:
172: public void testWrongHostCredentials() throws Exception {
173: HttpState state = new HttpState();
174: Credentials expected = new UsernamePasswordCredentials("name",
175: "pass");
176: state.setCredentials(new AuthScope("host1", AuthScope.ANY_PORT,
177: "realm"), expected);
178: Credentials got = state.getCredentials(new AuthScope("host2",
179: AuthScope.ANY_PORT, "realm"));
180: assertNotSame(expected, got);
181: }
182:
183: public void testWrongRealmCredentials() throws Exception {
184: HttpState state = new HttpState();
185: Credentials cred = new UsernamePasswordCredentials("name",
186: "pass");
187: state.setCredentials(new AuthScope("host", AuthScope.ANY_PORT,
188: "realm1"), cred);
189: Credentials got = state.getCredentials(new AuthScope("host",
190: AuthScope.ANY_PORT, "realm2"));
191: assertNotSame(cred, got);
192: }
193:
194: // ------------------------------- Test Methods for matching Credentials
195:
196: public void testScopeMatching() {
197: AuthScope authscope1 = new AuthScope("somehost", 80,
198: "somerealm", "somescheme");
199: AuthScope authscope2 = new AuthScope("someotherhost", 80,
200: "somerealm", "somescheme");
201: assertTrue(authscope1.match(authscope2) < 0);
202:
203: int m1 = authscope1.match(new AuthScope(AuthScope.ANY_HOST,
204: AuthScope.ANY_PORT, AuthScope.ANY_REALM, "somescheme"));
205: int m2 = authscope1.match(new AuthScope(AuthScope.ANY_HOST,
206: AuthScope.ANY_PORT, "somerealm", AuthScope.ANY_SCHEME));
207: assertTrue(m2 > m1);
208:
209: m1 = authscope1.match(new AuthScope(AuthScope.ANY_HOST,
210: AuthScope.ANY_PORT, AuthScope.ANY_REALM, "somescheme"));
211: m2 = authscope1.match(new AuthScope(AuthScope.ANY_HOST,
212: AuthScope.ANY_PORT, "somerealm", AuthScope.ANY_SCHEME));
213: assertTrue(m2 > m1);
214:
215: m1 = authscope1.match(new AuthScope(AuthScope.ANY_HOST,
216: AuthScope.ANY_PORT, "somerealm", "somescheme"));
217: m2 = authscope1.match(new AuthScope(AuthScope.ANY_HOST, 80,
218: AuthScope.ANY_REALM, AuthScope.ANY_SCHEME));
219: assertTrue(m2 > m1);
220:
221: m1 = authscope1.match(new AuthScope(AuthScope.ANY_HOST, 80,
222: "somerealm", "somescheme"));
223: m2 = authscope1.match(new AuthScope("somehost",
224: AuthScope.ANY_PORT, AuthScope.ANY_REALM,
225: AuthScope.ANY_SCHEME));
226: assertTrue(m2 > m1);
227:
228: m1 = authscope1.match(AuthScope.ANY);
229: m2 = authscope1.match(new AuthScope(AuthScope.ANY_HOST,
230: AuthScope.ANY_PORT, AuthScope.ANY_REALM, "somescheme"));
231: assertTrue(m2 > m1);
232: }
233:
234: public void testCredentialsMatching() {
235: Credentials creds1 = new UsernamePasswordCredentials("name1",
236: "pass1");
237: Credentials creds2 = new UsernamePasswordCredentials("name2",
238: "pass2");
239: Credentials creds3 = new UsernamePasswordCredentials("name3",
240: "pass3");
241:
242: AuthScope scope1 = new AuthScope(AuthScope.ANY_HOST,
243: AuthScope.ANY_PORT, AuthScope.ANY_REALM);
244: AuthScope scope2 = new AuthScope(AuthScope.ANY_HOST,
245: AuthScope.ANY_PORT, "somerealm");
246: AuthScope scope3 = new AuthScope("somehost",
247: AuthScope.ANY_PORT, AuthScope.ANY_REALM);
248:
249: HttpState state = new HttpState();
250: state.setCredentials(scope1, creds1);
251: state.setCredentials(scope2, creds2);
252: state.setCredentials(scope3, creds3);
253:
254: Credentials got = state.getCredentials(new AuthScope(
255: "someotherhost", 80, "someotherrealm", "basic"));
256: Credentials expected = creds1;
257: assertEquals(expected, got);
258:
259: got = state.getCredentials(new AuthScope("someotherhost", 80,
260: "somerealm", "basic"));
261: expected = creds2;
262: assertEquals(expected, got);
263:
264: got = state.getCredentials(new AuthScope("somehost", 80,
265: "someotherrealm", "basic"));
266: expected = creds3;
267: assertEquals(expected, got);
268: }
269: }
|