1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappBasicAuth.java,v 1.12 2003/03/27 20:58:28 olegk Exp $
3 * $Revision: 1.12 $
4 * $Date: 2003/03/27 20:58:28 $
5 * ====================================================================
6 *
7 * The Apache Software License, Version 1.1
8 *
9 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
10 * reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
22 * distribution.
23 *
24 * 3. The end-user documentation included with the redistribution, if
25 * any, must include the following acknowlegement:
26 * "This product includes software developed by the
27 * Apache Software Foundation (http://www.apache.org/)."
28 * Alternately, this acknowlegement may appear in the software itself,
29 * if and wherever such third-party acknowlegements normally appear.
30 *
31 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
32 * Foundation" must not be used to endorse or promote products derived
33 * from this software without prior written permission. For written
34 * permission, please contact apache@apache.org.
35 *
36 * 5. Products derived from this software may not be called "Apache"
37 * nor may "Apache" appear in their names without prior written
38 * permission of the Apache Group.
39 *
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This software consists of voluntary contributions made by many
55 * individuals on behalf of the Apache Software Foundation. For more
56 * information on the Apache Software Foundation, please see
57 * <http://www.apache.org/>.
58 *
59 * [Additional notices, if required by prior licensing conditions]
60 *
61 */
62
63 package org.apache.commons.httpclient;
64
65 import junit.framework.Test;
66 import junit.framework.TestSuite;
67
68 import org.apache.commons.httpclient.methods.GetMethod;
69 import org.apache.commons.httpclient.methods.HeadMethod;
70 import org.apache.commons.httpclient.methods.PostMethod;
71 import org.apache.commons.httpclient.methods.PutMethod;
72
73 /***
74 * This suite of tests depends upon the httpclienttest webapp,
75 * which is available in the httpclient/src/test-webapp
76 * directory in the CVS tree.
77 * <p>
78 * The webapp should be deployed in the context "httpclienttest"
79 * on a servlet engine running on port 8080 on the localhost
80 * (IP 127.0.0.1).
81 * <p>
82 * You can change the assumed port by setting the
83 * "httpclient.test.localPort" property.
84 * You can change the assumed host by setting the
85 * "httpclient.test.localHost" property.
86 * You can change the assumed context by setting the
87 * "httpclient.test.webappContext" property.
88 *
89 * @author Rodney Waldhoff
90 * @version $Id: TestWebappBasicAuth.java,v 1.12 2003/03/27 20:58:28 olegk Exp $
91 */
92 public class TestWebappBasicAuth extends TestWebappBase {
93
94 public TestWebappBasicAuth(String testName) {
95 super(testName);
96 }
97
98 public static Test suite() {
99 TestSuite suite = new TestSuite(TestWebappBasicAuth.class);
100 return suite;
101 }
102
103 public static void main(String args[]) {
104 String[] testCaseName = { TestWebappBasicAuth.class.getName() };
105 junit.textui.TestRunner.main(testCaseName);
106 }
107
108 // ------------------------------------------------------------------ Tests
109
110 public void testSimpleAuthGet() throws Exception {
111 HttpClient client = createHttpClient();
112 client.getState().setCredentials("BasicAuthServlet",new UsernamePasswordCredentials("jakarta","commons"));
113 GetMethod method = new GetMethod("/" + getWebappContext() + "/auth/basic");
114
115 try {
116 client.executeMethod(method);
117 } catch (Throwable t) {
118 t.printStackTrace();
119 fail("Unable to execute method : " + t.toString());
120 }
121 assertEquals(200,method.getStatusCode());
122 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0);
123 assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0);
124
125 method.recycle();
126 method.setPath("/" + getWebappContext() + "/auth/basic");
127 try {
128 client.executeMethod(method);
129 } catch (Throwable t) {
130 t.printStackTrace();
131 fail("Unable to execute method : " + t.toString());
132 }
133 assertEquals(200,method.getStatusCode());
134 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0);
135 assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0);
136 }
137
138 public void testSimpleAuthPost() throws Exception {
139 HttpClient client = createHttpClient();
140 client.getState().setCredentials("BasicAuthServlet",new UsernamePasswordCredentials("jakarta","commons"));
141 PostMethod method = new PostMethod("/" + getWebappContext() + "/auth/basic");
142 method.setRequestBody(new NameValuePair[] { new NameValuePair("testing","one") } );
143
144 try {
145 client.executeMethod(method);
146 } catch (Throwable t) {
147 t.printStackTrace();
148 fail("Unable to execute method : " + t.toString());
149 }
150 assertEquals(200,method.getStatusCode());
151 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: POST</title>") >= 0);
152 assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0);
153
154 method.recycle();
155 method.setPath("/" + getWebappContext() + "/auth/basic");
156 method.setRequestBody(new NameValuePair[] { new NameValuePair("testing","one") } );
157 try {
158 client.executeMethod(method);
159 } catch (Throwable t) {
160 t.printStackTrace();
161 fail("Unable to execute method : " + t.toString());
162 }
163 assertEquals(200,method.getStatusCode());
164 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: POST</title>") >= 0);
165 assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0);
166 }
167
168 public void testSimpleAuthPut() throws Exception {
169 HttpClient client = createHttpClient();
170 client.getState().setCredentials("BasicAuthServlet",new UsernamePasswordCredentials("jakarta","commons"));
171 PutMethod method = new PutMethod("/" + getWebappContext() + "/auth/basic");
172 method.setRequestBody("testing one two three");
173 try {
174 client.executeMethod(method);
175 } catch (Throwable t) {
176 t.printStackTrace();
177 fail("Unable to execute method : " + t.toString());
178 }
179 assertEquals(200,method.getStatusCode());
180 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: PUT</title>") >= 0);
181 assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0);
182
183 method.recycle();
184 method.setPath("/" + getWebappContext() + "/auth/basic");
185 try {
186 client.executeMethod(method);
187 } catch (Throwable t) {
188 t.printStackTrace();
189 fail("Unable to execute method : " + t.toString());
190 }
191 assertEquals(200,method.getStatusCode());
192 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: PUT</title>") >= 0);
193 assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0);
194 }
195
196 public void testNoCredAuthRetry() throws Exception {
197 HttpClient client = createHttpClient();
198 GetMethod method = new GetMethod("/" + getWebappContext() + "/auth/basic");
199
200 try {
201 client.executeMethod(method);
202 } catch (Throwable t) {
203 t.printStackTrace();
204 fail("Unable to execute method : " + t.toString());
205 }
206 assertEquals(401,method.getStatusCode());
207 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0);
208 assertTrue(method.getResponseBodyAsString().indexOf("<p>Not authorized.</p>") >= 0);
209
210 client.getState().setCredentials("BasicAuthServlet",new UsernamePasswordCredentials("jakarta","commons"));
211
212 method.recycle();
213 method.setPath("/" + getWebappContext() + "/auth/basic");
214 try {
215 client.executeMethod(method);
216 } catch (Throwable t) {
217 t.printStackTrace();
218 fail("Unable to execute method : " + t.toString());
219 }
220 assertEquals(200,method.getStatusCode());
221 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0);
222 assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0);
223 }
224
225 public void testBadCredFails() throws Exception {
226 HttpClient client = createHttpClient();
227 GetMethod method = new GetMethod("/" + getWebappContext() + "/auth/basic");
228
229 try {
230 client.executeMethod(method);
231 } catch (Throwable t) {
232 t.printStackTrace();
233 fail("Unable to execute method : " + t.toString());
234 }
235 assertEquals(HttpStatus.SC_UNAUTHORIZED,method.getStatusCode());
236 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0);
237 assertTrue(method.getResponseBodyAsString().indexOf("<p>Not authorized.</p>") >= 0);
238
239 client.getState().setCredentials("BasicAuthServlet",new UsernamePasswordCredentials("bad","creds"));
240
241 method.recycle();
242 method.setPath("/" + getWebappContext() + "/auth/basic");
243 try {
244 client.executeMethod(method);
245 } catch (Throwable t) {
246 t.printStackTrace();
247 fail("Unable to execute method : " + t.toString());
248 }
249 assertEquals(HttpStatus.SC_UNAUTHORIZED,method.getStatusCode());
250 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0);
251 assertTrue(method.getResponseBodyAsString().indexOf("<p>Not authorized. \"Basic YmFkOmNyZWRz\" not recognized.</p>") >= 0);
252 }
253
254 public void testHeadAuth() throws Exception {
255 HttpClient client = new HttpClient();
256 HttpState state = client.getState();
257 Credentials cred = new UsernamePasswordCredentials("jakarta", "commons");
258 state.setCredentials(null, cred);
259 HostConfiguration hc = new HostConfiguration();
260 hc.setHost(getHost(), getPort(), getProtocol());
261 client.setHostConfiguration(hc);
262 client.setState(state);
263 HeadMethod method = new HeadMethod("/"+ getWebappContext() +"/auth/basic");
264 client.executeMethod(method);
265 method.releaseConnection();
266 assertEquals(200, method.getStatusCode());
267 }
268
269 }
270
271
272
273
This page was automatically generated by Maven