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 package org.apache.commons.httpclient;
29
30 import java.io.IOException;
31 import java.io.InputStreamReader;
32 import java.io.Reader;
33
34 import org.apache.commons.httpclient.methods.GetMethod;
35 import org.apache.commons.httpclient.protocol.Protocol;
36 import org.apache.commons.httpclient.server.HttpService;
37 import org.apache.commons.httpclient.server.SimpleRequest;
38 import org.apache.commons.httpclient.server.SimpleResponse;
39
40 import junit.framework.Test;
41 import junit.framework.TestSuite;
42
43 /***
44 * Tests basic method functionality.
45 *
46 * @author Remy Maucherat
47 * @author Rodney Waldhoff
48 * @author Oleg Kalnichevski
49 *
50 * @version $Id: TestHttpMethodFundamentals.java 155418 2005-02-26 13:01:52Z dirkv $
51 */
52 public class TestHttpMethodFundamentals extends HttpClientTestBase {
53
54 public TestHttpMethodFundamentals(final String testName) throws IOException {
55 super(testName);
56 }
57
58 public static Test suite() {
59 TestSuite suite = new TestSuite(TestHttpMethodFundamentals.class);
60 ProxyTestDecorator.addTests(suite);
61 return suite;
62 }
63
64 public static void main(String args[]) {
65 String[] testCaseName = { TestHttpMethodFundamentals.class.getName() };
66 junit.textui.TestRunner.main(testCaseName);
67 }
68
69 class ManyAService implements HttpService {
70
71 public ManyAService() {
72 super();
73 }
74
75 public boolean process(final SimpleRequest request, final SimpleResponse response)
76 throws IOException
77 {
78 HttpVersion httpversion = request.getRequestLine().getHttpVersion();
79 response.setStatusLine(httpversion, HttpStatus.SC_OK);
80 response.addHeader(new Header("Content-Type", "text/plain"));
81 response.addHeader(new Header("Connection", "close"));
82 StringBuffer buffer = new StringBuffer(1024);
83 for (int i = 0; i < 1024; i++) {
84 buffer.append('A');
85 }
86 response.setBodyString(buffer.toString());
87 return true;
88 }
89 }
90
91 class SimpleChunkedService implements HttpService {
92
93 public SimpleChunkedService() {
94 super();
95 }
96
97 public boolean process(final SimpleRequest request, final SimpleResponse response)
98 throws IOException
99 {
100 HttpVersion httpversion = request.getRequestLine().getHttpVersion();
101 response.setStatusLine(httpversion, HttpStatus.SC_OK);
102 response.addHeader(new Header("Content-Type", "text/plain"));
103 response.addHeader(new Header("Content-Length", "garbage"));
104 response.addHeader(new Header("Transfer-Encoding", "chunked"));
105 response.addHeader(new Header("Connection", "close"));
106 response.setBodyString("1234567890123");
107 return true;
108 }
109 }
110
111 class EmptyResponseService implements HttpService {
112
113 public EmptyResponseService() {
114 super();
115 }
116
117 public boolean process(final SimpleRequest request, final SimpleResponse response)
118 throws IOException
119 {
120 HttpVersion httpversion = request.getRequestLine().getHttpVersion();
121 response.setStatusLine(httpversion, HttpStatus.SC_OK);
122 response.addHeader(new Header("Content-Type", "text/plain"));
123 response.addHeader(new Header("Transfer-Encoding", "chunked"));
124 response.addHeader(new Header("Connection", "close"));
125 return true;
126 }
127 }
128
129 public void testRelativeURLHitWithDefaultHost() throws IOException {
130 this.server.setHttpService(new EchoService());
131
132 this.client.getHostConfiguration().setHost(
133 this.server.getLocalAddress(),
134 this.server.getLocalPort(),
135 Protocol.getProtocol("http"));
136
137 GetMethod httpget = new GetMethod("/test/");
138 try {
139 this.client.executeMethod(httpget);
140 assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
141 } finally {
142 httpget.releaseConnection();
143 }
144 }
145
146 public void testRelativeURLHitWithoutDefaultHost() throws IOException {
147 this.server.setHttpService(new EchoService());
148
149 this.client.setHostConfiguration(new HostConfiguration());
150
151 GetMethod httpget = new GetMethod("/test/");
152 try {
153 this.client.executeMethod(httpget);
154 fail("IllegalArgumentException should have been thrown");
155 } catch (IllegalArgumentException expected) {
156 } finally {
157 httpget.releaseConnection();
158 }
159 }
160
161 public void testAbsoluteURLHitWithoutDefaultHost() throws IOException {
162 this.server.setHttpService(new EchoService());
163
164 this.client.setHostConfiguration(new HostConfiguration());
165
166 GetMethod httpget = new GetMethod("http://" +
167 this.server.getLocalAddress() + ":" + this.server.getLocalPort() + "/test/");
168 try {
169 this.client.executeMethod(httpget);
170 assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
171 } finally {
172 httpget.releaseConnection();
173 }
174 }
175
176 public void testAbsoluteURLHitWithDefaultHost() throws IOException {
177 this.server.setHttpService(new EchoService());
178
179 this.client.getHostConfiguration().setHost(
180 "somewhere.in.pampa",
181 9999,
182 Protocol.getProtocol("http"));
183
184 GetMethod httpget = new GetMethod("http://" +
185 this.server.getLocalAddress() + ":" + this.server.getLocalPort() + "/test/");
186 try {
187 this.client.executeMethod(httpget);
188 assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
189 } finally {
190 httpget.releaseConnection();
191 }
192 }
193
194 public void testHttpMethodBasePaths() throws Exception {
195 HttpMethod simple = new FakeHttpMethod();
196 String[] paths = {
197 "/some/absolute/path",
198 "../some/relative/path",
199 "/",
200 "/some/path/with?query=string"
201 };
202
203 for (int i=0; i<paths.length; i++){
204 simple.setPath(paths[i]);
205 assertEquals(paths[i], simple.getPath());
206 }
207 }
208
209 public void testHttpMethodBaseDefaultPath() throws Exception {
210 HttpMethod simple = new FakeHttpMethod();
211 assertEquals("/", simple.getPath());
212
213 simple.setPath("");
214 assertEquals("/", simple.getPath());
215
216 simple.setPath(null);
217 assertEquals("/", simple.getPath());
218 }
219
220 public void testHttpMethodBasePathConstructor() throws Exception {
221 HttpMethod simple = new FakeHttpMethod();
222 assertEquals("/", simple.getPath());
223
224 simple = new FakeHttpMethod("");
225 assertEquals("/", simple.getPath());
226
227 simple = new FakeHttpMethod("/some/path/");
228 assertEquals("/some/path/", simple.getPath());
229 }
230
231 /***
232 * Tests response with a Trasfer-Encoding and Content-Length
233 */
234 public void testHttpMethodBaseTEandCL() throws Exception {
235 this.server.setHttpService(new SimpleChunkedService());
236
237 GetMethod httpget = new GetMethod("/test/");
238 try {
239 this.client.executeMethod(httpget);
240 assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
241 assertEquals("1234567890123", httpget.getResponseBodyAsString());
242 assertTrue(this.client.getHttpConnectionManager() instanceof SimpleHttpConnectionManager);
243 HttpConnection conn = this.client.getHttpConnectionManager().
244 getConnection(this.client.getHostConfiguration());
245 assertNotNull(conn);
246 conn.assertNotOpen();
247 } finally {
248 httpget.releaseConnection();
249 }
250 }
251
252 public void testConnectionAutoClose() throws Exception {
253 this.server.setHttpService(new ManyAService());
254
255 GetMethod httpget = new GetMethod("/test/");
256 try {
257 this.client.executeMethod(httpget);
258 assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
259 Reader response = new InputStreamReader(httpget.getResponseBodyAsStream());
260 int c;
261 while ((c = response.read()) != -1) {
262 assertEquals((int) 'A', c);
263 }
264 assertTrue(this.client.getHttpConnectionManager() instanceof SimpleHttpConnectionManager);
265 HttpConnection conn = this.client.getHttpConnectionManager().
266 getConnection(this.client.getHostConfiguration());
267 assertNotNull(conn);
268 conn.assertNotOpen();
269 } finally {
270 httpget.releaseConnection();
271 }
272 }
273
274 public void testSetGetQueryString1() {
275 HttpMethod method = new GetMethod();
276 String qs1 = "name1=value1&name2=value2";
277 method.setQueryString(qs1);
278 assertEquals(qs1, method.getQueryString());
279 }
280
281 public void testQueryURIEncoding() {
282 HttpMethod method = new GetMethod("http://server/servlet?foo=bar&baz=schmoo");
283 assertEquals("foo=bar&baz=schmoo", method.getQueryString());
284 }
285
286 public void testSetGetQueryString2() {
287 HttpMethod method = new GetMethod();
288 NameValuePair[] q1 = new NameValuePair[] {
289 new NameValuePair("name1", "value1"),
290 new NameValuePair("name2", "value2")
291 };
292 method.setQueryString(q1);
293 String qs1 = "name1=value1&name2=value2";
294 assertEquals(qs1, method.getQueryString());
295 }
296
297 /***
298 * Make sure that its OK to call releaseConnection if the connection has not been.
299 */
300 public void testReleaseConnection() {
301 HttpMethod method = new GetMethod("http://bogus.url/path/");
302 method.releaseConnection();
303 }
304
305 /***
306 * Tests empty body response
307 */
308 public void testEmptyBodyAsString() throws Exception {
309 this.server.setHttpService(new EmptyResponseService());
310
311 GetMethod httpget = new GetMethod("/test/");
312 try {
313 this.client.executeMethod(httpget);
314 assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
315 String response = httpget.getResponseBodyAsString();
316 assertNull(response);
317 } finally {
318 httpget.releaseConnection();
319 }
320 }
321
322
323 public void testEmptyBodyAsByteArray() throws Exception {
324 this.server.setHttpService(new EmptyResponseService());
325
326 GetMethod httpget = new GetMethod("/test/");
327 try {
328 this.client.executeMethod(httpget);
329 assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
330 byte[] response = httpget.getResponseBody();
331 assertNull(response);
332 } finally {
333 httpget.releaseConnection();
334 }
335 }
336
337 public void testUrlGetMethodWithPathQuery() {
338 GetMethod method = new GetMethod("http://www.fubar.com/path1/path2?query=string");
339 try {
340 assertEquals(
341 "Get URL",
342 "http://www.fubar.com/path1/path2?query=string",
343 method.getURI().toString()
344 );
345 } catch ( URIException e ) {
346 fail( "trouble getting URI: " + e );
347 }
348 assertEquals("Get Path", "/path1/path2", method.getPath());
349 assertEquals("Get query string", "query=string", method.getQueryString());
350
351 }
352
353 public void testUrlGetMethodWithPath() {
354 GetMethod method = new GetMethod("http://www.fubar.com/path1/path2");
355 try {
356 assertEquals(
357 "Get URL",
358 "http://www.fubar.com/path1/path2",
359 method.getURI().toString()
360 );
361 } catch ( URIException e ) {
362 fail( "trouble getting URI: " + e );
363 }
364 assertEquals("Get Path", "/path1/path2", method.getPath());
365 assertEquals("Get query string", null, method.getQueryString());
366 }
367
368 public void testUrlGetMethod() {
369 GetMethod method = new GetMethod("http://www.fubar.com/");
370 try {
371 assertEquals(
372 "Get URL",
373 "http://www.fubar.com/",
374 method.getURI().toString()
375 );
376 } catch ( URIException e ) {
377 fail( "trouble getting URI: " + e );
378 }
379 assertEquals("Get Path", "/", method.getPath());
380 assertEquals("Get query string", null, method.getQueryString());
381
382 }
383
384
385 public void testUrlGetMethodWithInvalidProtocol() {
386 try {
387 GetMethod method = new GetMethod("crap://www.fubar.com/");
388 fail("The use of invalid protocol must have resulted in an IllegalStateException");
389 }
390 catch(IllegalStateException expected) {
391 }
392 }
393 }