KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > TestWebappBasicAuth


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappBasicAuth.java,v 1.12.2.1 2004/02/22 18:21:16 olegk Exp $
3  * $Revision: 1.12.2.1 $
4  * $Date: 2004/02/22 18:21:16 $
5  * ====================================================================
6  *
7  * Copyright 1999-2004 The Apache Software Foundation
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ====================================================================
21  *
22  * This software consists of voluntary contributions made by many
23  * individuals on behalf of the Apache Software Foundation. For more
24  * information on the Apache Software Foundation, please see
25  * <http://www.apache.org/>.
26  *
27  * [Additional notices, if required by prior licensing conditions]
28  *
29  */

30
31 package org.apache.commons.httpclient;
32
33 import junit.framework.Test;
34 import junit.framework.TestSuite;
35
36 import org.apache.commons.httpclient.methods.GetMethod;
37 import org.apache.commons.httpclient.methods.HeadMethod;
38 import org.apache.commons.httpclient.methods.PostMethod;
39 import org.apache.commons.httpclient.methods.PutMethod;
40
41 /**
42  * This suite of tests depends upon the httpclienttest webapp,
43  * which is available in the httpclient/src/test-webapp
44  * directory in the CVS tree.
45  * <p>
46  * The webapp should be deployed in the context "httpclienttest"
47  * on a servlet engine running on port 8080 on the localhost
48  * (IP 127.0.0.1).
49  * <p>
50  * You can change the assumed port by setting the
51  * "httpclient.test.localPort" property.
52  * You can change the assumed host by setting the
53  * "httpclient.test.localHost" property.
54  * You can change the assumed context by setting the
55  * "httpclient.test.webappContext" property.
56  *
57  * @author Rodney Waldhoff
58  * @version $Id: TestWebappBasicAuth.java,v 1.12.2.1 2004/02/22 18:21:16 olegk Exp $
59  */

60 public class TestWebappBasicAuth extends TestWebappBase {
61
62     public TestWebappBasicAuth(String JavaDoc testName) {
63         super(testName);
64     }
65
66     public static Test suite() {
67         TestSuite suite = new TestSuite(TestWebappBasicAuth.class);
68         return suite;
69     }
70
71     public static void main(String JavaDoc args[]) {
72         String JavaDoc[] testCaseName = { TestWebappBasicAuth.class.getName() };
73         junit.textui.TestRunner.main(testCaseName);
74     }
75
76     // ------------------------------------------------------------------ Tests
77

78     public void testSimpleAuthGet() throws Exception JavaDoc {
79         HttpClient client = createHttpClient();
80         client.getState().setCredentials("BasicAuthServlet",new UsernamePasswordCredentials("jakarta","commons"));
81         GetMethod method = new GetMethod("/" + getWebappContext() + "/auth/basic");
82         
83         try {
84             client.executeMethod(method);
85         } catch (Throwable JavaDoc t) {
86             t.printStackTrace();
87             fail("Unable to execute method : " + t.toString());
88         }
89         assertEquals(200,method.getStatusCode());
90         assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0);
91         assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0);
92
93         method.recycle();
94         method.setPath("/" + getWebappContext() + "/auth/basic");
95         try {
96             client.executeMethod(method);
97         } catch (Throwable JavaDoc t) {
98             t.printStackTrace();
99             fail("Unable to execute method : " + t.toString());
100         }
101         assertEquals(200,method.getStatusCode());
102         assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0);
103         assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0);
104     }
105
106     public void testSimpleAuthPost() throws Exception JavaDoc {
107         HttpClient client = createHttpClient();
108         client.getState().setCredentials("BasicAuthServlet",new UsernamePasswordCredentials("jakarta","commons"));
109         PostMethod method = new PostMethod("/" + getWebappContext() + "/auth/basic");
110         method.setRequestBody(new NameValuePair[] { new NameValuePair("testing","one") } );
111         
112         try {
113             client.executeMethod(method);
114         } catch (Throwable JavaDoc t) {
115             t.printStackTrace();
116             fail("Unable to execute method : " + t.toString());
117         }
118         assertEquals(200,method.getStatusCode());
119         assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: POST</title>") >= 0);
120         assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0);
121
122         method.recycle();
123         method.setPath("/" + getWebappContext() + "/auth/basic");
124         method.setRequestBody(new NameValuePair[] { new NameValuePair("testing","one") } );
125         try {
126             client.executeMethod(method);
127         } catch (Throwable JavaDoc t) {
128             t.printStackTrace();
129             fail("Unable to execute method : " + t.toString());
130         }
131         assertEquals(200,method.getStatusCode());
132         assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: POST</title>") >= 0);
133         assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0);
134     }
135
136     public void testSimpleAuthPut() throws Exception JavaDoc {
137         HttpClient client = createHttpClient();
138         client.getState().setCredentials("BasicAuthServlet",new UsernamePasswordCredentials("jakarta","commons"));
139         PutMethod method = new PutMethod("/" + getWebappContext() + "/auth/basic");
140         method.setRequestBody("testing one two three");
141         try {
142             client.executeMethod(method);
143         } catch (Throwable JavaDoc t) {
144             t.printStackTrace();
145             fail("Unable to execute method : " + t.toString());
146         }
147         assertEquals(200,method.getStatusCode());
148         assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: PUT</title>") >= 0);
149         assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0);
150
151         method.recycle();
152         method.setPath("/" + getWebappContext() + "/auth/basic");
153         try {
154             client.executeMethod(method);
155         } catch (Throwable JavaDoc t) {
156             t.printStackTrace();
157             fail("Unable to execute method : " + t.toString());
158         }
159         assertEquals(200,method.getStatusCode());
160         assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: PUT</title>") >= 0);
161         assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0);
162     }
163
164     public void testNoCredAuthRetry() throws Exception JavaDoc {
165         HttpClient client = createHttpClient();
166         GetMethod method = new GetMethod("/" + getWebappContext() + "/auth/basic");
167         
168         try {
169             client.executeMethod(method);
170         } catch (Throwable JavaDoc t) {
171             t.printStackTrace();
172             fail("Unable to execute method : " + t.toString());
173         }
174         assertEquals(401,method.getStatusCode());
175         assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0);
176         assertTrue(method.getResponseBodyAsString().indexOf("<p>Not authorized.</p>") >= 0);
177
178         client.getState().setCredentials("BasicAuthServlet",new UsernamePasswordCredentials("jakarta","commons"));
179
180         method.recycle();
181         method.setPath("/" + getWebappContext() + "/auth/basic");
182         try {
183             client.executeMethod(method);
184         } catch (Throwable JavaDoc t) {
185             t.printStackTrace();
186             fail("Unable to execute method : " + t.toString());
187         }
188         assertEquals(200,method.getStatusCode());
189         assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0);
190         assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0);
191     }
192
193     public void testBadCredFails() throws Exception JavaDoc {
194         HttpClient client = createHttpClient();
195         GetMethod method = new GetMethod("/" + getWebappContext() + "/auth/basic");
196         
197         try {
198             client.executeMethod(method);
199         } catch (Throwable JavaDoc t) {
200             t.printStackTrace();
201             fail("Unable to execute method : " + t.toString());
202         }
203         assertEquals(HttpStatus.SC_UNAUTHORIZED,method.getStatusCode());
204         assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0);
205         assertTrue(method.getResponseBodyAsString().indexOf("<p>Not authorized.</p>") >= 0);
206
207         client.getState().setCredentials("BasicAuthServlet",new UsernamePasswordCredentials("bad","creds"));
208
209         method.recycle();
210         method.setPath("/" + getWebappContext() + "/auth/basic");
211         try {
212             client.executeMethod(method);
213         } catch (Throwable JavaDoc t) {
214             t.printStackTrace();
215             fail("Unable to execute method : " + t.toString());
216         }
217         assertEquals(HttpStatus.SC_UNAUTHORIZED,method.getStatusCode());
218         assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0);
219         assertTrue(method.getResponseBodyAsString().indexOf("<p>Not authorized. \"Basic YmFkOmNyZWRz\" not recognized.</p>") >= 0);
220     }
221     
222     public void testHeadAuth() throws Exception JavaDoc {
223         HttpClient client = new HttpClient();
224         HttpState state = client.getState();
225         Credentials cred = new UsernamePasswordCredentials("jakarta", "commons");
226         state.setCredentials(null, cred);
227         HostConfiguration hc = new HostConfiguration();
228         hc.setHost(getHost(), getPort(), getProtocol());
229         client.setHostConfiguration(hc);
230         client.setState(state);
231         HeadMethod method = new HeadMethod("/"+ getWebappContext() +"/auth/basic");
232         client.executeMethod(method);
233         method.releaseConnection();
234         assertEquals(200, method.getStatusCode());
235     }
236     
237 }
238
239
240
241
242
Popular Tags