KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestResponseHeaders.java,v 1.8.2.3 2004/02/26 20:26:46 olegk Exp $
3  * $Revision: 1.8.2.3 $
4  * $Date: 2004/02/26 20:26:46 $
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 org.apache.commons.httpclient.methods.GetMethod;
34
35 import junit.framework.Test;
36 import junit.framework.TestCase;
37 import junit.framework.TestSuite;
38
39 /**
40  * Tests for reading response headers.
41  *
42  * @author <a HREF="mailto:dims@apache.org">Davanum Srinivas</a>
43  * @author <a HREF="mailto:jsdever@apache.org">Jeff Dever</a>
44  * @author <a HREF="mailto:adrian@intencha.com">Adrian Sutton</a>
45  * @version $Id: TestResponseHeaders.java,v 1.8.2.3 2004/02/26 20:26:46 olegk Exp $
46  */

47 public class TestResponseHeaders extends TestCase {
48
49     // ------------------------------------------------------------ Constructor
50
public TestResponseHeaders(String JavaDoc testName) {
51         super(testName);
52     }
53
54     // ------------------------------------------------------------------- Main
55
public static void main(String JavaDoc args[]) {
56         String JavaDoc[] testCaseName = {TestResponseHeaders.class.getName()};
57         junit.textui.TestRunner.main(testCaseName);
58     }
59
60     // ------------------------------------------------------- TestCase Methods
61
public static Test suite() {
62         return new TestSuite(TestResponseHeaders.class);
63     }
64
65
66
67     // ----------------------------------------------------------- Test Methods
68
public void testHeaders() throws Exception JavaDoc {
69         String JavaDoc body = "XXX\r\nYYY\r\nZZZ";
70         String JavaDoc headers =
71                 "HTTP/1.1 200 OK\r\n" +
72                 "Connection: close\r\n" +
73                 "Content-Length: " + body.length() + "\r\n" +
74                 "Content-Type: text/xml; charset=utf-8\r\n" +
75                 "Date: Wed, 28 Mar 2001 05:05:04 GMT\r\n" +
76                 "Server: UserLand Frontier/7.0-WinNT\r\n";
77         HttpState state = new HttpState();
78         HttpMethod method = new SimpleHttpMethod();
79         SimpleHttpConnection conn = new SimpleHttpConnection(headers, body);
80         method.execute(state, conn);
81         assertEquals("close", method.getResponseHeader("Connection").getValue());
82         assertEquals(body.length(), Integer.parseInt(method.getResponseHeader("Content-Length").getValue()));
83         assertEquals("text/xml; charset=utf-8", method.getResponseHeader("Content-Type").getValue());
84         assertEquals("Wed, 28 Mar 2001 05:05:04 GMT", method.getResponseHeader("Date").getValue());
85         assertEquals("UserLand Frontier/7.0-WinNT", method.getResponseHeader("Server").getValue());
86     }
87
88     /**
89      * Tests that having a duplicate content length causes no problems.
90      */

91     public void testDuplicateContentLength() throws Exception JavaDoc {
92         
93         String JavaDoc body = "XXX\r\nYYY\r\nZZZ";
94         String JavaDoc headers =
95                 "HTTP/1.1 200 OK\r\n" +
96                 "Content-Length: " + body.length() + "\r\n" +
97                 "Content-Length: " + body.length() + "\r\n";
98         HttpState state = new HttpState();
99         HttpMethod method = new SimpleHttpMethod();
100         SimpleHttpConnection conn = new SimpleHttpConnection(headers, body);
101         method.execute(state, conn);
102         assertNotNull( "Response body is null.", method.getResponseBodyAsStream() );
103                 
104     }
105
106     public void testDuplicateProxyConnection() throws Exception JavaDoc {
107         
108         SimpleHttpConnection conn = new SimpleHttpConnection();
109         String JavaDoc headers =
110             "HTTP/1.1 200 OK\r\n"
111             + "proxy-connection: close\r\n"
112             + "proxy-connection: close\r\n"
113             + "Content-Length: 0\r\n"
114             + "\r\n";
115
116         conn.addResponse(headers, "");
117         conn.setProxyHost("proxy");
118         conn.setProxyPort(1);
119         GetMethod method = new GetMethod("/");
120         method.execute(new HttpState(), conn);
121         method.getResponseBodyAsString();
122         
123         assertFalse(conn.isOpen());
124         
125         conn = new SimpleHttpConnection();
126         headers =
127             "HTTP/1.0 200 OK\r\n"
128             + "proxy-connection: keep-alive\r\n"
129             + "proxy-connection: keep-alive\r\n"
130             + "Content-Length: 0\r\n"
131             + "\r\n";
132
133         conn.addResponse(headers, "");
134         conn.setProxyHost("proxy");
135         conn.setProxyPort(1);
136         method = new GetMethod("/");
137         method.execute(new HttpState(), conn);
138         method.getResponseBodyAsString();
139         
140         assertTrue(conn.isOpen());
141     }
142
143     public void testDuplicateConnection() throws Exception JavaDoc {
144         
145         SimpleHttpConnection conn = new SimpleHttpConnection();
146         String JavaDoc headers =
147             "HTTP/1.1 200 OK\r\n"
148             + "Connection: close\r\n"
149             + "Connection: close\r\n"
150             + "\r\n";
151
152         conn.addResponse(headers, "");
153         GetMethod method = new GetMethod("/");
154         method.execute(new HttpState(), conn);
155         method.getResponseBodyAsString();
156         
157         assertFalse(conn.isOpen());
158
159         conn = new SimpleHttpConnection();
160         headers =
161             "HTTP/1.0 200 OK\r\n"
162             +"Connection: keep-alive\r\n"
163             +"Connection: keep-alive\r\n"
164             + "Content-Length: 0\r\n"
165             +"\r\n";
166
167         conn.addResponse(headers, "");
168         method = new GetMethod("/");
169         method.execute(new HttpState(), conn);
170         method.getResponseBodyAsString();
171         
172         assertTrue(conn.isOpen());
173     }
174     
175     public void testNoContentLength() throws Exception JavaDoc {
176         // test with connection header
177
SimpleHttpConnection conn = new SimpleHttpConnection();
178         String JavaDoc headers =
179             "HTTP/1.1 200 OK\r\n"
180             + "Connection: keep-alive\r\n"
181             + "\r\n";
182
183         conn.addResponse(headers, "12345");
184         GetMethod method = new GetMethod("/");
185         method.execute(new HttpState(), conn);
186         method.getResponseBodyAsString();
187         
188         assertFalse(conn.isOpen());
189         
190         // test without connection header
191
conn = new SimpleHttpConnection();
192         headers = "HTTP/1.1 200 OK\r\n\r\n";
193
194         // test with connection header
195
conn.addResponse(headers, "12345");
196         method = new GetMethod("/");
197         method.execute(new HttpState(), conn);
198         method.getResponseBodyAsString();
199         
200         assertFalse(conn.isOpen());
201     }
202
203     public void testInvalidContentLength1() throws Exception JavaDoc {
204         // test with connection header
205
SimpleHttpConnection conn = new SimpleHttpConnection();
206         String JavaDoc headers = "HTTP/1.1 200 OK\r\n"
207             + "Content-Length: 5\r\n"
208             + "Content-Length: stuff\r\n"
209             + "\r\n";
210
211         // test with connection header
212
conn.addResponse(headers, "12345");
213         GetMethod method = new GetMethod("/");
214         method.execute(new HttpState(), conn);
215         assertEquals(5, method.getResponseContentLength());
216     }
217
218     public void testInvalidContentLength2() throws Exception JavaDoc {
219         // test with connection header
220
SimpleHttpConnection conn = new SimpleHttpConnection();
221         String JavaDoc headers = "HTTP/1.1 200 OK\r\n"
222             + "Content-Length: stuff\r\n"
223             + "Content-Length: 5\r\n"
224             + "\r\n";
225
226         // test with connection header
227
conn.addResponse(headers, "12345");
228         GetMethod method = new GetMethod("/");
229         method.execute(new HttpState(), conn);
230         assertEquals(5, method.getResponseContentLength());
231     }
232
233     public void testProxyNoContentLength() throws Exception JavaDoc {
234         // test with proxy-connection header
235
SimpleHttpConnection conn = new SimpleHttpConnection();
236         String JavaDoc headers =
237             "HTTP/1.1 200 OK\r\n"
238             + "proxy-connection: keep-alive\r\n"
239             + "\r\n";
240
241         conn.addResponse(headers, "12345");
242         conn.setProxyHost("proxy");
243         conn.setProxyPort(1);
244         GetMethod method = new GetMethod("/");
245         method.execute(new HttpState(), conn);
246         method.getResponseBodyAsString();
247         
248         assertFalse(conn.isOpen());
249
250         // test without proxy-connection header
251
conn = new SimpleHttpConnection();
252         headers = "HTTP/1.1 200 OK\r\n\r\n";
253
254         conn.addResponse(headers, "12345");
255         conn.setProxyHost("proxy");
256         conn.setProxyPort(1);
257         method = new GetMethod("/");
258         method.execute(new HttpState(), conn);
259         method.getResponseBodyAsString();
260         
261         assertFalse(conn.isOpen());
262     }
263
264     public void testNullHeaders() throws Exception JavaDoc {
265         String JavaDoc body = "XXX\r\nYYY\r\nZZZ";
266         String JavaDoc headers =
267                 "HTTP/1.1 200 OK\r\n" +
268                 "Content-Length: " + body.length() + "\r\n";
269         HttpState state = new HttpState();
270         HttpMethod method = new SimpleHttpMethod();
271         SimpleHttpConnection conn = new SimpleHttpConnection(headers, body);
272         method.execute(state, conn);
273         assertEquals(null, method.getResponseHeader(null));
274         assertEquals(null, method.getResponseHeader("bogus"));
275     }
276     
277     public void testFoldedHeaders() throws Exception JavaDoc {
278         String JavaDoc body = "XXX\r\nYYY\r\nZZZ";
279         String JavaDoc headers =
280                 "HTTP/1.1 200 OK\r\n" +
281                 "Connection: close\r\n" +
282                 "Content-Length: " + body.length() + "\r\n" +
283                 "Content-Type: text/xml; charset=utf-8\r\n" +
284                 "\tboundary=XXXX\r\n" +
285                 "Date: Wed, 28 Mar 2001\r\n" +
286                 " 05:05:04 GMT\r\n" +
287                 "Server: UserLand Frontier/7.0-WinNT\r\n";
288         HttpState state = new HttpState();
289         HttpMethod method = new SimpleHttpMethod();
290         SimpleHttpConnection conn = new SimpleHttpConnection(headers, body);
291         method.execute(state, conn);
292         assertEquals("close", method.getResponseHeader("Connection").getValue());
293         assertEquals(body.length(), Integer.parseInt(method.getResponseHeader("Content-Length").getValue()));
294         assertEquals("text/xml; charset=utf-8 boundary=XXXX", method.getResponseHeader("Content-Type").getValue());
295         assertEquals("Wed, 28 Mar 2001 05:05:04 GMT", method.getResponseHeader("Date").getValue());
296         assertEquals("UserLand Frontier/7.0-WinNT", method.getResponseHeader("Server").getValue());
297         assertTrue(method.getResponseHeader("Content-Type").toString().indexOf("boundary") != -1);
298     }
299
300
301     public void testForceCloseConnection() throws Exception JavaDoc {
302         String JavaDoc body = "stuff";
303         String JavaDoc headers =
304                 "HTTP/1.1 200 OK\r\n" +
305                 "Content-Type: garbage\r\n";
306         HttpState state = new HttpState();
307         SimpleHttpMethod method = new SimpleHttpMethod();
308         SimpleHttpConnection conn = new SimpleHttpConnection(headers, body);
309         method.execute(state, conn);
310         assertTrue("Connection should be closed", method.shouldCloseConnection(conn));
311         assertTrue("Connection should be force-closed", method.isConnectionCloseForced());
312     }
313     
314     public void testForceCloseConnection2() throws Exception JavaDoc {
315         String JavaDoc body = "stuff";
316         String JavaDoc headers =
317                 "HTTP/1.1 200 OK\r\n" +
318                 "Content-Type: garbage\r\n" +
319                 "Connection: close\r\n";
320         HttpState state = new HttpState();
321         SimpleHttpMethod method = new SimpleHttpMethod();
322         SimpleHttpConnection conn = new SimpleHttpConnection(headers, body);
323         method.execute(state, conn);
324         assertTrue("Connection should be closed", method.shouldCloseConnection(conn));
325         assertFalse("Connection should NOT be closed", method.isConnectionCloseForced());
326     }
327 }
328
Popular Tags