1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestResponseHeaders.java,v 1.8.2.1 2003/11/03 22:40:29 olegk Exp $ 3 * $Revision: 1.8.2.1 $ 4 * $Date: 2003/11/03 22:40:29 $ 5 * ==================================================================== 6 * 7 * The Apache Software License, Version 1.1 8 * 9 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights 10 * reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in 21 * the documentation and/or other materials provided with the 22 * distribution. 23 * 24 * 3. The end-user documentation included with the redistribution, if 25 * any, must include the following acknowlegement: 26 * "This product includes software developed by the 27 * Apache Software Foundation (http://www.apache.org/)." 28 * Alternately, this acknowlegement may appear in the software itself, 29 * if and wherever such third-party acknowlegements normally appear. 30 * 31 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software 32 * Foundation" must not be used to endorse or promote products derived 33 * from this software without prior written permission. For written 34 * permission, please contact apache@apache.org. 35 * 36 * 5. Products derived from this software may not be called "Apache" 37 * nor may "Apache" appear in their names without prior written 38 * permission of the Apache Group. 39 * 40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 41 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 42 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 43 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 46 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 47 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 49 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 50 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * ==================================================================== 53 * 54 * This software consists of voluntary contributions made by many 55 * individuals on behalf of the Apache Software Foundation. For more 56 * information on the Apache Software Foundation, please see 57 * <http://www.apache.org/>. 58 * 59 * [Additional notices, if required by prior licensing conditions] 60 * 61 */ 62 63 package org.apache.commons.httpclient; 64 65 import org.apache.commons.httpclient.methods.GetMethod; 66 67 import junit.framework.Test; 68 import junit.framework.TestCase; 69 import junit.framework.TestSuite; 70 71 /*** 72 * Tests for reading response headers. 73 * 74 * @author <a href="mailto:dims@apache.org">Davanum Srinivas</a> 75 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a> 76 * @author <a href="mailto:adrian@intencha.com">Adrian Sutton</a> 77 * @version $Id: TestResponseHeaders.java,v 1.8.2.1 2003/11/03 22:40:29 olegk Exp $ 78 */ 79 public class TestResponseHeaders extends TestCase { 80 81 // ------------------------------------------------------------ Constructor 82 public TestResponseHeaders(String testName) { 83 super(testName); 84 } 85 86 // ------------------------------------------------------------------- Main 87 public static void main(String args[]) { 88 String[] testCaseName = {TestResponseHeaders.class.getName()}; 89 junit.textui.TestRunner.main(testCaseName); 90 } 91 92 // ------------------------------------------------------- TestCase Methods 93 public static Test suite() { 94 return new TestSuite(TestResponseHeaders.class); 95 } 96 97 98 99 // ----------------------------------------------------------- Test Methods 100 public void testHeaders() throws Exception { 101 String body = "XXX\r\nYYY\r\nZZZ"; 102 String headers = 103 "HTTP/1.1 200 OK\r\n" + 104 "Connection: close\r\n" + 105 "Content-Length: " + body.length() + "\r\n" + 106 "Content-Type: text/xml; charset=utf-8\r\n" + 107 "Date: Wed, 28 Mar 2001 05:05:04 GMT\r\n" + 108 "Server: UserLand Frontier/7.0-WinNT\r\n"; 109 HttpState state = new HttpState(); 110 HttpMethod method = new SimpleHttpMethod(); 111 SimpleHttpConnection conn = new SimpleHttpConnection(headers, body); 112 method.execute(state, conn); 113 assertEquals("close", method.getResponseHeader("Connection").getValue()); 114 assertEquals(body.length(), Integer.parseInt(method.getResponseHeader("Content-Length").getValue())); 115 assertEquals("text/xml; charset=utf-8", method.getResponseHeader("Content-Type").getValue()); 116 assertEquals("Wed, 28 Mar 2001 05:05:04 GMT", method.getResponseHeader("Date").getValue()); 117 assertEquals("UserLand Frontier/7.0-WinNT", method.getResponseHeader("Server").getValue()); 118 } 119 120 /*** 121 * Tests that having a duplicate content length causes no problems. 122 */ 123 public void testDuplicateContentLength() throws Exception { 124 125 String body = "XXX\r\nYYY\r\nZZZ"; 126 String headers = 127 "HTTP/1.1 200 OK\r\n" + 128 "Content-Length: " + body.length() + "\r\n" + 129 "Content-Length: " + body.length() + "\r\n"; 130 HttpState state = new HttpState(); 131 HttpMethod method = new SimpleHttpMethod(); 132 SimpleHttpConnection conn = new SimpleHttpConnection(headers, body); 133 method.execute(state, conn); 134 assertNotNull( "Response body is null.", method.getResponseBodyAsStream() ); 135 136 } 137 138 public void testDuplicateProxyConnection() throws Exception { 139 140 SimpleHttpConnection conn = new SimpleHttpConnection(); 141 String headers = 142 "HTTP/1.1 200 OK\r\n" 143 + "proxy-connection: close\r\n" 144 + "proxy-connection: close\r\n" 145 + "Content-Length: 0\r\n" 146 + "\r\n"; 147 148 conn.addResponse(headers, ""); 149 conn.setProxyHost("proxy"); 150 conn.setProxyPort(1); 151 GetMethod method = new GetMethod("/"); 152 method.execute(new HttpState(), conn); 153 method.getResponseBodyAsString(); 154 155 assertFalse(conn.isOpen()); 156 157 conn = new SimpleHttpConnection(); 158 headers = 159 "HTTP/1.0 200 OK\r\n" 160 + "proxy-connection: keep-alive\r\n" 161 + "proxy-connection: keep-alive\r\n" 162 + "Content-Length: 0\r\n" 163 + "\r\n"; 164 165 conn.addResponse(headers, ""); 166 conn.setProxyHost("proxy"); 167 conn.setProxyPort(1); 168 method = new GetMethod("/"); 169 method.execute(new HttpState(), conn); 170 method.getResponseBodyAsString(); 171 172 assertTrue(conn.isOpen()); 173 } 174 175 public void testDuplicateConnection() throws Exception { 176 177 SimpleHttpConnection conn = new SimpleHttpConnection(); 178 String headers = 179 "HTTP/1.1 200 OK\r\n" 180 + "Connection: close\r\n" 181 + "Connection: close\r\n" 182 + "\r\n"; 183 184 conn.addResponse(headers, ""); 185 GetMethod method = new GetMethod("/"); 186 method.execute(new HttpState(), conn); 187 method.getResponseBodyAsString(); 188 189 assertFalse(conn.isOpen()); 190 191 conn = new SimpleHttpConnection(); 192 headers = 193 "HTTP/1.0 200 OK\r\n" 194 +"Connection: keep-alive\r\n" 195 +"Connection: keep-alive\r\n" 196 + "Content-Length: 0\r\n" 197 +"\r\n"; 198 199 conn.addResponse(headers, ""); 200 method = new GetMethod("/"); 201 method.execute(new HttpState(), conn); 202 method.getResponseBodyAsString(); 203 204 assertTrue(conn.isOpen()); 205 } 206 207 public void testNoContentLength() throws Exception { 208 // test with connection header 209 SimpleHttpConnection conn = new SimpleHttpConnection(); 210 String headers = 211 "HTTP/1.1 200 OK\r\n" 212 + "Connection: keep-alive\r\n" 213 + "\r\n"; 214 215 conn.addResponse(headers, "12345"); 216 GetMethod method = new GetMethod("/"); 217 method.execute(new HttpState(), conn); 218 method.getResponseBodyAsString(); 219 220 assertFalse(conn.isOpen()); 221 222 // test without connection header 223 conn = new SimpleHttpConnection(); 224 headers = "HTTP/1.1 200 OK\r\n\r\n"; 225 226 // test with connection header 227 conn.addResponse(headers, "12345"); 228 method = new GetMethod("/"); 229 method.execute(new HttpState(), conn); 230 method.getResponseBodyAsString(); 231 232 assertFalse(conn.isOpen()); 233 } 234 235 public void testProxyNoContentLength() throws Exception { 236 // test with proxy-connection header 237 SimpleHttpConnection conn = new SimpleHttpConnection(); 238 String headers = 239 "HTTP/1.1 200 OK\r\n" 240 + "proxy-connection: keep-alive\r\n" 241 + "\r\n"; 242 243 conn.addResponse(headers, "12345"); 244 conn.setProxyHost("proxy"); 245 conn.setProxyPort(1); 246 GetMethod method = new GetMethod("/"); 247 method.execute(new HttpState(), conn); 248 method.getResponseBodyAsString(); 249 250 assertFalse(conn.isOpen()); 251 252 // test without proxy-connection header 253 conn = new SimpleHttpConnection(); 254 headers = "HTTP/1.1 200 OK\r\n\r\n"; 255 256 conn.addResponse(headers, "12345"); 257 conn.setProxyHost("proxy"); 258 conn.setProxyPort(1); 259 method = new GetMethod("/"); 260 method.execute(new HttpState(), conn); 261 method.getResponseBodyAsString(); 262 263 assertFalse(conn.isOpen()); 264 } 265 266 public void testNullHeaders() throws Exception { 267 String body = "XXX\r\nYYY\r\nZZZ"; 268 String headers = 269 "HTTP/1.1 200 OK\r\n" + 270 "Content-Length: " + body.length() + "\r\n"; 271 HttpState state = new HttpState(); 272 HttpMethod method = new SimpleHttpMethod(); 273 SimpleHttpConnection conn = new SimpleHttpConnection(headers, body); 274 method.execute(state, conn); 275 assertEquals(null, method.getResponseHeader(null)); 276 assertEquals(null, method.getResponseHeader("bogus")); 277 } 278 279 public void testFoldedHeaders() throws Exception { 280 String body = "XXX\r\nYYY\r\nZZZ"; 281 String headers = 282 "HTTP/1.1 200 OK\r\n" + 283 "Connection: close\r\n" + 284 "Content-Length: " + body.length() + "\r\n" + 285 "Content-Type: text/xml; charset=utf-8\r\n" + 286 "\tboundary=XXXX\r\n" + 287 "Date: Wed, 28 Mar 2001\r\n" + 288 " 05:05:04 GMT\r\n" + 289 "Server: UserLand Frontier/7.0-WinNT\r\n"; 290 HttpState state = new HttpState(); 291 HttpMethod method = new SimpleHttpMethod(); 292 SimpleHttpConnection conn = new SimpleHttpConnection(headers, body); 293 method.execute(state, conn); 294 assertEquals("close", method.getResponseHeader("Connection").getValue()); 295 assertEquals(body.length(), Integer.parseInt(method.getResponseHeader("Content-Length").getValue())); 296 assertEquals("text/xml; charset=utf-8 boundary=XXXX", method.getResponseHeader("Content-Type").getValue()); 297 assertEquals("Wed, 28 Mar 2001 05:05:04 GMT", method.getResponseHeader("Date").getValue()); 298 assertEquals("UserLand Frontier/7.0-WinNT", method.getResponseHeader("Server").getValue()); 299 assertTrue(method.getResponseHeader("Content-Type").toString().indexOf("boundary") != -1); 300 } 301 302 303 public void testForceCloseConnection() throws Exception { 304 String body = "stuff"; 305 String headers = 306 "HTTP/1.1 200 OK\r\n" + 307 "Content-Type: garbage\r\n"; 308 HttpState state = new HttpState(); 309 SimpleHttpMethod method = new SimpleHttpMethod(); 310 SimpleHttpConnection conn = new SimpleHttpConnection(headers, body); 311 method.execute(state, conn); 312 assertTrue("Connection should be closed", method.shouldCloseConnection(conn)); 313 assertTrue("Connection should be force-closed", method.isConnectionCloseForced()); 314 } 315 316 public void testForceCloseConnection2() throws Exception { 317 String body = "stuff"; 318 String headers = 319 "HTTP/1.1 200 OK\r\n" + 320 "Content-Type: garbage\r\n" + 321 "Connection: close\r\n"; 322 HttpState state = new HttpState(); 323 SimpleHttpMethod method = new SimpleHttpMethod(); 324 SimpleHttpConnection conn = new SimpleHttpConnection(headers, body); 325 method.execute(state, conn); 326 assertTrue("Connection should be closed", method.shouldCloseConnection(conn)); 327 assertFalse("Connection should NOT be closed", method.isConnectionCloseForced()); 328 } 329 }

This page was automatically generated by Maven