1 20 package org.apache.cactus.sample.servlet.unit; 21 22 import java.io.BufferedReader ; 23 import java.io.ByteArrayInputStream ; 24 import java.io.File ; 25 26 import org.apache.cactus.ServletTestCase; 27 import org.apache.cactus.WebRequest; 28 29 34 public class TestHttpRequest extends ServletTestCase 35 { 36 43 public void beginGetPathTranslated(WebRequest theRequest) 44 { 45 theRequest.setURL("jakarta.apache.org", "/mywebapp", "/myservlet", 46 "/test1/test2", "PARAM1=value1"); 47 } 48 49 57 public void testGetPathTranslated() 58 { 59 String nativePathInfo = File.separator + "test1" + File.separator 60 + "test2"; 61 62 String pathTranslated = request.getPathTranslated(); 63 64 if (request.getRealPath("/") == null) 66 { 67 assertNull("Should have been null", pathTranslated); 68 } 69 else 70 { 71 assertNotNull("Should not be null", pathTranslated); 72 assertTrue("Should end with [" + nativePathInfo + "] but got [" 73 + pathTranslated + "] instead", 74 pathTranslated.endsWith(nativePathInfo)); 75 } 76 } 77 78 80 86 public void beginSendUserData(WebRequest theRequest) 87 { 88 ByteArrayInputStream bais = new ByteArrayInputStream ( 89 "<data>some data to send in the body</data>".getBytes()); 90 91 theRequest.setUserData(bais); 92 theRequest.setContentType("text/xml"); 93 } 94 95 100 public void testSendUserData() throws Exception 101 { 102 String buffer; 103 StringBuffer body = new StringBuffer (); 104 105 BufferedReader reader = request.getReader(); 106 107 while ((buffer = reader.readLine()) != null) 108 { 109 body.append(buffer); 110 } 111 112 assertEquals("<data>some data to send in the body</data>", 113 body.toString()); 114 assertEquals("text/xml", request.getContentType()); 115 } 116 117 119 123 public void testRemoteClientCheck() 124 { 125 request.setRemoteIPAddress("192.168.0.1"); 126 request.setRemoteHostName("atlantis"); 127 request.setRemoteUser("george"); 128 129 assertEquals("192.168.0.1", request.getRemoteAddr()); 130 assertEquals("atlantis", request.getRemoteHost()); 131 assertEquals("george", request.getRemoteUser()); 132 } 133 134 } 135 | Popular Tags |