KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsNoHost.java,v 1.19.2.1 2004/02/22 18:21:16 olegk Exp $
3  * $Revision: 1.19.2.1 $
4  * $Date: 2004/02/22 18:21:16 $
5  * ====================================================================
6  *
7  * Copyright 1999-2004 The Apache Software Foundation
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ====================================================================
21  *
22  * This software consists of voluntary contributions made by many
23  * individuals on behalf of the Apache Software Foundation. For more
24  * information on the Apache Software Foundation, please see
25  * <http://www.apache.org/>.
26  *
27  * [Additional notices, if required by prior licensing conditions]
28  *
29  */

30
31 package org.apache.commons.httpclient;
32
33 import java.io.IOException JavaDoc;
34 import java.io.InputStreamReader JavaDoc;
35 import java.io.Reader JavaDoc;
36
37 import junit.framework.Test;
38 import junit.framework.TestCase;
39 import junit.framework.TestSuite;
40 import org.apache.commons.httpclient.methods.GetMethod;
41 import org.apache.commons.httpclient.methods.PostMethod;
42 import org.apache.commons.httpclient.methods.HeadMethod;
43
44 /**
45  * @author Rodney Waldhoff
46  * @author <a HREF="mailto:jsdever@apache.org">Jeff Dever</a>
47  * @author Ortwin Glück
48  * @author <a HREF="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
49  * @version $Revision: 1.19.2.1 $ $Date: 2004/02/22 18:21:16 $
50  */

51 public class TestMethodsNoHost extends TestCase {
52
53     static final String JavaDoc NAME = "name", VALUE = "value";
54     static final String JavaDoc NAME0 = "name0", VALUE0 = "value0";
55     static final String JavaDoc NAME1 = "name1", VALUE1 = "value1";
56     static final String JavaDoc NAME2 = "name2", VALUE2 = "value2";
57
58     static final NameValuePair PAIR = new NameValuePair(NAME, VALUE);
59     static final NameValuePair PAIR0 = new NameValuePair(NAME0, VALUE0);
60     static final NameValuePair PAIR1 = new NameValuePair(NAME1, VALUE1);
61     static final NameValuePair PAIR2 = new NameValuePair(NAME2, VALUE2);
62     
63     // ------------------------------------------------------------ Constructor
64

65     public TestMethodsNoHost(String JavaDoc testName) {
66         super(testName);
67     }
68
69     // ------------------------------------------------------- TestCase Methods
70

71     public static Test suite() {
72         return new TestSuite(TestMethodsNoHost.class);
73     }
74
75     // ----------------------------------------------------------------- Tests
76

77     public void testPostParametersEncoding() throws IOException JavaDoc {
78         PostMethod post = new PostMethod();
79         post.setRequestBody(new NameValuePair[] { PAIR });
80         assertEquals("name=value", post.getRequestBodyAsString());
81
82         post.setRequestBody(new NameValuePair[]{ PAIR, PAIR1, PAIR2 });
83         assertEquals("name=value&name1=value1&name2=value2",
84             post.getRequestBodyAsString());
85
86         post.setRequestBody(new NameValuePair[]{ PAIR, PAIR1, PAIR2, new NameValuePair("hasSpace", "a b c d") });
87         assertEquals("name=value&name1=value1&name2=value2&hasSpace=a+b+c+d",
88             post.getRequestBodyAsString());
89
90     }
91
92     public void testPostSetRequestBody() throws Exception JavaDoc {
93         PostMethod post = new PostMethod("/foo");
94         String JavaDoc body = "this+is+the+body";
95         post.setRequestBody(body);
96         assertEquals(body, post.getRequestBodyAsString());
97     }
98
99
100     public void testHttpMethodBasePaths() throws Exception JavaDoc {
101         HttpMethod simple = new SimpleHttpMethod();
102         String JavaDoc[] paths = {
103            "/some/absolute/path",
104            "../some/relative/path",
105            "/",
106            "/some/path/with?query=string"
107        };
108     
109         for (int i=0; i<paths.length; i++){
110             simple.setPath(paths[i]);
111             assertEquals(paths[i], simple.getPath());
112         }
113     }
114
115     public void testHttpMethodBaseDefaultPath() throws Exception JavaDoc {
116         HttpMethod simple = new SimpleHttpMethod();
117         assertEquals("/", simple.getPath());
118
119         simple.setPath("");
120         assertEquals("/", simple.getPath());
121
122         simple.setPath(null);
123         assertEquals("/", simple.getPath());
124     }
125
126     public void testHttpMethodBasePathConstructor() throws Exception JavaDoc {
127         HttpMethod simple = new SimpleHttpMethod();
128         assertEquals("/", simple.getPath());
129
130         simple = new SimpleHttpMethod("");
131         assertEquals("/", simple.getPath());
132
133         simple = new SimpleHttpMethod("/some/path/");
134         assertEquals("/some/path/", simple.getPath());
135     }
136
137     /** Tests response with a Trasfer-Encoding and Content-Length */
138     public void testHttpMethodBaseTEandCL() throws Exception JavaDoc {
139         SimpleHttpConnection conn = new SimpleHttpConnection();
140         String JavaDoc headers = "HTTP/1.1 200 OK\r\n"
141                        +"Date: Wed, 28 Mar 2001 05:05:04 GMT\r\n"
142                        +"Connection: close\r\n"
143                        +"Transfer-Encoding: chunked\r\n"
144                        +"Content-Length: 1\r\n";
145         String JavaDoc body = "0a\r\n1234567890\r\n3\r\n123\r\n0\r\n";
146         conn.addResponse(headers, body);
147         conn.open();
148         HttpMethodBase method = new GetMethod("/");
149         method.execute(new HttpState(), conn);
150         String JavaDoc responseBody = method.getResponseBodyAsString();
151         // verify that the connection was closed.
152
conn.assertNotOpen();
153         assertEquals("1234567890123", responseBody);
154     }
155
156     public void testConnectionAutoClose() throws Exception JavaDoc {
157         SimpleHttpConnection conn = new SimpleHttpConnection();
158         String JavaDoc headers = "HTTP/1.1 200 OK\r\n"
159                        +"Date: Wed, 28 Mar 2001 05:05:04 GMT\r\n"
160                        +"Connection: close\r\n";
161         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(8200);
162         for (int i = 0; i < 8200; i++) {
163             buffer.append('A');
164         }
165         String JavaDoc body = buffer.toString();
166
167         conn.addResponse(headers, body);
168         conn.open();
169         HttpMethodBase method = new GetMethod("/");
170         method.execute(new HttpState(), conn);
171         Reader JavaDoc response = new InputStreamReader JavaDoc(method.getResponseBodyAsStream());
172         int c;
173         while ((c = response.read()) != -1) {
174            assertEquals((int) 'A', c);
175         }
176         conn.assertNotOpen();
177
178         // note - this test is here because the HEAD method handler overrides the
179
// standard behavior for reading a response body.
180
HeadMethod headMethod = new HeadMethod("/");
181
182         conn.addResponse(headers, "");
183
184         try {
185             headMethod.execute(new HttpState(), conn);
186             conn.assertNotOpen();
187
188         } catch (Throwable JavaDoc t) {
189             t.printStackTrace();
190             fail("Unable to execute method : " + t.toString());
191         }
192     }
193
194     public void testSetGetQueryString1() {
195         HttpMethod method = new GetMethod();
196         String JavaDoc qs1 = "name1=value1&name2=value2";
197         method.setQueryString(qs1);
198         assertEquals(qs1, method.getQueryString());
199     }
200
201     public void testQueryURIEncoding() {
202         HttpMethod method = new GetMethod("http://server/servlet?foo=bar&baz=schmoo");
203         assertEquals("foo=bar&baz=schmoo", method.getQueryString());
204     }
205
206     public void testSetGetQueryString2() {
207         HttpMethod method = new GetMethod();
208         NameValuePair[] q1 = new NameValuePair[] {
209             new NameValuePair("name1", "value1"),
210             new NameValuePair("name2", "value2")
211         };
212         method.setQueryString(q1);
213         String JavaDoc qs1 = "name1=value1&name2=value2";
214         assertEquals(qs1, method.getQueryString());
215     }
216
217     /**
218      * Make sure that its OK to call releaseConnection if the connection has not been.
219      */

220     public void testReleaseConnection() {
221         HttpClient client = new HttpClient();
222         HttpMethod method = new GetMethod("http://bogus.url/path/");
223         method.releaseConnection();
224     }
225
226
227     /**
228      * Tests empty body response
229      */

230     
231     public void testEmptyBodyAsString() throws Exception JavaDoc {
232         SimpleHttpConnection conn = new SimpleHttpConnection();
233         String JavaDoc headers = "HTTP/1.1 200 OK\r\n"
234                        +"Connection: close\r\n"
235                        +"Transfer-Encoding: chunked\r\n"
236                        +"Content-Length: 0\r\n";
237         String JavaDoc body = "";
238         conn.addResponse(headers, body);
239         conn.open();
240         HttpMethodBase method = new GetMethod("/");
241         method.execute(new HttpState(), conn);
242
243         String JavaDoc response = method.getResponseBodyAsString();
244         assertNull(response);
245     }
246
247
248     public void testEmptyBodyAsByteArray() throws Exception JavaDoc {
249         SimpleHttpConnection conn = new SimpleHttpConnection();
250         String JavaDoc headers = "HTTP/1.1 200 OK\r\n"
251                        +"Connection: close\r\n"
252                        +"Transfer-Encoding: chunked\r\n"
253                        +"Content-Length: 0\r\n";
254         String JavaDoc body = "";
255         conn.addResponse(headers, body);
256         conn.open();
257         HttpMethodBase method = new GetMethod("/");
258         method.execute(new HttpState(), conn);
259
260         byte[] response = method.getResponseBody();
261         assertNull(response);
262     }
263
264
265 }
266
Popular Tags