1 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 47 public class TestResponseHeaders extends TestCase { 48 49 public TestResponseHeaders(String testName) { 51 super(testName); 52 } 53 54 public static void main(String args[]) { 56 String [] testCaseName = {TestResponseHeaders.class.getName()}; 57 junit.textui.TestRunner.main(testCaseName); 58 } 59 60 public static Test suite() { 62 return new TestSuite(TestResponseHeaders.class); 63 } 64 65 66 67 public void testHeaders() throws Exception { 69 String body = "XXX\r\nYYY\r\nZZZ"; 70 String 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 91 public void testDuplicateContentLength() throws Exception { 92 93 String body = "XXX\r\nYYY\r\nZZZ"; 94 String 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 { 107 108 SimpleHttpConnection conn = new SimpleHttpConnection(); 109 String 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 { 144 145 SimpleHttpConnection conn = new SimpleHttpConnection(); 146 String 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 { 176 SimpleHttpConnection conn = new SimpleHttpConnection(); 178 String 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 conn = new SimpleHttpConnection(); 192 headers = "HTTP/1.1 200 OK\r\n\r\n"; 193 194 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 { 204 SimpleHttpConnection conn = new SimpleHttpConnection(); 206 String 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 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 { 219 SimpleHttpConnection conn = new SimpleHttpConnection(); 221 String 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 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 { 234 SimpleHttpConnection conn = new SimpleHttpConnection(); 236 String 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 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 { 265 String body = "XXX\r\nYYY\r\nZZZ"; 266 String 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 { 278 String body = "XXX\r\nYYY\r\nZZZ"; 279 String 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 { 302 String body = "stuff"; 303 String 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 { 315 String body = "stuff"; 316 String 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 |