KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > sample > servlet > unit > TestHttpResponse


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.sample.servlet.unit;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.DataInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29
30 import org.apache.cactus.ServletTestCase;
31 import org.apache.cactus.WebRequest;
32 import org.apache.cactus.WebResponse;
33
34 /**
35  * Tests that writes to the HTTP response.
36  *
37  * @version $Id: TestHttpResponse.java,v 1.5 2004/06/19 15:10:52 vmassol Exp $
38  */

39 public class TestHttpResponse extends ServletTestCase
40 {
41     /**
42      * Verify that it is possible to write to the servlet output stream.
43      *
44      * @exception IOException on test failure
45      */

46     public void testWriteOutputStream() throws IOException JavaDoc
47     {
48         PrintWriter JavaDoc pw = response.getWriter();
49
50         pw.println("should not result in an error");
51     }
52
53     /**
54      * Verify that it is possible to write to the servlet output stream.
55      *
56      * @param theResponse the response from the server side.
57      *
58      * @exception IOException on test failure
59      */

60     public void endWriteOutputStream(WebResponse theResponse) throws IOException JavaDoc
61     {
62         BufferedReader JavaDoc br =
63             new BufferedReader JavaDoc(new InputStreamReader JavaDoc(new DataInputStream JavaDoc(
64             theResponse.getConnection().getInputStream())));
65
66         assertEquals("should not result in an error", br.readLine());
67     }
68
69     //-------------------------------------------------------------------------
70

71     /**
72      * Verify that the <code>WebResponse.getText()</code> method works.
73      *
74      * @exception IOException on test failure
75      */

76     public void testGetResponseAsText() throws IOException JavaDoc
77     {
78         PrintWriter JavaDoc pw = response.getWriter();
79
80         // Note: Ideally we could also test multi line to verify that end
81
// of lines are correctly handled. However, the different containers
82
// handle end of lines differently (some return "\r\n" - Windows
83
// style, others return "\n" - Unix style).
84
pw.print("<html><head/><body>A GET request</body></html>");
85     }
86
87     /**
88      * Verify that the <code>WebResponse.getText()</code> method works.
89      *
90      * @param theResponse the response from the server side.
91      *
92      * @exception IOException on test failure
93      */

94     public void endGetResponseAsText(WebResponse theResponse)
95         throws IOException JavaDoc
96     {
97         String JavaDoc expected = "<html><head/><body>A GET request</body></html>";
98         
99         String JavaDoc result = theResponse.getText();
100
101         assertEquals(expected, result);
102     }
103
104     //-------------------------------------------------------------------------
105

106     /**
107      * Verify that the <code>getTextAsArray()</code> method
108      * works with output text sent on multiple lines. We also verify that
109      * we can call it several times with the same result.
110      *
111      * @exception IOException on test failure
112      */

113     public void testGetResponseAsStringArrayMultiLines() throws IOException JavaDoc
114     {
115         PrintWriter JavaDoc 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     /**
124      * Verify that the <code>getTextAsArray()</code> method
125      * works with output text sent on multiple lines. We also verify that
126      * we can call it several times with the same result.
127      *
128      * @param theResponse the response from the server side.
129      *
130      * @exception IOException on test failure
131      */

132     public void endGetResponseAsStringArrayMultiLines(WebResponse theResponse)
133         throws IOException JavaDoc
134     {
135         String JavaDoc[] results1 = theResponse.getTextAsArray();
136         String JavaDoc[] 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     //-------------------------------------------------------------------------
152

153     /**
154      * Verify that a test case can get the request body by calling
155      * <code>HttpServletRequest.getReader()</code>. In other words, verify that
156      * internal parameters that Cactus passes from its client side to the
157      * server do not affect this ability.
158      *
159      * @param theRequest the request object that serves to initialize the
160      * HTTP connection to the server redirector.
161      */

162     public void beginGetReader(WebRequest theRequest)
163     {
164         theRequest.addParameter("test", "something", WebRequest.POST_METHOD);
165     }
166
167     /**
168      * Verify that a test case can get the request body by calling
169      * <code>HttpServletRequest.getReader()</code>. In other words, verify that
170      * internal parameters that Cactus passes from its client side to the
171      * server do not affect this ability.
172      *
173      * @exception Exception on test failure
174      */

175     public void testGetReader() throws Exception JavaDoc
176     {
177         String JavaDoc buffer;
178         StringBuffer JavaDoc body = new StringBuffer JavaDoc();
179
180         BufferedReader JavaDoc 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     //-------------------------------------------------------------------------
191

192     /**
193      * Verify we can set and retrieve the content type.
194      *
195      * @throws Exception If an unexpected error occurs
196      */

197     public void testSetContentType() throws Exception JavaDoc
198     {
199         // Note: We also specify the charset so that we are sure to known the
200
// full content type string that will be returned on the client side.
201
// Indeed, some containers will specify a charset even if we don't
202
// specify one in the call to setContentType. This is normal and in
203
// accordance with RFC2616, section 3.4.1.
204
response.setContentType("text/xml;charset=ISO-8859-1");
205
206         // Although we don't assert the written content, this is needed to make
207
// the test succeed on some versions of Orion. If the content is left
208
// empty, Orion will somehow reset the content-type to text/html. Sigh.
209
PrintWriter JavaDoc pw = response.getWriter();
210         pw.println("<?xml version=\"1.0\"?>");
211         pw.println("<test></test>");
212     }
213
214     /**
215      * Verify we can set and retrieve the content type.
216      *
217      * @param theResponse the response from the server side.
218      */

219     public void endSetContentType(WebResponse theResponse)
220     {
221         assertEquals("text/xml;charset=ISO-8859-1",
222             theResponse.getConnection().getContentType());
223     }
224     
225     //-------------------------------------------------------------------------
226

227     /**
228      * Verify that we can assert HTTP status code when it is a redirect and
229      * that the client side of Cactus does not follow the redirect.
230      *
231      * @exception IOException on test failure
232      */

233     public void testRedirect() throws IOException JavaDoc
234     {
235         response.sendRedirect("http://jakarta.apache.org");
236     }
237
238     /**
239      * Verify that we can assert HTTP status code when it is a redirect and
240      * that the client side of Cactus does not follow the redirect.
241      *
242      * @param theResponse the response from the server side.
243      *
244      * @exception IOException on test failure
245      */

246     public void endRedirect(WebResponse theResponse) throws IOException JavaDoc
247     {
248         assertEquals(HttpServletResponse.SC_MOVED_TEMPORARILY,
249             theResponse.getStatusCode());
250     }
251
252     //-------------------------------------------------------------------------
253

254     /**
255      * Verify that we can assert HTTP status code when it is an error
256      * status code (> 400).
257      *
258      * Note: HttpURLConnection will return a FileNotFoundException if the
259      * status code is > 400 and the request does not end with a "/" !
260      */

261     public void testStatusCode()
262     {
263         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
264     }
265
266     /**
267      * Verify that we can assert HTTP status code when it is an error
268      * status code (> 400).
269      *
270      * Note: HttpURLConnection will return a FileNotFoundException if the
271      * status code is > 400 and the request does not end with a "/" !
272      *
273      * @param theResponse the response from the server side.
274      *
275      * @exception IOException on test failure
276      */

277     public void endStatusCode(WebResponse theResponse) throws IOException JavaDoc
278     {
279         assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
280             theResponse.getStatusCode());
281     }
282
283     //-------------------------------------------------------------------------
284

285     /**
286      * Verify that we can return a NO_CONTENT response.
287      */

288     public void testNoContentResponseCode()
289     {
290         response.setStatus(HttpServletResponse.SC_NO_CONTENT);
291     }
292
293     /**
294      * Verify that we can return a NO_CONTENT response.
295      *
296      * @param theResponse the response from the server side.
297      */

298     public void endNoContentResponseCode(WebResponse theResponse)
299     {
300         assertEquals(theResponse.getStatusCode(), 204);
301     }
302 }
303
Popular Tags