001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.sample.servlet.unit;
021:
022: import java.io.BufferedReader;
023: import java.io.ByteArrayInputStream;
024: import java.io.ByteArrayOutputStream;
025: import java.io.File;
026: import java.io.InputStream;
027: import java.io.ObjectInputStream;
028: import java.io.ObjectOutputStream;
029:
030: import org.apache.cactus.ServletTestCase;
031: import org.apache.cactus.WebRequest;
032:
033: /**
034: * Tests that exercise the HTTP request.
035: *
036: * @version $Id: TestHttpRequest.java 239170 2005-05-06 12:22:24Z vmassol $
037: */
038: public class TestHttpRequest extends ServletTestCase {
039: /**
040: * Verify that <code>HttpServletRequestWrapper.getPathTranslated()</code>
041: * takes into account the simulated URL (if any).
042: *
043: * @param theRequest the request object that serves to initialize the
044: * HTTP connection to the server redirector.
045: */
046: public void beginGetPathTranslated(WebRequest theRequest) {
047: theRequest.setURL("jakarta.apache.org", "/mywebapp",
048: "/myservlet", "/test1/test2", "PARAM1=value1");
049: }
050:
051: /**
052: * Verify that <code>HttpServletRequestWrapper.getPathTranslated()</code>
053: * takes into account the simulated URL (if any) or null in situations
054: * where the servlet container cannot determine a valid file path for
055: * these methods, such as when the web application is executed from an
056: * archive, on a remote file system not accessible locally, or in a
057: * database (see section SRV.4.5 of the Servlet 2.3 spec).
058: */
059: public void testGetPathTranslated() {
060: String nativePathInfo = File.separator + "test1"
061: + File.separator + "test2";
062:
063: String pathTranslated = request.getPathTranslated();
064:
065: // Should be null if getRealPath("/") is null
066: if (request.getRealPath("/") == null) {
067: assertNull("Should have been null", pathTranslated);
068: } else {
069: assertNotNull("Should not be null", pathTranslated);
070: assertTrue("Should end with [" + nativePathInfo
071: + "] but got [" + pathTranslated + "] instead",
072: pathTranslated.endsWith(nativePathInfo));
073: }
074: }
075:
076: //-------------------------------------------------------------------------
077:
078: /**
079: * Verify that we can send arbitrary data in the request body and read the
080: * data back on the server side using a Reader.
081: *
082: * @param theRequest the request object that serves to initialize the
083: * HTTP connection to the server redirector.
084: */
085: public void beginSendUserDataAndReadWithReader(WebRequest theRequest) {
086: ByteArrayInputStream bais = new ByteArrayInputStream(
087: "<data>some data to send in the body</data>".getBytes());
088:
089: theRequest.setUserData(bais);
090: theRequest.setContentType("text/xml");
091: }
092:
093: /**
094: * Verify that we can send arbitrary data in the request body and read the
095: * data back on the server side using a Reader.
096: *
097: * @exception Exception on test failure
098: */
099: public void testSendUserDataAndReadWithReader() throws Exception {
100: String buffer;
101: StringBuffer body = new StringBuffer();
102:
103: BufferedReader reader = request.getReader();
104:
105: while ((buffer = reader.readLine()) != null) {
106: body.append(buffer);
107: }
108:
109: assertEquals("<data>some data to send in the body</data>", body
110: .toString());
111: assertEquals("text/xml", request.getContentType());
112: }
113:
114: //-------------------------------------------------------------------------
115:
116: /**
117: * Verify that we can send arbitrary data in the request body and read the
118: * data back on the server side using an ObjectInputStream.
119: *
120: * @param theRequest the request object that serves to initialize the
121: * HTTP connection to the server redirector.
122: * @exception Exception on test failure
123: */
124: public void beginSendUserDataAndReadWithObjectInputStream(
125: WebRequest theRequest) throws Exception {
126: ByteArrayOutputStream baos = new ByteArrayOutputStream();
127: ObjectOutputStream oos = new ObjectOutputStream(baos);
128: oos.writeObject("Test with a String object");
129: oos.flush();
130:
131: theRequest.setUserData(new ByteArrayInputStream(baos
132: .toByteArray()));
133: theRequest.setContentType("application/octet-stream");
134: }
135:
136: /**
137: * Verify that we can send arbitrary data in the request body and read the
138: * data back on the server side using an ObjectInputStream.
139: *
140: * @exception Exception on test failure
141: */
142: public void testSendUserDataAndReadWithObjectInputStream()
143: throws Exception {
144: InputStream in = request.getInputStream();
145: ObjectInputStream ois = new ObjectInputStream(in);
146: String data = (String) ois.readObject();
147: assertNotNull(data);
148: assertEquals("Test with a String object", data);
149: }
150:
151: //-------------------------------------------------------------------------
152:
153: /**
154: * Verify that we can simulate the client remote IP address and the client
155: * remote host name.
156: */
157: public void testRemoteClientCheck() {
158: request.setRemoteIPAddress("192.168.0.1");
159: request.setRemoteHostName("atlantis");
160: request.setRemoteUser("george");
161:
162: assertEquals("192.168.0.1", request.getRemoteAddr());
163: assertEquals("atlantis", request.getRemoteHost());
164: assertEquals("george", request.getRemoteUser());
165: }
166:
167: }
|