KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > auth > TestChallengeProcessor


1 /*
2  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/auth/TestChallengeProcessor.java,v 1.1 2004/03/25 20:37:20 olegk Exp $
3  * $Revision: 480424 $
4  * $Date: 2006-11-29 05:56:49 +0000 (Wed, 29 Nov 2006) $
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.auth;
33
34 import java.util.ArrayList JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Map JavaDoc;
38
39 import junit.framework.Test;
40 import junit.framework.TestCase;
41 import junit.framework.TestSuite;
42
43 import org.apache.commons.httpclient.params.DefaultHttpParams;
44 import org.apache.commons.httpclient.params.HttpParams;
45
46 /**
47  * Unit tests for {@link testParsingChallenge}.
48  *
49  * @author <a HREF="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
50  */

51 public class TestChallengeProcessor extends TestCase {
52
53     // ------------------------------------------------------------ Constructor
54
public TestChallengeProcessor(String JavaDoc testName) {
55         super(testName);
56     }
57
58     // ------------------------------------------------------------------- Main
59
public static void main(String JavaDoc args[]) {
60         String JavaDoc[] testCaseName = { TestChallengeProcessor.class.getName() };
61         junit.textui.TestRunner.main(testCaseName);
62     }
63
64     // ------------------------------------------------------- TestCase Methods
65

66     public static Test suite() {
67         return new TestSuite(TestChallengeProcessor.class);
68     }
69
70
71     public void testChallengeSelection() throws Exception JavaDoc {
72         List JavaDoc authPrefs = new ArrayList JavaDoc(3);
73         authPrefs.add(AuthPolicy.NTLM);
74         authPrefs.add(AuthPolicy.DIGEST);
75         authPrefs.add(AuthPolicy.BASIC);
76         HttpParams httpparams = new DefaultHttpParams();
77         httpparams.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
78         
79         AuthChallengeProcessor processor = new AuthChallengeProcessor(httpparams);
80
81         Map JavaDoc map = new HashMap JavaDoc();
82         map.put("unknown", "unknown realm=\"whatever\"");
83         map.put("basic", "basic realm=\"whatever\"");
84         
85         AuthScheme authscheme = processor.selectAuthScheme(map);
86         assertTrue(authscheme instanceof BasicScheme);
87     }
88
89
90     public void testInvalidChallenge() throws Exception JavaDoc {
91         List JavaDoc authPrefs = new ArrayList JavaDoc(3);
92         authPrefs.add("unsupported1");
93         authPrefs.add("unsupported2");
94         HttpParams httpparams = new DefaultHttpParams();
95         httpparams.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
96         
97         AuthChallengeProcessor processor = new AuthChallengeProcessor(httpparams);
98
99         Map JavaDoc map = new HashMap JavaDoc();
100         map.put("unsupported1", "unsupported1 realm=\"whatever\"");
101         map.put("unsupported2", "unsupported2 realm=\"whatever\"");
102         try {
103             AuthScheme authscheme = processor.selectAuthScheme(map);
104             fail("AuthChallengeException should have been thrown");
105         } catch (AuthChallengeException e) {
106             //ignore
107
}
108     }
109
110
111     public void testUnsupportedChallenge() throws Exception JavaDoc {
112         List JavaDoc authPrefs = new ArrayList JavaDoc(3);
113         authPrefs.add(AuthPolicy.NTLM);
114         authPrefs.add(AuthPolicy.BASIC);
115         authPrefs.add(AuthPolicy.DIGEST);
116         HttpParams httpparams = new DefaultHttpParams();
117         httpparams.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
118         
119         AuthChallengeProcessor processor = new AuthChallengeProcessor(httpparams);
120
121         Map JavaDoc map = new HashMap JavaDoc();
122         map.put("unsupported1", "unsupported1 realm=\"whatever\"");
123         map.put("unsupported2", "unsupported2 realm=\"whatever\"");
124         
125         try {
126             AuthScheme authscheme = processor.selectAuthScheme(map);
127             fail("AuthChallengeException should have been thrown");
128         } catch (AuthChallengeException e) {
129             //expected
130
}
131     }
132
133     public void testChallengeProcessing() throws Exception JavaDoc {
134         HttpParams httpparams = new DefaultHttpParams();
135         AuthChallengeProcessor processor = new AuthChallengeProcessor(httpparams);
136
137         Map JavaDoc map = new HashMap JavaDoc();
138         map.put("basic", "basic realm=\"whatever\", param=\"value\"");
139         
140         AuthState authstate = new AuthState();
141         
142         AuthScheme authscheme = processor.processChallenge(authstate, map);
143         assertTrue(authscheme instanceof BasicScheme);
144         assertEquals("whatever", authscheme.getRealm());
145         assertEquals(authscheme, authstate.getAuthScheme());
146         assertEquals("value", authscheme.getParameter("param"));
147     }
148
149     public void testInvalidChallengeProcessing() throws Exception JavaDoc {
150         HttpParams httpparams = new DefaultHttpParams();
151         AuthChallengeProcessor processor = new AuthChallengeProcessor(httpparams);
152
153         Map JavaDoc map = new HashMap JavaDoc();
154         map.put("basic", "basic realm=\"whatever\", param=\"value\"");
155         
156         AuthState authstate = new AuthState();
157         
158         AuthScheme authscheme = processor.processChallenge(authstate, map);
159         assertTrue(authscheme instanceof BasicScheme);
160         assertEquals("whatever", authscheme.getRealm());
161         assertEquals(authscheme, authstate.getAuthScheme());
162         assertEquals("value", authscheme.getParameter("param"));
163
164         Map JavaDoc map2 = new HashMap JavaDoc();
165         map2.put("ntlm", "NTLM");
166         try {
167             // Basic authentication scheme expected
168
authscheme = processor.processChallenge(authstate, map2);
169             fail("AuthenticationException should have been thrown");
170         } catch (AuthenticationException e) {
171             //expected
172
}
173     }
174 }
175
Popular Tags