KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > TestWebappParameters


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappParameters.java,v 1.10.2.1 2004/02/22 18:21:16 olegk Exp $
3  * $Revision: 1.10.2.1 $
4  * $Date: 2004/02/22 18:21:16 $
5  * ====================================================================
6  *
7  * Copyright 1999-2004 The Apache Software Foundation
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ====================================================================
21  *
22  * This software consists of voluntary contributions made by many
23  * individuals on behalf of the Apache Software Foundation. For more
24  * information on the Apache Software Foundation, please see
25  * <http://www.apache.org/>.
26  *
27  * [Additional notices, if required by prior licensing conditions]
28  *
29  */

30
31 package org.apache.commons.httpclient;
32
33 import junit.framework.*;
34 import org.apache.commons.httpclient.methods.*;
35
36 /**
37  * This suite of tests depends upon the httpclienttest webapp,
38  * which is available in the httpclient/src/test-webapp
39  * directory in the CVS tree.
40  * <p>
41  * The webapp should be deployed in the context "httpclienttest"
42  * on a servlet engine running on port 8080 on the localhost
43  * (IP 127.0.0.1).
44  * <p>
45  * You can change the assumed port by setting the
46  * "httpclient.test.localPort" property.
47  * You can change the assumed host by setting the
48  * "httpclient.test.localHost" property.
49  * You can change the assumed context by setting the
50  * "httpclient.test.webappContext" property.
51  *
52  * @author Rodney Waldhoff
53  * @version $Id: TestWebappParameters.java,v 1.10.2.1 2004/02/22 18:21:16 olegk Exp $
54  */

55 public class TestWebappParameters extends TestWebappBase {
56
57     public TestWebappParameters(String JavaDoc testName) {
58         super(testName);
59     }
60
61     public static Test suite() {
62         TestSuite suite = new TestSuite(TestWebappParameters.class);
63         return suite;
64     }
65
66     public static void main(String JavaDoc args[]) {
67         String JavaDoc[] testCaseName = { TestWebappParameters.class.getName() };
68         junit.textui.TestRunner.main(testCaseName);
69     }
70
71     // ------------------------------------------------------------------ Tests
72

73     /**
74      * Test that {@link GetMethod#setQueryString(java.lang.String)}
75      * can include a leading question mark.
76      */

77     public void testGetMethodQueryString() throws Exception JavaDoc {
78         HttpClient client = createHttpClient();
79         GetMethod method = new GetMethod("/" + getWebappContext() + "/params");
80         method.setQueryString("?hadQuestionMark=true");
81         
82         try {
83             client.executeMethod(method);
84         } catch (Throwable JavaDoc t) {
85             t.printStackTrace();
86             fail("Unable to execute method : " + t.toString());
87         }
88         assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
89         assertEquals(200,method.getStatusCode());
90         assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"hadQuestionMark=true\"</p>") >= 0);
91     }
92
93     /**
94      * Test that {@link GetMethod#setQueryString(java.lang.String)}
95      * doesn't have to include a leading question mark.
96      */

97     public void testGetMethodQueryString2() throws Exception JavaDoc {
98         HttpClient client = createHttpClient();
99         GetMethod method = new GetMethod("/" + getWebappContext() + "/params");
100         method.setQueryString("hadQuestionMark=false");
101         
102         try {
103             client.executeMethod(method);
104         } catch (Throwable JavaDoc t) {
105             t.printStackTrace();
106             fail("Unable to execute method : " + t.toString());
107         }
108         assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
109         assertEquals(200,method.getStatusCode());
110         assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"hadQuestionMark=false\"</p>") >= 0);
111     }
112
113     /**
114      * Test that {@link GetMethod#addParameter(java.lang.String,java.lang.String)}
115      * values get added to the query string.
116      */

117     public void testGetMethodParameters() throws Exception JavaDoc {
118         HttpClient client = createHttpClient();
119         GetMethod method = new GetMethod("/" + getWebappContext() + "/params");
120         method.setQueryString(new NameValuePair[] { new NameValuePair("param-one","param-value") });
121         
122         try {
123             client.executeMethod(method);
124         } catch (Throwable JavaDoc t) {
125             t.printStackTrace();
126             fail("Unable to execute method : " + t.toString());
127         }
128         assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
129         assertEquals(200,method.getStatusCode());
130         assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"param-one=param-value\"</p>") >= 0);
131     }
132
133     /**
134      * Test that {@link GetMethod#addParameter(java.lang.String,java.lang.String)}
135      * works with multiple parameters.
136      */

137     public void testGetMethodMultiParameters() throws Exception JavaDoc {
138         HttpClient client = createHttpClient();
139         GetMethod method = new GetMethod("/" + getWebappContext() + "/params");
140         method.setQueryString(new NameValuePair[] {
141                                 new NameValuePair("param-one","param-value"),
142                                 new NameValuePair("param-two","param-value2"),
143                                 new NameValuePair("special-chars",":/?~.")
144                               });
145         
146         try {
147             client.executeMethod(method);
148         } catch (Throwable JavaDoc t) {
149             t.printStackTrace();
150             fail("Unable to execute method : " + t.toString());
151         }
152         assertEquals(200,method.getStatusCode());
153         assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
154         assertTrue(method.getResponseBodyAsString().indexOf("name=\"special-chars\";value=\":/?~.\"") >= 0);
155         assertTrue(method.getResponseBodyAsString().indexOf("name=\"param-one\";value=\"param-value\"") >= 0);
156         assertTrue(method.getResponseBodyAsString().indexOf("name=\"param-two\";value=\"param-value2\"") >= 0);
157     }
158
159     /**
160      * Test that {@link GetMethod#addParameter(java.lang.String,java.lang.String)}
161      * works with a parameter name but no value.
162      */

163     public void testGetMethodParameterWithoutValue() throws Exception JavaDoc {
164         HttpClient client = createHttpClient();
165         GetMethod method = new GetMethod("/" + getWebappContext() + "/params");
166         method.setQueryString(new NameValuePair[] { new NameValuePair("param-without-value",null) });
167         
168         try {
169             client.executeMethod(method);
170         } catch (Throwable JavaDoc t) {
171             t.printStackTrace();
172             fail("Unable to execute method : " + t.toString());
173         }
174         assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
175         assertEquals(200,method.getStatusCode());
176         assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"param-without-value=\"</p>") >= 0);
177     }
178
179     /**
180      * Test that {@link GetMethod#addParameter(java.lang.String,java.lang.String)}
181      * works with a parameter name that occurs more than once.
182      */

183     public void testGetMethodParameterAppearsTwice() throws Exception JavaDoc {
184         HttpClient client = createHttpClient();
185         GetMethod method = new GetMethod("/" + getWebappContext() + "/params");
186         method.setQueryString(new NameValuePair[] {
187                                   new NameValuePair("foo","one"),
188                                   new NameValuePair("foo","two")
189                              });
190         
191         try {
192             client.executeMethod(method);
193         } catch (Throwable JavaDoc t) {
194             t.printStackTrace();
195             fail("Unable to execute method : " + t.toString());
196         }
197         assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
198         assertEquals(200,method.getStatusCode());
199         assertTrue(method.getResponseBodyAsString().indexOf("name=\"foo\";value=\"one\"") >= 0);
200         assertTrue(method.getResponseBodyAsString().indexOf("name=\"foo\";value=\"two\"") >= 0);
201     }
202
203     public void testGetMethodOverwriteQueryString() throws Exception JavaDoc {
204         HttpClient client = createHttpClient();
205         GetMethod method = new GetMethod("/" + getWebappContext() + "/params");
206         method.setQueryString("query=string");
207         method.setQueryString(new NameValuePair[] {
208                                   new NameValuePair("param","eter"),
209                                   new NameValuePair("para","meter")
210                              });
211         
212         try {
213             client.executeMethod(method);
214         } catch (Throwable JavaDoc t) {
215             t.printStackTrace();
216             fail("Unable to execute method : " + t.toString());
217         }
218         assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
219         assertEquals(200,method.getStatusCode());
220         assertTrue(method.getResponseBodyAsString().indexOf("name=\"query\";value=\"string\"") == -1);
221         assertTrue(method.getResponseBodyAsString().indexOf("name=\"param\";value=\"eter\"") >= 0);
222         assertTrue(method.getResponseBodyAsString().indexOf("name=\"para\";value=\"meter\"") >= 0);
223     }
224
225     /**
226      * Test that {@link PostMethod#addParameter(java.lang.String,java.lang.String)}
227      * and {@link PostMethod#setQueryString(java.lang.String)} combine
228      * properly.
229      */

230     public void testPostMethodParameterAndQueryString() throws Exception JavaDoc {
231         HttpClient client = createHttpClient();
232         PostMethod method = new PostMethod("/" + getWebappContext() + "/params");
233         method.setQueryString("query=string");
234         method.setRequestBody(new NameValuePair[] {
235            new NameValuePair("param","eter"),
236            new NameValuePair("para","meter") } );
237         
238         try {
239             client.executeMethod(method);
240         } catch (Throwable JavaDoc t) {
241             t.printStackTrace();
242             fail("Unable to execute method : " + t.toString());
243         }
244         assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: POST</title>") >= 0);
245         assertEquals(200,method.getStatusCode());
246         assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"query=string\"</p>") >= 0);
247         assertTrue(method.getResponseBodyAsString(),method.getResponseBodyAsString().indexOf("name=\"param\";value=\"eter\"") >= 0);
248         assertTrue(method.getResponseBodyAsString().indexOf("name=\"para\";value=\"meter\"") >= 0);
249     }
250 }
251
252
Popular Tags