1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestResponseHeaders.java,v 1.8 2003/06/20 13:33:09 adrian Exp $ 3 * $Revision: 1.8 $ 4 * $Date: 2003/06/20 13:33:09 $ 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 2003/06/20 13:33:09 adrian 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 /*** 100 * Simple extension of HttpMethodBase. 101 */ 102 private class SimpleHttpMethod extends HttpMethodBase { 103 public SimpleHttpMethod() { 104 super(""); 105 } 106 public String getName() { 107 return "simple"; 108 } 109 } 110 111 // ----------------------------------------------------------- Test Methods 112 public void testHeaders() throws Exception { 113 String body = "XXX\r\nYYY\r\nZZZ"; 114 String headers = 115 "HTTP/1.1 200 OK\r\n" + 116 "Connection: close\r\n" + 117 "Content-Length: " + body.length() + "\r\n" + 118 "Content-Type: text/xml; charset=utf-8\r\n" + 119 "Date: Wed, 28 Mar 2001 05:05:04 GMT\r\n" + 120 "Server: UserLand Frontier/7.0-WinNT\r\n"; 121 HttpState state = new HttpState(); 122 HttpMethod method = new SimpleHttpMethod(); 123 SimpleHttpConnection conn = new SimpleHttpConnection(headers, body); 124 method.execute(state, conn); 125 assertEquals("close", method.getResponseHeader("Connection").getValue()); 126 assertEquals(body.length(), Integer.parseInt(method.getResponseHeader("Content-Length").getValue())); 127 assertEquals("text/xml; charset=utf-8", method.getResponseHeader("Content-Type").getValue()); 128 assertEquals("Wed, 28 Mar 2001 05:05:04 GMT", method.getResponseHeader("Date").getValue()); 129 assertEquals("UserLand Frontier/7.0-WinNT", method.getResponseHeader("Server").getValue()); 130 } 131 132 /*** 133 * Tests that having a duplicate content length causes no problems. 134 */ 135 public void testDuplicateContentLength() throws Exception { 136 137 String body = "XXX\r\nYYY\r\nZZZ"; 138 String headers = 139 "HTTP/1.1 200 OK\r\n" + 140 "Content-Length: " + body.length() + "\r\n" + 141 "Content-Length: " + body.length() + "\r\n"; 142 HttpState state = new HttpState(); 143 HttpMethod method = new SimpleHttpMethod(); 144 SimpleHttpConnection conn = new SimpleHttpConnection(headers, body); 145 method.execute(state, conn); 146 assertNotNull( "Response body is null.", method.getResponseBodyAsStream() ); 147 148 } 149 150 public void testDuplicateProxyConnection() throws Exception { 151 152 SimpleHttpConnection conn = new SimpleHttpConnection(); 153 String headers = 154 "HTTP/1.1 200 OK\r\n" 155 + "proxy-connection: close\r\n" 156 + "proxy-connection: close\r\n" 157 + "Content-Length: 0\r\n" 158 + "\r\n"; 159 160 conn.addResponse(headers, ""); 161 conn.setProxyHost("proxy"); 162 conn.setProxyPort(1); 163 GetMethod method = new GetMethod("/"); 164 method.execute(new HttpState(), conn); 165 method.getResponseBodyAsString(); 166 167 assertFalse(conn.isOpen()); 168 169 conn = new SimpleHttpConnection(); 170 headers = 171 "HTTP/1.0 200 OK\r\n" 172 + "proxy-connection: keep-alive\r\n" 173 + "proxy-connection: keep-alive\r\n" 174 + "Content-Length: 0\r\n" 175 + "\r\n"; 176 177 conn.addResponse(headers, ""); 178 conn.setProxyHost("proxy"); 179 conn.setProxyPort(1); 180 method = new GetMethod("/"); 181 method.execute(new HttpState(), conn); 182 method.getResponseBodyAsString(); 183 184 assertTrue(conn.isOpen()); 185 } 186 187 public void testDuplicateConnection() throws Exception { 188 189 SimpleHttpConnection conn = new SimpleHttpConnection(); 190 String headers = 191 "HTTP/1.1 200 OK\r\n" 192 + "Connection: close\r\n" 193 + "Connection: close\r\n" 194 + "\r\n"; 195 196 conn.addResponse(headers, ""); 197 GetMethod method = new GetMethod("/"); 198 method.execute(new HttpState(), conn); 199 method.getResponseBodyAsString(); 200 201 assertFalse(conn.isOpen()); 202 203 conn = new SimpleHttpConnection(); 204 headers = 205 "HTTP/1.0 200 OK\r\n" 206 +"Connection: keep-alive\r\n" 207 +"Connection: keep-alive\r\n" 208 + "Content-Length: 0\r\n" 209 +"\r\n"; 210 211 conn.addResponse(headers, ""); 212 method = new GetMethod("/"); 213 method.execute(new HttpState(), conn); 214 method.getResponseBodyAsString(); 215 216 assertTrue(conn.isOpen()); 217 } 218 219 public void testNoContentLength() throws Exception { 220 // test with connection header 221 SimpleHttpConnection conn = new SimpleHttpConnection(); 222 String headers = 223 "HTTP/1.1 200 OK\r\n" 224 + "Connection: keep-alive\r\n" 225 + "\r\n"; 226 227 conn.addResponse(headers, "12345"); 228 GetMethod method = new GetMethod("/"); 229 method.execute(new HttpState(), conn); 230 method.getResponseBodyAsString(); 231 232 assertFalse(conn.isOpen()); 233 234 // test without connection header 235 conn = new SimpleHttpConnection(); 236 headers = "HTTP/1.1 200 OK\r\n\r\n"; 237 238 // test with connection header 239 conn.addResponse(headers, "12345"); 240 method = new GetMethod("/"); 241 method.execute(new HttpState(), conn); 242 method.getResponseBodyAsString(); 243 244 assertFalse(conn.isOpen()); 245 } 246 247 public void testProxyNoContentLength() throws Exception { 248 // test with proxy-connection header 249 SimpleHttpConnection conn = new SimpleHttpConnection(); 250 String headers = 251 "HTTP/1.1 200 OK\r\n" 252 + "proxy-connection: keep-alive\r\n" 253 + "\r\n"; 254 255 conn.addResponse(headers, "12345"); 256 conn.setProxyHost("proxy"); 257 conn.setProxyPort(1); 258 GetMethod method = new GetMethod("/"); 259 method.execute(new HttpState(), conn); 260 method.getResponseBodyAsString(); 261 262 assertFalse(conn.isOpen()); 263 264 // test without proxy-connection header 265 conn = new SimpleHttpConnection(); 266 headers = "HTTP/1.1 200 OK\r\n\r\n"; 267 268 conn.addResponse(headers, "12345"); 269 conn.setProxyHost("proxy"); 270 conn.setProxyPort(1); 271 method = new GetMethod("/"); 272 method.execute(new HttpState(), conn); 273 method.getResponseBodyAsString(); 274 275 assertFalse(conn.isOpen()); 276 } 277 278 public void testNullHeaders() throws Exception { 279 String body = "XXX\r\nYYY\r\nZZZ"; 280 String headers = 281 "HTTP/1.1 200 OK\r\n" + 282 "Content-Length: " + body.length() + "\r\n"; 283 HttpState state = new HttpState(); 284 HttpMethod method = new SimpleHttpMethod(); 285 SimpleHttpConnection conn = new SimpleHttpConnection(headers, body); 286 method.execute(state, conn); 287 assertEquals(null, method.getResponseHeader(null)); 288 assertEquals(null, method.getResponseHeader("bogus")); 289 } 290 291 public void testFoldedHeaders() throws Exception { 292 String body = "XXX\r\nYYY\r\nZZZ"; 293 String headers = 294 "HTTP/1.1 200 OK\r\n" + 295 "Connection: close\r\n" + 296 "Content-Length: " + body.length() + "\r\n" + 297 "Content-Type: text/xml; charset=utf-8\r\n" + 298 "\tboundary=XXXX\r\n" + 299 "Date: Wed, 28 Mar 2001\r\n" + 300 " 05:05:04 GMT\r\n" + 301 "Server: UserLand Frontier/7.0-WinNT\r\n"; 302 HttpState state = new HttpState(); 303 HttpMethod method = new SimpleHttpMethod(); 304 SimpleHttpConnection conn = new SimpleHttpConnection(headers, body); 305 method.execute(state, conn); 306 assertEquals("close", method.getResponseHeader("Connection").getValue()); 307 assertEquals(body.length(), Integer.parseInt(method.getResponseHeader("Content-Length").getValue())); 308 assertEquals("text/xml; charset=utf-8 boundary=XXXX", method.getResponseHeader("Content-Type").getValue()); 309 assertEquals("Wed, 28 Mar 2001 05:05:04 GMT", method.getResponseHeader("Date").getValue()); 310 assertEquals("UserLand Frontier/7.0-WinNT", method.getResponseHeader("Server").getValue()); 311 assertTrue(method.getResponseHeader("Content-Type").toString().indexOf("boundary") != -1); 312 } 313 }

This page was automatically generated by Maven