001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/oac.hc3x/tags/HTTPCLIENT_3_1/src/test/org/apache/commons/httpclient/TestQueryParameters.java $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: */
029:
030: package org.apache.commons.httpclient;
031:
032: import java.io.IOException;
033:
034: import junit.framework.*;
035: import org.apache.commons.httpclient.methods.*;
036: import org.apache.commons.httpclient.server.HttpService;
037: import org.apache.commons.httpclient.server.SimpleRequest;
038: import org.apache.commons.httpclient.server.SimpleResponse;
039:
040: /**
041: * @author Rodney Waldhoff
042: * @version $Id: TestQueryParameters.java 480424 2006-11-29 05:56:49Z bayard $
043: */
044: public class TestQueryParameters extends HttpClientTestBase {
045:
046: public TestQueryParameters(String testName) throws Exception {
047: super (testName);
048: }
049:
050: public static Test suite() {
051: TestSuite suite = new TestSuite(TestQueryParameters.class);
052: return suite;
053: }
054:
055: public static void main(String args[]) {
056: String[] testCaseName = { TestQueryParameters.class.getName() };
057: junit.textui.TestRunner.main(testCaseName);
058: }
059:
060: // ------------------------------------------------------------------ Tests
061:
062: class QueryInfoService implements HttpService {
063:
064: public QueryInfoService() {
065: super ();
066: }
067:
068: public boolean process(final SimpleRequest request,
069: final SimpleResponse response) throws IOException {
070: HttpVersion httpversion = request.getRequestLine()
071: .getHttpVersion();
072: response.setStatusLine(httpversion, HttpStatus.SC_OK);
073: response
074: .addHeader(new Header("Content-Type", "text/plain"));
075:
076: URI uri = new URI(request.getRequestLine().getUri(), true);
077:
078: StringBuffer buffer = new StringBuffer();
079: buffer.append("QueryString=\"");
080: buffer.append(uri.getQuery());
081: buffer.append("\"\r\n");
082: response.setBodyString(buffer.toString());
083: return true;
084: }
085: }
086:
087: /**
088: * Test that {@link GetMethod#setQueryString(java.lang.String)}
089: * can include a leading question mark.
090: */
091: public void testGetMethodQueryString() throws Exception {
092: this .server.setHttpService(new QueryInfoService());
093: GetMethod method = new GetMethod("/");
094: method.setQueryString("?hadQuestionMark=true");
095: try {
096: this .client.executeMethod(method);
097: assertEquals(200, method.getStatusCode());
098: String response = method.getResponseBodyAsString();
099: assertTrue(response
100: .indexOf("QueryString=\"hadQuestionMark=true\"") >= 0);
101: } finally {
102: method.releaseConnection();
103: }
104: }
105:
106: /**
107: * Test that {@link GetMethod#setQueryString(java.lang.String)}
108: * doesn't have to include a leading question mark.
109: */
110: public void testGetMethodQueryString2() throws Exception {
111: this .server.setHttpService(new QueryInfoService());
112: GetMethod method = new GetMethod("/");
113: method.setQueryString("hadQuestionMark=false");
114: try {
115: this .client.executeMethod(method);
116: assertEquals(200, method.getStatusCode());
117: String response = method.getResponseBodyAsString();
118: assertTrue(response
119: .indexOf("QueryString=\"hadQuestionMark=false\"") >= 0);
120: } finally {
121: method.releaseConnection();
122: }
123: }
124:
125: /**
126: * Test that {@link GetMethod#addParameter(java.lang.String,java.lang.String)}
127: * values get added to the query string.
128: */
129: public void testGetMethodParameters() throws Exception {
130: this .server.setHttpService(new QueryInfoService());
131: GetMethod method = new GetMethod("/");
132: method.setQueryString(new NameValuePair[] { new NameValuePair(
133: "param-one", "param-value") });
134: try {
135: this .client.executeMethod(method);
136: assertEquals(200, method.getStatusCode());
137: String response = method.getResponseBodyAsString();
138: assertTrue(response
139: .indexOf("QueryString=\"param-one=param-value\"") >= 0);
140: } finally {
141: method.releaseConnection();
142: }
143: }
144:
145: /**
146: * Test that {@link GetMethod#addParameter(java.lang.String,java.lang.String)}
147: * works with multiple parameters.
148: */
149: public void testGetMethodMultiParameters() throws Exception {
150: this .server.setHttpService(new QueryInfoService());
151: GetMethod method = new GetMethod("/");
152: method.setQueryString(new NameValuePair[] {
153: new NameValuePair("param-one", "param-value"),
154: new NameValuePair("param-two", "param-value2"),
155: new NameValuePair("special-chars", ":/?~.") });
156: try {
157: this .client.executeMethod(method);
158: assertEquals(200, method.getStatusCode());
159: String response = method.getResponseBodyAsString();
160: assertTrue(response
161: .indexOf("QueryString=\"param-one=param-value¶m-two=param-value2&special-chars=:/?~.\"") >= 0);
162: } finally {
163: method.releaseConnection();
164: }
165: }
166:
167: /**
168: * Test that {@link GetMethod#addParameter(java.lang.String,java.lang.String)}
169: * works with a parameter name but no value.
170: */
171: public void testGetMethodParameterWithoutValue() throws Exception {
172: this .server.setHttpService(new QueryInfoService());
173: GetMethod method = new GetMethod("/");
174: method.setQueryString(new NameValuePair[] { new NameValuePair(
175: "param-without-value", null) });
176: try {
177: this .client.executeMethod(method);
178: assertEquals(200, method.getStatusCode());
179: String response = method.getResponseBodyAsString();
180: assertTrue(response
181: .indexOf("QueryString=\"param-without-value=\"") >= 0);
182: } finally {
183: method.releaseConnection();
184: }
185: }
186:
187: /**
188: * Test that {@link GetMethod#addParameter(java.lang.String,java.lang.String)}
189: * works with a parameter name that occurs more than once.
190: */
191: public void testGetMethodParameterAppearsTwice() throws Exception {
192: this .server.setHttpService(new QueryInfoService());
193: GetMethod method = new GetMethod("/");
194: method.setQueryString(new NameValuePair[] {
195: new NameValuePair("foo", "one"),
196: new NameValuePair("foo", "two") });
197: try {
198: this .client.executeMethod(method);
199: assertEquals(200, method.getStatusCode());
200: String response = method.getResponseBodyAsString();
201: assertTrue(response
202: .indexOf("QueryString=\"foo=one&foo=two\"") >= 0);
203: } finally {
204: method.releaseConnection();
205: }
206: }
207:
208: public void testGetMethodOverwriteQueryString() throws Exception {
209: this .server.setHttpService(new QueryInfoService());
210: GetMethod method = new GetMethod("/");
211: method.setQueryString("query=string");
212: method.setQueryString(new NameValuePair[] {
213: new NameValuePair("param", "eter"),
214: new NameValuePair("para", "meter") });
215: try {
216: this .client.executeMethod(method);
217: assertEquals(200, method.getStatusCode());
218: String response = method.getResponseBodyAsString();
219: assertFalse(response
220: .indexOf("QueryString=\"query=string\"") >= 0);
221: assertTrue(response
222: .indexOf("QueryString=\"param=eter¶=meter\"") >= 0);
223: } finally {
224: method.releaseConnection();
225: }
226: }
227:
228: /**
229: * Test that {@link PostMethod#addParameter(java.lang.String,java.lang.String)}
230: * and {@link PostMethod#setQueryString(java.lang.String)} combine
231: * properly.
232: */
233: public void testPostMethodParameterAndQueryString()
234: throws Exception {
235: this .server.setHttpService(new QueryInfoService());
236: PostMethod method = new PostMethod("/");
237: method.setQueryString("query=string");
238: method.setRequestBody(new NameValuePair[] {
239: new NameValuePair("param", "eter"),
240: new NameValuePair("para", "meter") });
241: try {
242: this .client.executeMethod(method);
243: assertEquals(200, method.getStatusCode());
244: String response = method.getResponseBodyAsString();
245: assertTrue(response.indexOf("QueryString=\"query=string\"") >= 0);
246: assertFalse(response
247: .indexOf("QueryString=\"param=eter¶=meter\"") >= 0);
248: } finally {
249: method.releaseConnection();
250: }
251: }
252: }
|