001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2004 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;
021:
022: import java.util.Enumeration;
023:
024: import junit.framework.TestCase;
025:
026: import org.apache.cactus.internal.WebRequestImpl;
027: import org.apache.cactus.internal.configuration.DefaultServletConfiguration;
028: import org.apache.cactus.util.ChainedRuntimeException;
029:
030: /**
031: * Unit tests of the <code>WebRequest</code> class.
032: *
033: * @version $Id: TestWebRequest.java 238991 2004-05-22 11:34:50Z vmassol $
034: */
035: public class TestWebRequest extends TestCase {
036: /**
037: * Object to unit test.
038: */
039: private WebRequest request;
040:
041: /**
042: * @see TestCase#setUp()
043: */
044: protected void setUp() {
045: request = new WebRequestImpl(new DefaultServletConfiguration());
046: }
047:
048: /**
049: * Verify that an exception is thrown when an invalid HTTP METHOD is used
050: * when adding an HTTP parameter.
051: */
052: public void testAddParameterInvalidMethod() {
053: try {
054: request.addParameter("param1", "value1", "INVALIDMETHOD");
055: fail("Should have thrown an exception");
056: } catch (ChainedRuntimeException e) {
057: assertEquals(
058: "The method need to be either \"POST\" or \"GET\"",
059: e.getMessage());
060: }
061: }
062:
063: /**
064: * Verify that <code>getParameterGet</code> returns the first parameter
065: * that was added to the request.
066: */
067: public void testGetParametersGetOk() {
068: request.addParameter("param1", "value1", WebRequest.GET_METHOD);
069: request.addParameter("param1", "value2", WebRequest.GET_METHOD);
070:
071: String result = request.getParameterGet("param1");
072:
073: assertEquals("value1", result);
074: }
075:
076: /**
077: * Verify that <code>getParameterGet</code> returns null if no parameter
078: * of a given name was added to the request.
079: */
080: public void testGetParameterGetNull() {
081: request
082: .addParameter("param1", "value1",
083: WebRequest.POST_METHOD);
084:
085: String result = request.getParameterGet("param1");
086:
087: assertNull(result);
088: }
089:
090: /**
091: * Verify that <code>getParameterPost</code> returns the first parameter
092: * that was added to the request.
093: */
094: public void testGetParametersPostOk() {
095: request
096: .addParameter("param1", "value1",
097: WebRequest.POST_METHOD);
098: request
099: .addParameter("param1", "value2",
100: WebRequest.POST_METHOD);
101:
102: String result = request.getParameterPost("param1");
103:
104: assertEquals("value1", result);
105: }
106:
107: /**
108: * Verify that <code>getParameterPost</code> returns null if no parameter
109: * of a given name was added to the request.
110: */
111: public void testGetParameterPostNull() {
112: request.addParameter("param1", "value1", WebRequest.GET_METHOD);
113:
114: String result = request.getParameterPost("param1");
115:
116: assertNull(result);
117: }
118:
119: /**
120: * Verify that <code>getHeader</code> returns the first header
121: * that was added to the request.
122: */
123: public void testGetHeaderOk() {
124: request.addHeader("header1", "value1");
125: request.addHeader("header2", "value2");
126:
127: String result = request.getHeader("header1");
128:
129: assertEquals("value1", result);
130: }
131:
132: /**
133: * Verify that <code>getHeader</code> returns null if no header
134: * of a given name was added to the request.
135: */
136: public void testGetHeaderNull() {
137: String result = request.getHeader("header1");
138:
139: assertNull(result);
140: }
141:
142: /**
143: * Verify that <Code>toString()</code> returns a nice string representation
144: * of the <code>WebRequest</code>.
145: */
146: public void testToString() {
147: request.addHeader("header1", "value1");
148: request.addHeader("header1", "value2");
149: request.addParameter("param1", "value1", WebRequest.GET_METHOD);
150: request
151: .addParameter("param1", "value1",
152: WebRequest.POST_METHOD);
153: request.addCookie("cookie1", "value1");
154: request.setAutomaticSession(false);
155: request.setURL("jakarta.apache.org:80", "/catalog", "/garden",
156: "/implements/", "param1=value1¶m2=¶m3=value3");
157:
158: String result = request.toString();
159:
160: assertEquals(
161: "simulation URL = [protocol = [http], host name = "
162: + "[jakarta.apache.org], port = [80], context path = [/catalog], "
163: + "servlet path = [/garden], path info = [/implements/], query "
164: + "string = [param1=value1¶m2=¶m3=value3]], automatic "
165: + "session = [false], cookies = [[name = [cookie1], value = "
166: + "[value1], domain = [localhost], path = [null], isSecure = "
167: + "[false], comment = [null], expiryDate = [null]]], headers = "
168: + "[[[header1] = [[value1], [value2]]]], GET parameters = "
169: + "[[[param3] = [[value3]]][[param2] = [[]]][[param1] = [[value1], "
170: + "[value1]]]], POST parameters = [[[param1] = [[value1]]]]",
171: result);
172: }
173:
174: /**
175: * Verify that an error in the query string of <code>setURL()</code>
176: * raises an exception.
177: */
178: public void testSetURLBadQueryString() {
179: try {
180: request.setURL("jakarta.apache.org:80", "/catalog",
181: "/garden", "/implements/", "badquerystring");
182: fail("Failed to recognize invalid query string");
183: } catch (RuntimeException e) {
184: assertEquals(
185: "Bad QueryString [badquerystring] NameValue pair: "
186: + "[badquerystring]", e.getMessage());
187: }
188: }
189:
190: /**
191: * Verify that we can retrieve several POST parameters.
192: */
193: public void testGetPostParametersSeveral() {
194: request
195: .addParameter("param1", "value1",
196: WebRequest.POST_METHOD);
197: request
198: .addParameter("param2", "value2",
199: WebRequest.POST_METHOD);
200: request
201: .addParameter("param3", "value3",
202: WebRequest.POST_METHOD);
203:
204: Enumeration keys = request.getParameterNamesPost();
205: while (keys.hasMoreElements()) {
206: String key = (String) keys.nextElement();
207: String[] values = request.getParameterValuesPost(key);
208:
209: assertEquals(1, values.length);
210:
211: if (!values[0].equals("value1")
212: && !values[0].equals("value2")
213: && !values[0].equals("value3")) {
214: fail("Return value was [" + values[0]
215: + "] but should have "
216: + "been one of [value1], [value2] or [value3]");
217: }
218: }
219: }
220: }
|