1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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.auth.HttpAuthRealm;
37 import org.apache.commons.httpclient.methods.GetMethod;
38 import org.apache.commons.httpclient.methods.HeadMethod;
39 import org.apache.commons.httpclient.methods.PostMethod;
40 import org.apache.commons.httpclient.methods.PutMethod;
41 import org.apache.commons.httpclient.methods.StringRequestEntity;
42
43 /***
44 * This suite of tests depends upon the httpclienttest webapp,
45 * which is available in the httpclient/src/test-webapp
46 * directory in the CVS tree.
47 * <p>
48 * The webapp should be deployed in the context "httpclienttest"
49 * on a servlet engine running on port 8080 on the localhost
50 * (IP 127.0.0.1).
51 * <p>
52 * You can change the assumed port by setting the
53 * "httpclient.test.localPort" property.
54 * You can change the assumed host by setting the
55 * "httpclient.test.localHost" property.
56 * You can change the assumed context by setting the
57 * "httpclient.test.webappContext" property.
58 *
59 * @author Rodney Waldhoff
60 * @version $Id: TestWebappBasicAuth.java,v 1.16 2004/05/12 20:43:54 olegk Exp $
61 */
62 public class TestWebappBasicAuth extends TestWebappBase {
63
64 public TestWebappBasicAuth(String testName) {
65 super(testName);
66 }
67
68 public static Test suite() {
69 TestSuite suite = new TestSuite(TestWebappBasicAuth.class);
70 return suite;
71 }
72
73 public static void main(String args[]) {
74 String[] testCaseName = { TestWebappBasicAuth.class.getName() };
75 junit.textui.TestRunner.main(testCaseName);
76 }
77
78
79
80 public void testSimpleAuthGet() throws Exception {
81 HttpClient client = createHttpClient();
82 client.getState().setCredentials(
83 new HttpAuthRealm(getHost(), getPort(), "BasicAuthServlet"),
84 new UsernamePasswordCredentials("jakarta","commons"));
85 GetMethod method = new GetMethod("/" + getWebappContext() + "/auth/basic");
86
87 try {
88 client.executeMethod(method);
89 } catch (Throwable t) {
90 t.printStackTrace();
91 fail("Unable to execute method : " + t.toString());
92 }
93 assertEquals(200,method.getStatusCode());
94 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0);
95 assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0);
96
97 method.recycle();
98 method.setPath("/" + getWebappContext() + "/auth/basic");
99 try {
100 client.executeMethod(method);
101 } catch (Throwable t) {
102 t.printStackTrace();
103 fail("Unable to execute method : " + t.toString());
104 }
105 assertEquals(200,method.getStatusCode());
106 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0);
107 assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0);
108 }
109
110 public void testSimpleAuthPost() throws Exception {
111 HttpClient client = createHttpClient();
112 client.getState().setCredentials(
113 new HttpAuthRealm(getHost(), getPort(), "BasicAuthServlet"),
114 new UsernamePasswordCredentials("jakarta","commons"));
115 PostMethod method = new PostMethod("/" + getWebappContext() + "/auth/basic");
116 method.setRequestBody(new NameValuePair[] { new NameValuePair("testing","one") } );
117
118 try {
119 client.executeMethod(method);
120 } catch (Throwable t) {
121 t.printStackTrace();
122 fail("Unable to execute method : " + t.toString());
123 }
124 assertEquals(200,method.getStatusCode());
125 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: POST</title>") >= 0);
126 assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0);
127
128 method.recycle();
129 method.setPath("/" + getWebappContext() + "/auth/basic");
130 method.setRequestBody(new NameValuePair[] { new NameValuePair("testing","one") } );
131 try {
132 client.executeMethod(method);
133 } catch (Throwable t) {
134 t.printStackTrace();
135 fail("Unable to execute method : " + t.toString());
136 }
137 assertEquals(200,method.getStatusCode());
138 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: POST</title>") >= 0);
139 assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0);
140 }
141
142 public void testSimpleAuthPut() throws Exception {
143 HttpClient client = createHttpClient();
144 client.getState().setCredentials(
145 new HttpAuthRealm(getHost(), getPort(), "BasicAuthServlet"),
146 new UsernamePasswordCredentials("jakarta","commons"));
147 PutMethod method = new PutMethod("/" + getWebappContext() + "/auth/basic");
148 method.setRequestEntity(new StringRequestEntity("testing one two three"));
149 try {
150 client.executeMethod(method);
151 } catch (Throwable t) {
152 t.printStackTrace();
153 fail("Unable to execute method : " + t.toString());
154 }
155 assertEquals(200,method.getStatusCode());
156 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: PUT</title>") >= 0);
157 assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0);
158
159 method.recycle();
160 method.setPath("/" + getWebappContext() + "/auth/basic");
161 try {
162 client.executeMethod(method);
163 } catch (Throwable t) {
164 t.printStackTrace();
165 fail("Unable to execute method : " + t.toString());
166 }
167 assertEquals(200,method.getStatusCode());
168 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: PUT</title>") >= 0);
169 assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0);
170 }
171
172 public void testNoCredAuthRetry() throws Exception {
173 HttpClient client = createHttpClient();
174 GetMethod method = new GetMethod("/" + getWebappContext() + "/auth/basic");
175
176 try {
177 client.executeMethod(method);
178 } catch (Throwable t) {
179 t.printStackTrace();
180 fail("Unable to execute method : " + t.toString());
181 }
182 assertEquals(401,method.getStatusCode());
183 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0);
184 assertTrue(method.getResponseBodyAsString().indexOf("<p>Not authorized.</p>") >= 0);
185
186 client.getState().setCredentials(
187 new HttpAuthRealm(getHost(), getPort(), "BasicAuthServlet"),
188 new UsernamePasswordCredentials("jakarta","commons"));
189
190 method.recycle();
191 method.setPath("/" + getWebappContext() + "/auth/basic");
192 try {
193 client.executeMethod(method);
194 } catch (Throwable t) {
195 t.printStackTrace();
196 fail("Unable to execute method : " + t.toString());
197 }
198 assertEquals(200,method.getStatusCode());
199 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0);
200 assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0);
201 }
202
203 public void testBadCredFails() throws Exception {
204 HttpClient client = createHttpClient();
205 GetMethod method = new GetMethod("/" + getWebappContext() + "/auth/basic");
206
207 try {
208 client.executeMethod(method);
209 } catch (Throwable t) {
210 t.printStackTrace();
211 fail("Unable to execute method : " + t.toString());
212 }
213 assertEquals(HttpStatus.SC_UNAUTHORIZED,method.getStatusCode());
214 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0);
215 assertTrue(method.getResponseBodyAsString().indexOf("<p>Not authorized.</p>") >= 0);
216
217 client.getState().setCredentials(
218 new HttpAuthRealm(getHost(), getPort(), "BasicAuthServlet"),
219 new UsernamePasswordCredentials("bad","creds"));
220
221 method.recycle();
222 method.setPath("/" + getWebappContext() + "/auth/basic");
223 try {
224 client.executeMethod(method);
225 } catch (Throwable t) {
226 t.printStackTrace();
227 fail("Unable to execute method : " + t.toString());
228 }
229 assertEquals(HttpStatus.SC_UNAUTHORIZED,method.getStatusCode());
230 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0);
231 assertTrue(method.getResponseBodyAsString().indexOf("<p>Not authorized. \"Basic YmFkOmNyZWRz\" not recognized.</p>") >= 0);
232 }
233
234 public void testHeadAuth() throws Exception {
235 HttpClient client = new HttpClient();
236 HttpState state = client.getState();
237 Credentials cred = new UsernamePasswordCredentials("jakarta", "commons");
238 state.setCredentials(null, null, cred);
239 HostConfiguration hc = new HostConfiguration();
240 hc.setHost(getHost(), getPort(), getProtocol());
241 client.setHostConfiguration(hc);
242 client.setState(state);
243 HeadMethod method = new HeadMethod("/"+ getWebappContext() +"/auth/basic");
244 client.executeMethod(method);
245 method.releaseConnection();
246 assertEquals(200, method.getStatusCode());
247 }
248
249 }
250
251
252
253