1 29 30 package org.apache.commons.httpclient; 31 32 import java.io.IOException ; 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 44 public class TestQueryParameters extends HttpClientTestBase { 45 46 public TestQueryParameters(String testName) throws Exception { 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 args[]) { 56 String [] testCaseName = { TestQueryParameters.class.getName() }; 57 junit.textui.TestRunner.main(testCaseName); 58 } 59 60 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 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 buffer = new StringBuffer (); 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 90 public void testGetMethodQueryString() throws Exception { 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 response = method.getResponseBodyAsString(); 98 assertTrue(response.indexOf("QueryString=\"hadQuestionMark=true\"") >= 0); 99 } finally { 100 method.releaseConnection(); 101 } 102 } 103 104 108 public void testGetMethodQueryString2() throws Exception { 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 response = method.getResponseBodyAsString(); 116 assertTrue(response.indexOf("QueryString=\"hadQuestionMark=false\"") >= 0); 117 } finally { 118 method.releaseConnection(); 119 } 120 } 121 122 126 public void testGetMethodParameters() throws Exception { 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 response = method.getResponseBodyAsString(); 134 assertTrue(response.indexOf("QueryString=\"param-one=param-value\"") >= 0); 135 } finally { 136 method.releaseConnection(); 137 } 138 } 139 140 144 public void testGetMethodMultiParameters() throws Exception { 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 response = method.getResponseBodyAsString(); 156 assertTrue(response.indexOf("QueryString=\"param-one=param-value¶m-two=param-value2&special-chars=:/?~.\"") >= 0); 157 } finally { 158 method.releaseConnection(); 159 } 160 } 161 162 166 public void testGetMethodParameterWithoutValue() throws Exception { 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 response = method.getResponseBodyAsString(); 174 assertTrue(response.indexOf("QueryString=\"param-without-value=\"") >= 0); 175 } finally { 176 method.releaseConnection(); 177 } 178 } 179 180 184 public void testGetMethodParameterAppearsTwice() throws Exception { 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 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 { 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 response = method.getResponseBodyAsString(); 213 assertFalse(response.indexOf("QueryString=\"query=string\"") >= 0); 214 assertTrue(response.indexOf("QueryString=\"param=eter¶=meter\"") >= 0); 215 } finally { 216 method.releaseConnection(); 217 } 218 } 219 220 225 public void testPostMethodParameterAndQueryString() throws Exception { 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 response = method.getResponseBodyAsString(); 236 assertTrue(response.indexOf("QueryString=\"query=string\"") >= 0); 237 assertFalse(response.indexOf("QueryString=\"param=eter¶=meter\"") >= 0); 238 } finally { 239 method.releaseConnection(); 240 } 241 } 242 } 243 244 | Popular Tags |