1 //======================================================================== 2 //$Id: HttpGeneratorTest.java,v 1.1 2005/10/05 14:09:41 janb Exp $ 3 //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd. 4 //------------------------------------------------------------------------ 5 //Licensed under the Apache License, Version 2.0 (the "License"); 6 //you may not use this file except in compliance with the License. 7 //You may obtain a copy of the License at 8 //http://www.apache.org/licenses/LICENSE-2.0 9 //Unless required by applicable law or agreed to in writing, software 10 //distributed under the License is distributed on an "AS IS" BASIS, 11 //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 //See the License for the specific language governing permissions and 13 //limitations under the License. 14 //======================================================================== 15 16 package org.mortbay.jetty; 17 18 19 import java.io.IOException; 20 import java.io.InputStreamReader; 21 import java.io.LineNumberReader; 22 import java.io.PrintWriter; 23 import java.net.Socket; 24 import java.util.Enumeration; 25 import java.util.Locale; 26 import java.util.Map; 27 28 import javax.servlet.ServletContext; 29 import javax.servlet.ServletException; 30 import javax.servlet.http.HttpServletRequest; 31 import javax.servlet.http.HttpServletResponse; 32 import javax.servlet.http.HttpSessionContext; 33 34 import junit.framework.TestCase; 35 36 import org.mortbay.jetty.bio.SocketConnector; 37 import org.mortbay.jetty.handler.AbstractHandler; 38 import org.mortbay.jetty.handler.ContextHandler; 39 import org.mortbay.jetty.servlet.AbstractSessionManager; 40 import org.mortbay.jetty.servlet.HashSessionIdManager; 41 import org.mortbay.jetty.servlet.HashSessionManager; 42 43 /** 44 * @author gregw 45 * 46 * To change the template for this generated type comment go to 47 * Window - Preferences - Java - Code Generation - Code and Comments 48 */ 49 public class ResponseTest extends TestCase 50 { 51 Server server = new Server(); 52 LocalConnector connector = new LocalConnector(); 53 ResponseTest(String arg0)54 public ResponseTest(String arg0) 55 { 56 super(arg0); 57 server.setConnectors(new Connector[]{connector}); 58 server.setHandler(new DumpHandler()); 59 } 60 main(String[] args)61 public static void main(String[] args) 62 { 63 junit.textui.TestRunner.run(ResponseTest.class); 64 } 65 66 /* 67 * @see TestCase#setUp() 68 */ setUp()69 protected void setUp() throws Exception 70 { 71 super.setUp(); 72 73 server.start(); 74 } 75 76 /* 77 * @see TestCase#tearDown() 78 */ tearDown()79 protected void tearDown() throws Exception 80 { 81 super.tearDown(); 82 server.stop(); 83 } 84 85 testContentType()86 public void testContentType() 87 throws Exception 88 { 89 90 HttpConnection connection = new HttpConnection(connector,connector._endp,connector._server); 91 Response response = connection.getResponse(); 92 93 assertEquals(null,response.getContentType()); 94 95 response.setContentType("foo/bar"); 96 assertEquals("foo/bar",response.getContentType()); 97 response.getWriter(); 98 assertEquals("foo/bar; charset=ISO-8859-1",response.getContentType()); 99 response.setContentType("foo2/bar2"); 100 assertEquals("foo2/bar2; charset=ISO-8859-1",response.getContentType()); 101 response.setHeader("name","foo"); 102 Enumeration en=response.getHeaders("name"); 103 assertEquals("foo",en.nextElement()); 104 assertFalse(en.hasMoreElements()); 105 response.addHeader("name","bar"); 106 en=response.getHeaders("name"); 107 assertEquals("foo",en.nextElement()); 108 assertEquals("bar",en.nextElement()); 109 assertFalse(en.hasMoreElements()); 110 111 response.recycle(); 112 113 response.setContentType("text/html"); 114 assertEquals("text/html",response.getContentType()); 115 response.getWriter(); 116 assertEquals("text/html; charset=iso-8859-1",response.getContentType()); 117 response.setContentType("foo2/bar2"); 118 assertEquals("foo2/bar2; charset=ISO-8859-1",response.getContentType()); 119 120 response.recycle(); 121 } 122 123 testLocale()124 public void testLocale() 125 throws Exception 126 { 127 128 HttpConnection connection = new HttpConnection(connector,connector._endp,connector._server); 129 Request request = connection.getRequest(); 130 Response response = connection.getResponse(); 131 ContextHandler context = new ContextHandler(); 132 context.addLocaleEncoding(Locale.ENGLISH.toString(),"ISO-8859-1"); 133 context.addLocaleEncoding(Locale.ITALIAN.toString(),"ISO-8859-2"); 134 request.setContext(context.getServletContext()); 135 136 response.setLocale(java.util.Locale.ITALIAN); 137 assertEquals(null,response.getContentType()); 138 response.setContentType("text/plain"); 139 assertEquals("text/plain; charset=ISO-8859-2",response.getContentType()); 140 141 response.recycle(); 142 response.setContentType("text/plain"); 143 response.setCharacterEncoding("utf-8"); 144 response.setLocale(java.util.Locale.ITALIAN); 145 assertEquals("text/plain; charset=utf-8",response.getContentType()); 146 assertTrue(response.toString().indexOf("charset=utf-8")>0); 147 } 148 testContentTypeCharacterEncoding()149 public void testContentTypeCharacterEncoding() 150 throws Exception 151 { 152 HttpConnection connection = new HttpConnection(connector,connector._endp,connector._server); 153 154 Request request = connection.getRequest(); 155 Response response = connection.getResponse(); 156 157 158 response.setContentType("foo/bar"); 159 response.setCharacterEncoding("utf-8"); 160 assertEquals("foo/bar; charset=utf-8",response.getContentType()); 161 response.getWriter(); 162 assertEquals("foo/bar; charset=utf-8",response.getContentType()); 163 response.setContentType("foo2/bar2"); 164 assertEquals("foo2/bar2; charset=utf-8",response.getContentType()); 165 response.setCharacterEncoding("ISO-8859-1"); 166 assertEquals("foo2/bar2; charset=utf-8",response.getContentType()); 167 168 response.recycle(); 169 170 response.setContentType("text/html"); 171 response.setCharacterEncoding("utf-8"); 172 assertEquals("text/html; charset=utf-8",response.getContentType()); 173 response.getWriter(); 174 assertEquals("text/html; charset=utf-8",response.getContentType()); 175 response.setContentType("text/xml"); 176 assertEquals("text/xml; charset=utf-8",response.getContentType()); 177 response.setCharacterEncoding("ISO-8859-1"); 178 assertEquals("text/xml; charset=utf-8",response.getContentType()); 179 180 } 181 testCharacterEncodingContentType()182 public void testCharacterEncodingContentType() 183 throws Exception 184 { 185 Response response = new Response(new HttpConnection(connector,connector._endp,connector._server)); 186 187 response.setCharacterEncoding("utf-8"); 188 response.setContentType("foo/bar"); 189 assertEquals("foo/bar; charset=utf-8",response.getContentType()); 190 response.getWriter(); 191 assertEquals("foo/bar; charset=utf-8",response.getContentType()); 192 response.setContentType("foo2/bar2"); 193 assertEquals("foo2/bar2; charset=utf-8",response.getContentType()); 194 response.setCharacterEncoding("ISO-8859-1"); 195 assertEquals("foo2/bar2; charset=utf-8",response.getContentType()); 196 197 response.recycle(); 198 199 response.setCharacterEncoding("utf-8"); 200 response.setContentType("text/html"); 201 assertEquals("text/html; charset=utf-8",response.getContentType()); 202 response.getWriter(); 203 assertEquals("text/html; charset=utf-8",response.getContentType()); 204 response.setContentType("text/xml"); 205 assertEquals("text/xml; charset=utf-8",response.getContentType()); 206 response.setCharacterEncoding("iso-8859-1"); 207 assertEquals("text/xml; charset=utf-8",response.getContentType()); 208 209 } 210 testContentTypeWithCharacterEncoding()211 public void testContentTypeWithCharacterEncoding() 212 throws Exception 213 { 214 Response response = new Response(new HttpConnection(connector,connector._endp,connector._server)); 215 216 response.setCharacterEncoding("utf16"); 217 response.setContentType("foo/bar; charset=utf-8"); 218 assertEquals("foo/bar; charset=utf-8",response.getContentType()); 219 response.getWriter(); 220 assertEquals("foo/bar; charset=utf-8",response.getContentType()); 221 response.setContentType("foo2/bar2"); 222 assertEquals("foo2/bar2; charset=utf-8",response.getContentType()); 223 response.setCharacterEncoding("ISO-8859-1"); 224 assertEquals("foo2/bar2; charset=utf-8",response.getContentType()); 225 226 response.recycle(); 227 228 response.setCharacterEncoding("utf16"); 229 response.setContentType("text/html; charset=utf-8"); 230 assertEquals("text/html; charset=utf-8",response.getContentType()); 231 response.getWriter(); 232 assertEquals("text/html; charset=utf-8",response.getContentType()); 233 response.setContentType("text/xml"); 234 assertEquals("text/xml; charset=utf-8",response.getContentType()); 235 response.setCharacterEncoding("iso-8859-1"); 236 assertEquals("text/xml; charset=utf-8",response.getContentType()); 237 238 } 239 testContentTypeWithOther()240 public void testContentTypeWithOther() 241 throws Exception 242 { 243 Response response = new Response(new HttpConnection(connector,connector._endp,connector._server)); 244 245 response.setContentType("foo/bar; other=xyz"); 246 assertEquals("foo/bar; other=xyz",response.getContentType()); 247 response.getWriter(); 248 assertEquals("foo/bar; other=xyz charset=ISO-8859-1",response.getContentType()); 249 response.setContentType("foo2/bar2"); 250 assertEquals("foo2/bar2; charset=ISO-8859-1",response.getContentType()); 251 252 response.recycle(); 253 254 response.setCharacterEncoding("utf-8"); 255 response.setContentType("text/html; other=xyz"); 256 assertEquals("text/html; other=xyz charset=utf-8",response.getContentType()); 257 response.getWriter(); 258 assertEquals("text/html; other=xyz charset=utf-8",response.getContentType()); 259 response.setContentType("text/xml"); 260 assertEquals("text/xml; charset=utf-8",response.getContentType()); 261 } 262 263 testContentTypeWithCharacterEncodingAndOther()264 public void testContentTypeWithCharacterEncodingAndOther() 265 throws Exception 266 { 267 Response response = new Response(new HttpConnection(connector,connector._endp,connector._server)); 268 269 response.setCharacterEncoding("utf16"); 270 response.setContentType("foo/bar; charset=utf-8 other=xyz"); 271 assertEquals("foo/bar; charset=utf-8 other=xyz",response.getContentType()); 272 response.getWriter(); 273 assertEquals("foo/bar; charset=utf-8 other=xyz",response.getContentType()); 274 275 response.recycle(); 276 277 response.setCharacterEncoding("utf16"); 278 response.setContentType("text/html; other=xyz charset=utf-8"); 279 assertEquals("text/html; other=xyz charset=utf-8",response.getContentType()); 280 response.getWriter(); 281 assertEquals("text/html; other=xyz charset=utf-8",response.getContentType()); 282 283 response.recycle(); 284 285 response.setCharacterEncoding("utf16"); 286 response.setContentType("foo/bar; other=pq charset=utf-8 other=xyz"); 287 assertEquals("foo/bar; other=pq charset=utf-8 other=xyz",response.getContentType()); 288 response.getWriter(); 289 assertEquals("foo/bar; other=pq charset=utf-8 other=xyz",response.getContentType()); 290 291 } 292 testStatusCodes()293 public void testStatusCodes() throws Exception 294 { 295 Response response=newResponse(); 296 297 response.sendError(404); 298 assertEquals(404, response.getStatus()); 299 assertEquals(null, response.getReason()); 300 301 response=newResponse(); 302 303 response.sendError(500, "Database Error"); 304 assertEquals(500, response.getStatus()); 305 assertEquals("Database Error", response.getReason()); 306 assertEquals("must-revalidate,no-cache,no-store", response.getHeader(HttpHeaders.CACHE_CONTROL)); 307 308 response=newResponse(); 309 310 response.setStatus(200); 311 assertEquals(200, response.getStatus()); 312 assertEquals(null, response.getReason()); 313 314 response=newResponse(); 315 316 response.sendError(406, "Super Nanny"); 317 assertEquals(406, response.getStatus()); 318 assertEquals("Super Nanny", response.getReason()); 319 assertEquals("must-revalidate,no-cache,no-store", response.getHeader(HttpHeaders.CACHE_CONTROL)); 320 } 321 testEncodeRedirect()322 public void testEncodeRedirect() 323 throws Exception 324 { 325 HttpConnection connection=new HttpConnection(connector,connector._endp,connector._server); 326 Response response = new Response(connection); 327 Request request = connection.getRequest(); 328 329 assertEquals("http://host:port/path/info;param?query=0&more=1#target",response.encodeRedirectUrl("http://host:port/path/info;param?query=0&more=1#target")); 330 331 request.setRequestedSessionId("12345"); 332 request.setRequestedSessionIdFromCookie(false); 333 AbstractSessionManager manager=new HashSessionManager(); 334 manager.setIdManager(new HashSessionIdManager()); 335 request.setSessionManager(manager); 336 request.setSession(new TestSession(manager,"12345")); 337 338 assertEquals("http://host:port/path/info;param;jsessionid=12345?query=0&more=1#target",response.encodeRedirectUrl("http://host:port/path/info;param?query=0&more=1#target")); 339 340 } 341 testSetBufferSize()342 public void testSetBufferSize () 343 throws Exception 344 { 345 Response response = new Response(new HttpConnection(connector,connector._endp,connector._server)); 346 response.setBufferSize(20*1024); 347 response.getWriter().print("hello"); 348 try 349 { 350 response.setBufferSize(21*1024); 351 fail("Expected IllegalStateException on Request.setBufferSize"); 352 } 353 catch (Exception e) 354 { 355 assertTrue(e instanceof IllegalStateException); 356 } 357 } 358 testHead()359 public void testHead() throws Exception 360 { 361 Server server = new Server(); 362 try 363 { 364 SocketConnector socketConnector = new SocketConnector(); 365 socketConnector.setPort(0); 366 server.addConnector(socketConnector); 367 server.addHandler(new AbstractHandler() 368 { 369 public void handle(String string, HttpServletRequest request, HttpServletResponse response, int i) throws IOException, ServletException 370 { 371 response.setStatus(200); 372 response.setContentType("text/plain"); 373 PrintWriter w = response.getWriter(); 374 w.flush(); 375 w.println("Geht"); 376 w.flush(); 377 w.println("Doch"); 378 ((Request) request).setHandled(true); 379 } 380 }); 381 server.start(); 382 383 Socket socket = new Socket("localhost",socketConnector.getLocalPort()); 384 socket.getOutputStream().write("HEAD / HTTP/1.1\r\nHost: localhost\r\n\r\n".getBytes()); 385 socket.getOutputStream().write("GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n".getBytes()); 386 socket.getOutputStream().flush(); 387 388 LineNumberReader reader = new LineNumberReader(new InputStreamReader(socket.getInputStream())); 389 String line = reader.readLine(); 390 while (line!=null && line.length()>0) 391 line = reader.readLine(); 392 393 while (line!=null && line.length()==0) 394 line = reader.readLine(); 395 396 assertTrue(line.startsWith("HTTP/1.1 200 OK")); 397 398 } 399 finally 400 { 401 server.stop(); 402 } 403 } 404 newResponse()405 private Response newResponse() 406 { 407 HttpConnection connection=new HttpConnection(connector,connector._endp,connector._server); 408 connection.getGenerator().reset(false); 409 HttpConnection.setCurrentConnection(connection); 410 Response response = connection.getResponse(); 411 connection.getRequest().setRequestURI("/test"); 412 return response; 413 } 414 415 class TestSession extends AbstractSessionManager.Session 416 { TestSession(AbstractSessionManager abstractSessionManager, String id)417 public TestSession(AbstractSessionManager abstractSessionManager, String id) 418 { 419 abstractSessionManager.super(System.currentTimeMillis(), id); 420 } 421 getAttribute(String name)422 public Object getAttribute(String name) 423 { 424 return null; 425 } 426 getAttributeNames()427 public Enumeration getAttributeNames() 428 { 429 430 return null; 431 } 432 getCreationTime()433 public long getCreationTime() 434 { 435 436 return 0; 437 } 438 getId()439 public String getId() 440 { 441 return "12345"; 442 } 443 getLastAccessedTime()444 public long getLastAccessedTime() 445 { 446 return 0; 447 } 448 getMaxInactiveInterval()449 public int getMaxInactiveInterval() 450 { 451 return 0; 452 } 453 getServletContext()454 public ServletContext getServletContext() 455 { 456 return null; 457 } 458 getSessionContext()459 public HttpSessionContext getSessionContext() 460 { 461 return null; 462 } 463 getValue(String name)464 public Object getValue(String name) 465 { 466 return null; 467 } 468 getValueNames()469 public String[] getValueNames() 470 { 471 return null; 472 } 473 invalidate()474 public void invalidate() 475 { 476 } 477 isNew()478 public boolean isNew() 479 { 480 return false; 481 } 482 putValue(String name, Object value)483 public void putValue(String name, Object value) 484 { 485 } 486 removeAttribute(String name)487 public void removeAttribute(String name) 488 { 489 } 490 removeValue(String name)491 public void removeValue(String name) 492 { 493 } 494 setAttribute(String name, Object value)495 public void setAttribute(String name, Object value) 496 { 497 } 498 setMaxInactiveInterval(int interval)499 public void setMaxInactiveInterval(int interval) 500 { 501 } 502 newAttributeMap()503 protected Map newAttributeMap() 504 { 505 // TODO Auto-generated method stub 506 return null; 507 } 508 } 509 } 510