KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestHttpMethodFundamentals.java,v 1.6 2004/11/06 23:47:58 olegk Exp $
3  * $Revision: 481225 $
4  * $Date: 2006-12-01 11:26:28 +0000 (Fri, 01 Dec 2006) $
5  * ====================================================================
6  *
7  * Licensed to the Apache Software Foundation (ASF) under one or more
8  * contributor license agreements. See the NOTICE file distributed with
9  * this work for additional information regarding copyright ownership.
10  * The ASF licenses this file to You under the Apache License, Version 2.0
11  * (the "License"); you may not use this file except in compliance with
12  * the License. You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ====================================================================
22  *
23  * This software consists of voluntary contributions made by many
24  * individuals on behalf of the Apache Software Foundation. For more
25  * information on the Apache Software Foundation, please see
26  * <http://www.apache.org/>.
27  */

28
29 package org.apache.commons.httpclient;
30
31 import java.io.IOException JavaDoc;
32 import java.io.InputStreamReader JavaDoc;
33 import java.io.Reader JavaDoc;
34
35 import org.apache.commons.httpclient.methods.GetMethod;
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 481225 2006-12-01 11:26:28 +0000 (Fri, 01 Dec 2006) oglueck $
51  */

52 public class TestHttpMethodFundamentals extends HttpClientTestBase {
53
54     public TestHttpMethodFundamentals(final String JavaDoc testName) throws IOException JavaDoc {
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 JavaDoc args[]) {
65         String JavaDoc[] 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 JavaDoc
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 JavaDoc buffer = new StringBuffer JavaDoc(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 JavaDoc
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 JavaDoc
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 testHttpMethodBasePaths() throws Exception JavaDoc {
130         HttpMethod simple = new FakeHttpMethod();
131         String JavaDoc[] paths = {
132            "/some/absolute/path",
133            "../some/relative/path",
134            "/",
135            "/some/path/with?query=string"
136        };
137     
138         for (int i=0; i<paths.length; i++){
139             simple.setPath(paths[i]);
140             assertEquals(paths[i], simple.getPath());
141         }
142     }
143
144     public void testHttpMethodBaseDefaultPath() throws Exception JavaDoc {
145         HttpMethod simple = new FakeHttpMethod();
146         assertEquals("/", simple.getPath());
147
148         simple.setPath("");
149         assertEquals("/", simple.getPath());
150
151         simple.setPath(null);
152         assertEquals("/", simple.getPath());
153     }
154
155     public void testHttpMethodBasePathConstructor() throws Exception JavaDoc {
156         HttpMethod simple = new FakeHttpMethod();
157         assertEquals("/", simple.getPath());
158
159         simple = new FakeHttpMethod("");
160         assertEquals("/", simple.getPath());
161
162         simple = new FakeHttpMethod("/some/path/");
163         assertEquals("/some/path/", simple.getPath());
164     }
165
166     /**
167      * Tests response with a Trasfer-Encoding and Content-Length
168      */

169     public void testHttpMethodBaseTEandCL() throws Exception JavaDoc {
170         this.server.setHttpService(new SimpleChunkedService());
171         
172         GetMethod httpget = new GetMethod("/test/");
173         try {
174             this.client.executeMethod(httpget);
175             assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
176             assertEquals("1234567890123", httpget.getResponseBodyAsString());
177             assertTrue(this.client.getHttpConnectionManager() instanceof SimpleHttpConnectionManager);
178             HttpConnection conn = this.client.getHttpConnectionManager().
179                 getConnection(this.client.getHostConfiguration());
180             assertNotNull(conn);
181             conn.assertNotOpen();
182         } finally {
183             httpget.releaseConnection();
184         }
185     }
186
187     public void testConnectionAutoClose() throws Exception JavaDoc {
188         this.server.setHttpService(new ManyAService());
189         
190         GetMethod httpget = new GetMethod("/test/");
191         try {
192             this.client.executeMethod(httpget);
193             assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
194             Reader JavaDoc response = new InputStreamReader JavaDoc(httpget.getResponseBodyAsStream());
195             int c;
196             while ((c = response.read()) != -1) {
197                assertEquals((int) 'A', c);
198             }
199             assertTrue(this.client.getHttpConnectionManager() instanceof SimpleHttpConnectionManager);
200             HttpConnection conn = this.client.getHttpConnectionManager().
201                 getConnection(this.client.getHostConfiguration());
202             assertNotNull(conn);
203             conn.assertNotOpen();
204         } finally {
205             httpget.releaseConnection();
206         }
207     }
208
209     public void testSetGetQueryString1() {
210         HttpMethod method = new GetMethod();
211         String JavaDoc qs1 = "name1=value1&name2=value2";
212         method.setQueryString(qs1);
213         assertEquals(qs1, method.getQueryString());
214     }
215
216     public void testQueryURIEncoding() {
217         HttpMethod method = new GetMethod("http://server/servlet?foo=bar&baz=schmoo");
218         assertEquals("foo=bar&baz=schmoo", method.getQueryString());
219     }
220
221     public void testSetGetQueryString2() {
222         HttpMethod method = new GetMethod();
223         NameValuePair[] q1 = new NameValuePair[] {
224             new NameValuePair("name1", "value1"),
225             new NameValuePair("name2", "value2")
226         };
227         method.setQueryString(q1);
228         String JavaDoc qs1 = "name1=value1&name2=value2";
229         assertEquals(qs1, method.getQueryString());
230     }
231
232     /**
233      * Make sure that its OK to call releaseConnection if the connection has not been.
234      */

235     public void testReleaseConnection() {
236         HttpMethod method = new GetMethod("http://bogus.url/path/");
237         method.releaseConnection();
238     }
239
240     /**
241      * Tests empty body response
242      */

243     public void testEmptyBodyAsString() throws Exception JavaDoc {
244         this.server.setHttpService(new EmptyResponseService());
245         
246         GetMethod httpget = new GetMethod("/test/");
247         try {
248             this.client.executeMethod(httpget);
249             assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
250             String JavaDoc response = httpget.getResponseBodyAsString();
251             assertNull(response);
252
253             this.client.executeMethod(httpget);
254             assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
255             response = httpget.getResponseBodyAsString(1);
256             assertNull(response);
257         } finally {
258             httpget.releaseConnection();
259         }
260     }
261
262
263     public void testEmptyBodyAsByteArray() throws Exception JavaDoc {
264         this.server.setHttpService(new EmptyResponseService());
265         
266         GetMethod httpget = new GetMethod("/test/");
267         try {
268             this.client.executeMethod(httpget);
269             assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
270             byte[] response = httpget.getResponseBody();
271             assertNull(response);
272         } finally {
273             httpget.releaseConnection();
274         }
275     }
276     
277     public void testLongBodyAsString() throws Exception JavaDoc {
278         this.server.setHttpService(new SimpleChunkedService());
279         
280         GetMethod httpget = new GetMethod("/test/");
281         try {
282             this.client.executeMethod(httpget);
283             assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
284             try {
285                 httpget.getResponseBodyAsString(5); // too small
286
} catch(HttpContentTooLargeException e) {
287                 /* expected */
288                 assertEquals(5, e.getMaxLength());
289             }
290             
291             this.client.executeMethod(httpget);
292             assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
293             String JavaDoc response = httpget.getResponseBodyAsString(13); // exact size
294
assertEquals("1234567890123", response);
295
296             this.client.executeMethod(httpget);
297             assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
298             response = httpget.getResponseBodyAsString(128); // plenty
299
assertEquals("1234567890123", response);
300         } finally {
301             httpget.releaseConnection();
302         }
303     }
304     
305     public void testUrlGetMethodWithPathQuery() {
306         GetMethod method = new GetMethod("http://www.fubar.com/path1/path2?query=string");
307         try {
308             assertEquals(
309                 "Get URL",
310                 "http://www.fubar.com/path1/path2?query=string",
311                 method.getURI().toString()
312             );
313         } catch ( URIException e ) {
314             fail( "trouble getting URI: " + e );
315         }
316         assertEquals("Get Path", "/path1/path2", method.getPath());
317         assertEquals("Get query string", "query=string", method.getQueryString());
318      
319     }
320
321     public void testUrlGetMethodWithPath() {
322         GetMethod method = new GetMethod("http://www.fubar.com/path1/path2");
323         try {
324             assertEquals(
325                 "Get URL",
326                 "http://www.fubar.com/path1/path2",
327                 method.getURI().toString()
328             );
329         } catch ( URIException e ) {
330             fail( "trouble getting URI: " + e );
331         }
332         assertEquals("Get Path", "/path1/path2", method.getPath());
333         assertEquals("Get query string", null, method.getQueryString());
334     }
335
336     public void testUrlGetMethod() {
337         GetMethod method = new GetMethod("http://www.fubar.com/");
338         try {
339             assertEquals(
340                 "Get URL",
341                 "http://www.fubar.com/",
342                 method.getURI().toString()
343             );
344         } catch ( URIException e ) {
345             fail( "trouble getting URI: " + e );
346         }
347         assertEquals("Get Path", "/", method.getPath());
348         assertEquals("Get query string", null, method.getQueryString());
349
350     }
351     
352
353     public void testUrlGetMethodWithInvalidProtocol() {
354         try {
355             GetMethod method = new GetMethod("crap://www.fubar.com/");
356             fail("The use of invalid protocol must have resulted in an IllegalStateException");
357         }
358         catch(IllegalStateException JavaDoc expected) {
359         }
360     }
361 }
362
Popular Tags