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