KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByteArrayInputStream JavaDoc;
24 import java.io.File JavaDoc;
25
26 import org.apache.cactus.ServletTestCase;
27 import org.apache.cactus.WebRequest;
28
29 /**
30  * Tests that exercise the HTTP request.
31  *
32  * @version $Id: TestHttpRequest.java,v 1.3 2004/02/29 16:36:44 vmassol Exp $
33  */

34 public class TestHttpRequest extends ServletTestCase
35 {
36     /**
37      * Verify that <code>HttpServletRequestWrapper.getPathTranslated()</code>
38      * takes into account the simulated URL (if any).
39      *
40      * @param theRequest the request object that serves to initialize the
41      * HTTP connection to the server redirector.
42      */

43     public void beginGetPathTranslated(WebRequest theRequest)
44     {
45         theRequest.setURL("jakarta.apache.org", "/mywebapp", "/myservlet",
46             "/test1/test2", "PARAM1=value1");
47     }
48
49     /**
50      * Verify that <code>HttpServletRequestWrapper.getPathTranslated()</code>
51      * takes into account the simulated URL (if any) or null in situations
52      * where the servlet container cannot determine a valid file path for
53      * these methods, such as when the web application is executed from an
54      * archive, on a remote file system not accessible locally, or in a
55      * database (see section SRV.4.5 of the Servlet 2.3 spec).
56      */

57     public void testGetPathTranslated()
58     {
59         String JavaDoc nativePathInfo = File.separator + "test1" + File.separator
60             + "test2";
61
62         String JavaDoc pathTranslated = request.getPathTranslated();
63
64         // Should be null if getRealPath("/") is null
65
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     //-------------------------------------------------------------------------
79

80     /**
81      * Verify that we can send arbitrary data in the request body.
82      *
83      * @param theRequest the request object that serves to initialize the
84      * HTTP connection to the server redirector.
85      */

86     public void beginSendUserData(WebRequest theRequest)
87     {
88         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(
89             "<data>some data to send in the body</data>".getBytes());
90
91         theRequest.setUserData(bais);
92         theRequest.setContentType("text/xml");
93     }
94
95     /**
96      * Verify that we can send arbitrary data in the request body.
97      *
98      * @exception Exception on test failure
99      */

100     public void testSendUserData() throws Exception JavaDoc
101     {
102         String JavaDoc buffer;
103         StringBuffer JavaDoc body = new StringBuffer JavaDoc();
104
105         BufferedReader JavaDoc 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     //-------------------------------------------------------------------------
118

119     /**
120      * Verify that we can simulate the client remote IP address and the client
121      * remote host name.
122      */

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