KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $HeadURL: https://svn.apache.org/repos/asf/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestQueryParameters.java $
3  * $Revision: 480424 $
4  * $Date: 2006-11-29 05:56:49 +0000 (Wed, 29 Nov 2006) $
5  * ====================================================================
6  *
7  * Licensed to the Apache Software Foundation (ASF) under one or more
8  * contributor license agreements. See the NOTICE file distributed with
9  * this work for additional information regarding copyright ownership.
10  * The ASF licenses this file to You under the Apache License, Version 2.0
11  * (the "License"); you may not use this file except in compliance with
12  * the License. You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ====================================================================
22  *
23  * This software consists of voluntary contributions made by many
24  * individuals on behalf of the Apache Software Foundation. For more
25  * information on the Apache Software Foundation, please see
26  * <http://www.apache.org/>.
27  *
28  */

29
30 package org.apache.commons.httpclient;
31
32 import java.io.IOException JavaDoc;
33
34 import junit.framework.*;
35 import org.apache.commons.httpclient.methods.*;
36 import org.apache.commons.httpclient.server.HttpService;
37 import org.apache.commons.httpclient.server.SimpleRequest;
38 import org.apache.commons.httpclient.server.SimpleResponse;
39
40 /**
41  * @author Rodney Waldhoff
42  * @version $Id: TestQueryParameters.java 480424 2006-11-29 05:56:49 +0000 (Wed, 29 Nov 2006) bayard $
43  */

44 public class TestQueryParameters extends HttpClientTestBase {
45
46     public TestQueryParameters(String JavaDoc testName) throws Exception JavaDoc {
47         super(testName);
48     }
49
50     public static Test suite() {
51         TestSuite suite = new TestSuite(TestQueryParameters.class);
52         return suite;
53     }
54
55     public static void main(String JavaDoc args[]) {
56         String JavaDoc[] testCaseName = { TestQueryParameters.class.getName() };
57         junit.textui.TestRunner.main(testCaseName);
58     }
59
60     // ------------------------------------------------------------------ Tests
61

62     class QueryInfoService implements HttpService {
63
64         public QueryInfoService() {
65             super();
66         }
67
68         public boolean process(final SimpleRequest request, final SimpleResponse response)
69             throws IOException JavaDoc
70         {
71             HttpVersion httpversion = request.getRequestLine().getHttpVersion();
72             response.setStatusLine(httpversion, HttpStatus.SC_OK);
73             response.addHeader(new Header("Content-Type", "text/plain"));
74             
75             URI uri = new URI(request.getRequestLine().getUri(), true);
76
77             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
78             buffer.append("QueryString=\"");
79             buffer.append(uri.getQuery());
80             buffer.append("\"\r\n");
81             response.setBodyString(buffer.toString());
82             return true;
83         }
84     }
85     
86     /**
87      * Test that {@link GetMethod#setQueryString(java.lang.String)}
88      * can include a leading question mark.
89      */

90     public void testGetMethodQueryString() throws Exception JavaDoc {
91         this.server.setHttpService(new QueryInfoService());
92         GetMethod method = new GetMethod("/");
93         method.setQueryString("?hadQuestionMark=true");
94         try {
95             this.client.executeMethod(method);
96             assertEquals(200, method.getStatusCode());
97             String JavaDoc response = method.getResponseBodyAsString();
98             assertTrue(response.indexOf("QueryString=\"hadQuestionMark=true\"") >= 0);
99         } finally {
100             method.releaseConnection();
101         }
102     }
103
104     /**
105      * Test that {@link GetMethod#setQueryString(java.lang.String)}
106      * doesn't have to include a leading question mark.
107      */

108     public void testGetMethodQueryString2() throws Exception JavaDoc {
109         this.server.setHttpService(new QueryInfoService());
110         GetMethod method = new GetMethod("/");
111         method.setQueryString("hadQuestionMark=false");
112         try {
113             this.client.executeMethod(method);
114             assertEquals(200, method.getStatusCode());
115             String JavaDoc response = method.getResponseBodyAsString();
116             assertTrue(response.indexOf("QueryString=\"hadQuestionMark=false\"") >= 0);
117         } finally {
118             method.releaseConnection();
119         }
120     }
121
122     /**
123      * Test that {@link GetMethod#addParameter(java.lang.String,java.lang.String)}
124      * values get added to the query string.
125      */

126     public void testGetMethodParameters() throws Exception JavaDoc {
127         this.server.setHttpService(new QueryInfoService());
128         GetMethod method = new GetMethod("/");
129         method.setQueryString(new NameValuePair[] { new NameValuePair("param-one","param-value") });
130         try {
131             this.client.executeMethod(method);
132             assertEquals(200, method.getStatusCode());
133             String JavaDoc response = method.getResponseBodyAsString();
134             assertTrue(response.indexOf("QueryString=\"param-one=param-value\"") >= 0);
135         } finally {
136             method.releaseConnection();
137         }
138     }
139
140     /**
141      * Test that {@link GetMethod#addParameter(java.lang.String,java.lang.String)}
142      * works with multiple parameters.
143      */

144     public void testGetMethodMultiParameters() throws Exception JavaDoc {
145         this.server.setHttpService(new QueryInfoService());
146         GetMethod method = new GetMethod("/");
147         method.setQueryString(new NameValuePair[] {
148                                 new NameValuePair("param-one","param-value"),
149                                 new NameValuePair("param-two","param-value2"),
150                                 new NameValuePair("special-chars",":/?~.")
151                               });
152         try {
153             this.client.executeMethod(method);
154             assertEquals(200, method.getStatusCode());
155             String JavaDoc response = method.getResponseBodyAsString();
156             assertTrue(response.indexOf("QueryString=\"param-one=param-value&param-two=param-value2&special-chars=:/?~.\"") >= 0);
157         } finally {
158             method.releaseConnection();
159         }
160     }
161
162     /**
163      * Test that {@link GetMethod#addParameter(java.lang.String,java.lang.String)}
164      * works with a parameter name but no value.
165      */

166     public void testGetMethodParameterWithoutValue() throws Exception JavaDoc {
167         this.server.setHttpService(new QueryInfoService());
168         GetMethod method = new GetMethod("/");
169         method.setQueryString(new NameValuePair[] { new NameValuePair("param-without-value", null) });
170         try {
171             this.client.executeMethod(method);
172             assertEquals(200, method.getStatusCode());
173             String JavaDoc response = method.getResponseBodyAsString();
174             assertTrue(response.indexOf("QueryString=\"param-without-value=\"") >= 0);
175         } finally {
176             method.releaseConnection();
177         }
178     }
179
180     /**
181      * Test that {@link GetMethod#addParameter(java.lang.String,java.lang.String)}
182      * works with a parameter name that occurs more than once.
183      */

184     public void testGetMethodParameterAppearsTwice() throws Exception JavaDoc {
185         this.server.setHttpService(new QueryInfoService());
186         GetMethod method = new GetMethod("/");
187         method.setQueryString(new NameValuePair[] {
188                                   new NameValuePair("foo","one"),
189                                   new NameValuePair("foo","two")
190                              });
191         try {
192             this.client.executeMethod(method);
193             assertEquals(200, method.getStatusCode());
194             String JavaDoc response = method.getResponseBodyAsString();
195             assertTrue(response.indexOf("QueryString=\"foo=one&foo=two\"") >= 0);
196         } finally {
197             method.releaseConnection();
198         }
199     }
200
201     public void testGetMethodOverwriteQueryString() throws Exception JavaDoc {
202         this.server.setHttpService(new QueryInfoService());
203         GetMethod method = new GetMethod("/");
204         method.setQueryString("query=string");
205         method.setQueryString(new NameValuePair[] {
206                                   new NameValuePair("param","eter"),
207                                   new NameValuePair("para","meter")
208                              });
209         try {
210             this.client.executeMethod(method);
211             assertEquals(200, method.getStatusCode());
212             String JavaDoc response = method.getResponseBodyAsString();
213             assertFalse(response.indexOf("QueryString=\"query=string\"") >= 0);
214             assertTrue(response.indexOf("QueryString=\"param=eter&para=meter\"") >= 0);
215         } finally {
216             method.releaseConnection();
217         }
218     }
219
220     /**
221      * Test that {@link PostMethod#addParameter(java.lang.String,java.lang.String)}
222      * and {@link PostMethod#setQueryString(java.lang.String)} combine
223      * properly.
224      */

225     public void testPostMethodParameterAndQueryString() throws Exception JavaDoc {
226         this.server.setHttpService(new QueryInfoService());
227         PostMethod method = new PostMethod("/");
228         method.setQueryString("query=string");
229         method.setRequestBody(new NameValuePair[] {
230            new NameValuePair("param","eter"),
231            new NameValuePair("para","meter") } );
232         try {
233             this.client.executeMethod(method);
234             assertEquals(200, method.getStatusCode());
235             String JavaDoc response = method.getResponseBodyAsString();
236             assertTrue(response.indexOf("QueryString=\"query=string\"") >= 0);
237             assertFalse(response.indexOf("QueryString=\"param=eter&para=meter\"") >= 0);
238         } finally {
239             method.releaseConnection();
240         }
241     }
242 }
243
244
Popular Tags