1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/TestHttpState.java $
3    * $Revision: 1425331 $
4    * $Date: 2012-12-22 18:29:41 +0000 (Sat, 22 Dec 2012) $
5    * ====================================================================
6    *
7    *  Licensed to the Apache Software Foundation (ASF) under one or more
8    *  contributor license agreements.  See the NOTICE file distributed with
9    *  this work for additional information regarding copyright ownership.
10   *  The ASF licenses this file to You under the Apache License, Version 2.0
11   *  (the "License"); you may not use this file except in compliance with
12   *  the License.  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   * [Additional notices, if required by prior licensing conditions]
29   *
30   */
31  
32  package org.apache.commons.httpclient;
33  
34  import org.apache.commons.httpclient.auth.AuthScope;
35  
36  import junit.framework.*;
37  
38  /***
39   * 
40   * Simple tests for {@link HttpState}.
41   *
42   * @author Rodney Waldhoff
43   * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
44   * @author Sean C. Sullivan
45   * @author Oleg Kalnichevski
46   * 
47   * @version $Id: TestHttpState.java 608014 2008-01-02 05:48:53Z rolandw $
48   * 
49   */
50  public class TestHttpState extends TestCase {
51  
52      public final static Credentials CREDS1 = 
53          new UsernamePasswordCredentials("user1", "pass1");
54      public final static Credentials CREDS2 = 
55          new UsernamePasswordCredentials("user2", "pass2");
56  
57      public final static AuthScope SCOPE1 = 
58          new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm1");
59      public final static AuthScope SCOPE2 = 
60          new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm2");
61      public final static AuthScope BOGUS = 
62          new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "bogus");
63      public final static AuthScope DEFSCOPE = 
64          new AuthScope("host", AuthScope.ANY_PORT, "realm");
65  
66  
67      // ------------------------------------------------------------ Constructor
68      public TestHttpState(String testName) {
69          super(testName);
70      }
71  
72      // ------------------------------------------------------------------- Main
73      public static void main(String args[]) {
74          String[] testCaseName = { TestHttpState.class.getName() };
75          junit.textui.TestRunner.main(testCaseName);
76      }
77  
78      // ------------------------------------------------------- TestCase Methods
79  
80      public static Test suite() {
81          return new TestSuite(TestHttpState.class);
82      }
83  
84  
85      // ----------------------------------------------------------- Test Methods
86  
87      public void testHttpStateCredentials() {
88          HttpState state = new HttpState();
89          state.setCredentials(SCOPE1, CREDS1);
90          state.setCredentials(SCOPE2, CREDS2);
91          assertEquals(CREDS1, state.getCredentials(SCOPE1));
92          assertEquals(CREDS2, state.getCredentials(SCOPE2));
93      }
94  
95  	public void testToString()
96  	{
97          HttpState state = new HttpState();
98          assertNotNull(state.toString());
99          
100         state.addCookie(new Cookie("foo", "bar", "yeah"));
101         assertNotNull(state.toString());
102 
103         state.addCookie(new Cookie("flub", "duck", "yuck"));
104         assertNotNull(state.toString());
105 
106 		state.setCredentials(SCOPE1, CREDS1);
107         assertNotNull(state.toString());
108         
109 		state.setProxyCredentials(SCOPE2, CREDS2);
110         assertNotNull(state.toString());
111 	}
112 
113     public void testHttpStateNoCredentials() {
114         HttpState state = new HttpState();
115         assertEquals(null, state.getCredentials(BOGUS));
116     }
117 
118     public void testHttpStateDefaultCredentials() {
119         HttpState state = new HttpState();
120 	    state.setCredentials(AuthScope.ANY, CREDS1);
121 	    state.setCredentials(SCOPE2, CREDS2);
122         assertEquals(CREDS1, state.getCredentials(BOGUS));
123     }
124 
125     public void testHttpStateProxyCredentials() {
126         HttpState state = new HttpState();
127         state.setProxyCredentials(SCOPE1, CREDS1);
128         state.setProxyCredentials(SCOPE2, CREDS2);
129         assertEquals(CREDS1, state.getProxyCredentials(SCOPE1));
130         assertEquals(CREDS2, state.getProxyCredentials(SCOPE2));
131     }
132 
133     public void testHttpStateProxyNoCredentials() {
134         HttpState state = new HttpState();
135         assertEquals(null, state.getProxyCredentials(BOGUS));
136     }
137 
138     public void testHttpStateProxyDefaultCredentials() {
139         HttpState state = new HttpState();
140 	    state.setProxyCredentials(AuthScope.ANY, CREDS1);
141 	    state.setProxyCredentials(SCOPE2, CREDS2);
142         assertEquals(CREDS1, state.getProxyCredentials(BOGUS));
143     }
144 
145     // --------------------------------- Test Methods for Selecting Credentials
146     
147     public void testDefaultCredentials() throws Exception {
148         HttpState state = new HttpState();
149         Credentials expected = new UsernamePasswordCredentials("name", "pass");
150         state.setCredentials(AuthScope.ANY, expected);
151         Credentials got = state.getCredentials(DEFSCOPE);
152         assertEquals(got, expected);
153     }
154     
155     public void testRealmCredentials() throws Exception {
156         HttpState state = new HttpState();
157         Credentials expected = new UsernamePasswordCredentials("name", "pass");
158         state.setCredentials(DEFSCOPE, expected);
159         Credentials got = state.getCredentials(DEFSCOPE);
160         assertEquals(expected, got);
161     }
162     
163     public void testHostCredentials() throws Exception {
164         HttpState state = new HttpState();
165         Credentials expected = new UsernamePasswordCredentials("name", "pass");
166         state.setCredentials(
167             new AuthScope("host", AuthScope.ANY_PORT, 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", "pass");
175         state.setCredentials(
176             new AuthScope("host1", AuthScope.ANY_PORT, "realm"), expected);
177         Credentials got = state.getCredentials(
178             new AuthScope("host2", AuthScope.ANY_PORT, "realm"));
179         assertNotSame(expected, got);
180     }
181     
182     public void testWrongRealmCredentials() throws Exception {
183         HttpState state = new HttpState();
184         Credentials cred = new UsernamePasswordCredentials("name", "pass");
185         state.setCredentials(
186             new AuthScope("host", AuthScope.ANY_PORT, "realm1"), cred);
187         Credentials got = state.getCredentials(
188             new AuthScope("host", AuthScope.ANY_PORT, "realm2"));
189         assertNotSame(cred, got);
190     }
191 
192     // ------------------------------- Test Methods for matching Credentials
193     
194     public void testScopeMatching() {
195         AuthScope authscope1 = new AuthScope("somehost", 80, "somerealm", "somescheme");
196         AuthScope authscope2 = new AuthScope("someotherhost", 80, "somerealm", "somescheme");
197         assertTrue(authscope1.match(authscope2) < 0);
198 
199         int m1 = authscope1.match(
200             new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, "somescheme"));
201         int m2 = authscope1.match(
202             new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "somerealm", AuthScope.ANY_SCHEME));
203         assertTrue(m2 > m1);
204 
205         m1 = authscope1.match(
206             new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, "somescheme"));
207         m2 = authscope1.match(
208             new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "somerealm", AuthScope.ANY_SCHEME));
209         assertTrue(m2 > m1);
210 
211         m1 = authscope1.match(
212             new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "somerealm", "somescheme"));
213         m2 = authscope1.match(
214             new AuthScope(AuthScope.ANY_HOST, 80, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME));
215         assertTrue(m2 > m1);
216 
217         m1 = authscope1.match(
218             new AuthScope(AuthScope.ANY_HOST, 80, "somerealm", "somescheme"));
219         m2 = authscope1.match(
220             new AuthScope("somehost", AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME));
221         assertTrue(m2 > m1);
222 
223         m1 = authscope1.match(AuthScope.ANY);
224         m2 = authscope1.match(
225             new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, "somescheme"));
226         assertTrue(m2 > m1);
227     }
228     
229     public void testCredentialsMatching() {
230         Credentials creds1 = new UsernamePasswordCredentials("name1", "pass1");
231         Credentials creds2 = new UsernamePasswordCredentials("name2", "pass2");
232         Credentials creds3 = new UsernamePasswordCredentials("name3", "pass3");
233         
234         AuthScope scope1 = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
235         AuthScope scope2 = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "somerealm");
236         AuthScope scope3 = new AuthScope("somehost", AuthScope.ANY_PORT, AuthScope.ANY_REALM);
237         
238         HttpState state = new HttpState();
239         state.setCredentials(scope1, creds1);
240         state.setCredentials(scope2, creds2);
241         state.setCredentials(scope3, creds3);
242 
243         Credentials got = state.getCredentials(
244             new AuthScope("someotherhost", 80, "someotherrealm", "basic"));
245         Credentials expected = creds1;
246         assertEquals(expected, got);
247 
248         got = state.getCredentials(
249             new AuthScope("someotherhost", 80, "somerealm", "basic"));
250         expected = creds2;
251         assertEquals(expected, got);
252 
253         got = state.getCredentials(
254             new AuthScope("somehost", 80, "someotherrealm", "basic"));
255         expected = creds3;
256         assertEquals(expected, got);
257     }
258 }