1 20 package org.apache.cactus.sample.servlet.unit; 21 22 import java.io.BufferedReader ; 23 import java.io.DataInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStreamReader ; 26 import java.io.PrintWriter ; 27 28 import javax.servlet.http.HttpServletResponse ; 29 30 import org.apache.cactus.ServletTestCase; 31 import org.apache.cactus.WebRequest; 32 import org.apache.cactus.WebResponse; 33 34 39 public class TestHttpResponse extends ServletTestCase 40 { 41 46 public void testWriteOutputStream() throws IOException 47 { 48 PrintWriter pw = response.getWriter(); 49 50 pw.println("should not result in an error"); 51 } 52 53 60 public void endWriteOutputStream(WebResponse theResponse) throws IOException 61 { 62 BufferedReader br = 63 new BufferedReader (new InputStreamReader (new DataInputStream ( 64 theResponse.getConnection().getInputStream()))); 65 66 assertEquals("should not result in an error", br.readLine()); 67 } 68 69 71 76 public void testGetResponseAsText() throws IOException 77 { 78 PrintWriter pw = response.getWriter(); 79 80 pw.print("<html><head/><body>A GET request</body></html>"); 85 } 86 87 94 public void endGetResponseAsText(WebResponse theResponse) 95 throws IOException 96 { 97 String expected = "<html><head/><body>A GET request</body></html>"; 98 99 String result = theResponse.getText(); 100 101 assertEquals(expected, result); 102 } 103 104 106 113 public void testGetResponseAsStringArrayMultiLines() throws IOException 114 { 115 PrintWriter pw = response.getWriter(); 116 117 response.setContentType("text/html"); 118 pw.println("<html><head/>"); 119 pw.println("<body>A GET request</body>"); 120 pw.println("</html>"); 121 } 122 123 132 public void endGetResponseAsStringArrayMultiLines(WebResponse theResponse) 133 throws IOException 134 { 135 String [] results1 = theResponse.getTextAsArray(); 136 String [] results2 = theResponse.getTextAsArray(); 137 138 assertTrue("Should have returned 3 lines of text but returned [" 139 + results1.length + "]", results1.length == 3); 140 assertEquals("<html><head/>", results1[0]); 141 assertEquals("<body>A GET request</body>", results1[1]); 142 assertEquals("</html>", results1[2]); 143 144 assertTrue("Should have returned 3 lines of text but returned [" 145 + results2.length + "]", results2.length == 3); 146 assertEquals("<html><head/>", results2[0]); 147 assertEquals("<body>A GET request</body>", results2[1]); 148 assertEquals("</html>", results2[2]); 149 } 150 151 153 162 public void beginGetReader(WebRequest theRequest) 163 { 164 theRequest.addParameter("test", "something", WebRequest.POST_METHOD); 165 } 166 167 175 public void testGetReader() throws Exception 176 { 177 String buffer; 178 StringBuffer body = new StringBuffer (); 179 180 BufferedReader reader = request.getReader(); 181 182 while ((buffer = reader.readLine()) != null) 183 { 184 body.append(buffer); 185 } 186 187 assertEquals("test=something", body.toString()); 188 } 189 190 192 197 public void testSetContentType() throws Exception 198 { 199 response.setContentType("text/xml;charset=ISO-8859-1"); 205 206 PrintWriter pw = response.getWriter(); 210 pw.println("<?xml version=\"1.0\"?>"); 211 pw.println("<test></test>"); 212 } 213 214 219 public void endSetContentType(WebResponse theResponse) 220 { 221 assertEquals("text/xml;charset=ISO-8859-1", 222 theResponse.getConnection().getContentType()); 223 } 224 225 227 233 public void testRedirect() throws IOException 234 { 235 response.sendRedirect("http://jakarta.apache.org"); 236 } 237 238 246 public void endRedirect(WebResponse theResponse) throws IOException 247 { 248 assertEquals(HttpServletResponse.SC_MOVED_TEMPORARILY, 249 theResponse.getStatusCode()); 250 } 251 252 254 261 public void testStatusCode() 262 { 263 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 264 } 265 266 277 public void endStatusCode(WebResponse theResponse) throws IOException 278 { 279 assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 280 theResponse.getStatusCode()); 281 } 282 283 285 288 public void testNoContentResponseCode() 289 { 290 response.setStatus(HttpServletResponse.SC_NO_CONTENT); 291 } 292 293 298 public void endNoContentResponseCode(WebResponse theResponse) 299 { 300 assertEquals(theResponse.getStatusCode(), 204); 301 } 302 } 303 | Popular Tags |