1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestResponseHeaders.java,v 1.14 2004/02/26 20:25:55 olegk Exp $
3    * $Revision: 1.14 $
4    * $Date: 2004/02/26 20:25:55 $
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 junit.framework.Test;
34  import junit.framework.TestSuite;
35  
36  import org.apache.commons.httpclient.methods.GetMethod;
37  
38  /***
39   * Tests for reading response headers.
40   *
41   * @author <a href="mailto:dims@apache.org">Davanum Srinivas</a>
42   * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
43   * @author <a href="mailto:adrian@intencha.com">Adrian Sutton</a>
44   * @version $Id: TestResponseHeaders.java,v 1.14 2004/02/26 20:25:55 olegk Exp $
45   */
46  public class TestResponseHeaders extends TestNoHostBase {
47  
48      // ------------------------------------------------------------ Constructor
49      public TestResponseHeaders(String testName) {
50          super(testName);
51      }
52  
53      // ------------------------------------------------------------------- Main
54      public static void main(String args[]) {
55          String[] testCaseName = {TestResponseHeaders.class.getName()};
56          junit.textui.TestRunner.main(testCaseName);
57      }
58  
59      // ------------------------------------------------------- TestCase Methods
60      public static Test suite() {
61          return new TestSuite(TestResponseHeaders.class);
62      }
63  
64      // ----------------------------------------------------------- Test Methods
65      public void testHeaders() throws Exception {
66          String body = "XXX\r\nYYY\r\nZZZ";
67          String headers =
68                  "HTTP/1.1 200 OK\r\n" +
69                  "Connection: close\r\n" +
70                  "Content-Length: " + body.length() + "\r\n" +
71                  "Content-Type: text/xml; charset=utf-8\r\n" +
72                  "Date: Wed, 28 Mar 2001 05:05:04 GMT\r\n" +
73                  "Server: UserLand Frontier/7.0-WinNT\r\n";
74          HttpMethod method = new SimpleHttpMethod();
75          conn.addResponse(headers, body);
76          client.executeMethod(method);
77          assertEquals("close", method.getResponseHeader("Connection").getValue());
78          assertEquals(body.length(), Integer.parseInt(method.getResponseHeader("Content-Length").getValue()));
79          assertEquals("text/xml; charset=utf-8", method.getResponseHeader("Content-Type").getValue());
80          assertEquals("Wed, 28 Mar 2001 05:05:04 GMT", method.getResponseHeader("Date").getValue());
81          assertEquals("UserLand Frontier/7.0-WinNT", method.getResponseHeader("Server").getValue());
82      }
83  
84      /***
85       * Tests that having a duplicate content length causes no problems.
86       */    
87      public void testDuplicateContentLength() throws Exception {
88          
89          String body = "XXX\r\nYYY\r\nZZZ";
90          String headers =
91                  "HTTP/1.1 200 OK\r\n" +
92                  "Content-Length: " + body.length() + "\r\n" +
93                  "Content-Length: " + body.length() + "\r\n";
94          HttpMethod method = new SimpleHttpMethod();
95          conn.addResponse(headers, body);
96          client.executeMethod(method);
97          assertNotNull( "Response body is null.", method.getResponseBodyAsStream() );
98                  
99      }
100 
101     public void testDuplicateProxyConnection() throws Exception {
102         
103         client.getHostConfiguration().setProxy("proxy", 1);
104 
105         String headers = 
106             "HTTP/1.1 200 OK\r\n"
107             + "proxy-connection: close\r\n"
108             + "proxy-connection: close\r\n"
109             + "Content-Length: 0\r\n"
110             + "\r\n";
111 
112         conn.addResponse(headers, "");
113 
114         GetMethod method = new GetMethod("/");
115         client.executeMethod(method);
116         method.getResponseBodyAsString();
117         
118         assertFalse(conn.isOpen());
119         
120         headers = 
121             "HTTP/1.0 200 OK\r\n"
122             + "proxy-connection: keep-alive\r\n"
123             + "proxy-connection: keep-alive\r\n"
124             + "Content-Length: 2\r\n"
125             + "\r\n";
126 
127         conn.addResponse(headers, "");
128         method = new GetMethod("/");
129         client.executeMethod(method);
130         method.getResponseBodyAsString();
131         
132         assertTrue(conn.isOpen());        
133     }
134 
135     public void testDuplicateConnection() throws Exception {
136         
137         String headers = 
138             "HTTP/1.1 200 OK\r\n"
139             + "Connection: close\r\n"
140             + "Connection: close\r\n"
141             + "Content-Length: 0\r\n"
142             + "\r\n";
143 
144         GetMethod method = new GetMethod("/");
145         conn.addResponse(headers, "");
146         client.executeMethod(method);
147         method.getResponseBodyAsString();
148         
149         assertFalse(conn.isOpen());
150 
151         headers = 
152             "HTTP/1.0 200 OK\r\n"
153             +"Connection: keep-alive\r\n"
154             +"Connection: keep-alive\r\n"
155             + "Content-Length: 2\r\n"
156             +"\r\n";
157 
158         method = new GetMethod("/");
159         conn.addResponse(headers, "");
160         client.executeMethod(method);
161         method.getResponseBodyAsString();
162         
163         assertTrue(conn.isOpen());
164     }
165     
166     public void testNoContentLength() throws Exception {
167         // test with connection header
168         String headers = 
169             "HTTP/1.1 200 OK\r\n"
170             + "Connection: keep-alive\r\n"
171             + "\r\n";
172 
173         GetMethod method = new GetMethod("/");
174         conn.addResponse(headers, "12345");
175         client.executeMethod(method);
176         method.getResponseBodyAsString();
177         
178         assertFalse(conn.isOpen());
179         
180         // test without connection header
181         headers = "HTTP/1.1 200 OK\r\n\r\n";
182 
183         // test with connection header
184         method = new GetMethod("/");
185         conn.addResponse(headers, "12345");
186         client.executeMethod(method);
187         method.getResponseBodyAsString();
188         
189         assertFalse(conn.isOpen());
190     }
191 
192     public void testInvalidContentLength1() throws Exception {
193         // test with connection header
194         String headers = "HTTP/1.1 200 OK\r\n"
195             + "Content-Length: 5\r\n"
196             + "Content-Length: stuff\r\n"
197             + "\r\n";
198 
199         // test with connection header
200         conn.addResponse(headers, "12345");
201         GetMethod method = new GetMethod("/");
202         client.executeMethod(method);
203         assertEquals(5, method.getResponseContentLength()); 
204     }
205 
206     public void testInvalidContentLength2() throws Exception {
207         // test with connection header
208         String headers = "HTTP/1.1 200 OK\r\n"
209             + "Content-Length: stuff\r\n"
210             + "Content-Length: 5\r\n"
211             + "\r\n";
212 
213         // test with connection header
214         conn.addResponse(headers, "12345");
215         GetMethod method = new GetMethod("/");
216         client.executeMethod(method);
217         assertEquals(5, method.getResponseContentLength()); 
218     }
219 
220     public void testProxyNoContentLength() throws Exception {
221         // test with proxy-connection header
222         String headers =
223             "HTTP/1.1 200 OK\r\n"
224             + "proxy-connection: keep-alive\r\n"
225             + "\r\n";
226 
227         conn.setProxyHost("proxy");
228         conn.setProxyPort(1);
229         GetMethod method = new GetMethod("/");
230         conn.addResponse(headers, "12345");
231         client.executeMethod(method);
232         method.getResponseBodyAsString();
233         
234         assertFalse(conn.isOpen());
235 
236         // test without proxy-connection header
237         headers = "HTTP/1.1 200 OK\r\n\r\n";
238 
239         conn.setProxyHost("proxy");
240         conn.setProxyPort(1);
241         method = new GetMethod("/");
242         conn.addResponse(headers, "12345");
243         client.executeMethod(method);
244         method.getResponseBodyAsString();
245         
246         assertFalse(conn.isOpen());
247     }
248 
249     public void testNullHeaders() throws Exception {
250         String body = "XXX\r\nYYY\r\nZZZ";
251         String headers =
252                 "HTTP/1.1 200 OK\r\n" +
253                 "Content-Length: " + body.length() + "\r\n";
254         HttpMethod method = new SimpleHttpMethod();
255         conn.addResponse(headers, body);
256         client.executeMethod(method);
257         assertEquals(null, method.getResponseHeader(null));
258         assertEquals(null, method.getResponseHeader("bogus"));
259     }
260     
261     public void testFoldedHeaders() throws Exception {
262         String body = "XXX\r\nYYY\r\nZZZ";
263         String headers =
264                 "HTTP/1.1 200 OK\r\n" +
265                 "Connection: close\r\n" +
266                 "Content-Length: " + body.length() + "\r\n" +
267                 "Content-Type: text/xml; charset=utf-8\r\n" +
268                 "\tboundary=XXXX\r\n" +
269                 "Date: Wed, 28 Mar 2001\r\n" + 
270                 " 05:05:04 GMT\r\n" +
271                 "Server: UserLand Frontier/7.0-WinNT\r\n";
272         HttpMethod method = new SimpleHttpMethod();
273         conn.addResponse(headers, body);
274         client.executeMethod(method);
275         assertEquals("close", method.getResponseHeader("Connection").getValue());
276         assertEquals(body.length(), Integer.parseInt(method.getResponseHeader("Content-Length").getValue()));
277         assertEquals("text/xml; charset=utf-8 boundary=XXXX", method.getResponseHeader("Content-Type").getValue());
278         assertEquals("Wed, 28 Mar 2001 05:05:04 GMT", method.getResponseHeader("Date").getValue());
279         assertEquals("UserLand Frontier/7.0-WinNT", method.getResponseHeader("Server").getValue());
280         assertTrue(method.getResponseHeader("Content-Type").toString().indexOf("boundary") != -1);
281     }
282 
283 
284 	public void testForceCloseConnection() throws Exception {
285 		String body = "stuff";
286 		String headers =
287 				"HTTP/1.1 200 OK\r\n" +
288 				"Content-Type: garbage\r\n" +
289                 "\r\n";
290 		SimpleHttpMethod method = new SimpleHttpMethod();
291         conn.addResponse(headers, body);
292         client.executeMethod(method);
293 		assertTrue("Connection should be closed", method.shouldCloseConnection(conn));
294 		assertTrue("Connection should be force-closed", method.isConnectionCloseForced());
295 	}
296     
297 	public void testForceCloseConnection2() throws Exception {
298 		String body = "stuff";
299 		String headers =
300 				"HTTP/1.1 200 OK\r\n" +
301 				"Content-Type: garbage\r\n" +
302 				"Connection: close\r\n" +
303                 "\r\n";
304 		SimpleHttpMethod method = new SimpleHttpMethod();
305         conn.addResponse(headers, body);
306         client.executeMethod(method);
307 		assertTrue("Connection should be closed", method.shouldCloseConnection(conn));
308 		assertFalse("Connection should NOT be closed", method.isConnectionCloseForced());
309 	}
310 }