1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsNoHost.java,v 1.17 2003/04/17 11:34:19 olegk Exp $ 3 * $Revision: 1.17 $ 4 * $Date: 2003/04/17 11:34:19 $ 5 * ==================================================================== 6 * 7 * The Apache Software License, Version 1.1 8 * 9 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights 10 * reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in 21 * the documentation and/or other materials provided with the 22 * distribution. 23 * 24 * 3. The end-user documentation included with the redistribution, if 25 * any, must include the following acknowlegement: 26 * "This product includes software developed by the 27 * Apache Software Foundation (http://www.apache.org/)." 28 * Alternately, this acknowlegement may appear in the software itself, 29 * if and wherever such third-party acknowlegements normally appear. 30 * 31 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software 32 * Foundation" must not be used to endorse or promote products derived 33 * from this software without prior written permission. For written 34 * permission, please contact apache@apache.org. 35 * 36 * 5. Products derived from this software may not be called "Apache" 37 * nor may "Apache" appear in their names without prior written 38 * permission of the Apache Group. 39 * 40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 41 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 42 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 43 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 46 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 47 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 49 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 50 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * ==================================================================== 53 * 54 * This software consists of voluntary contributions made by many 55 * individuals on behalf of the Apache Software Foundation. For more 56 * information on the Apache Software Foundation, please see 57 * <http://www.apache.org/>;. 58 * 59 * [Additional notices, if required by prior licensing conditions] 60 * 61 */ 62 63 package org.apache.commons.httpclient; 64 65 import java.io.IOException; 66 import java.io.InputStreamReader; 67 import java.io.Reader; 68 69 import junit.framework.Test; 70 import junit.framework.TestCase; 71 import junit.framework.TestSuite; 72 import org.apache.commons.httpclient.methods.GetMethod; 73 import org.apache.commons.httpclient.methods.PostMethod; 74 import org.apache.commons.httpclient.methods.HeadMethod; 75 76 /*** 77 * @author Rodney Waldhoff 78 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a> 79 * @author Ortwin Gl�ck 80 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> 81 * @version $Revision: 1.17 $ $Date: 2003/04/17 11:34:19 $ 82 */ 83 public class TestMethodsNoHost extends TestCase { 84 85 static final String NAME = "name", VALUE = "value"; 86 static final String NAME0 = "name0", VALUE0 = "value0"; 87 static final String NAME1 = "name1", VALUE1 = "value1"; 88 static final String NAME2 = "name2", VALUE2 = "value2"; 89 90 static final NameValuePair PAIR = new NameValuePair(NAME, VALUE); 91 static final NameValuePair PAIR0 = new NameValuePair(NAME0, VALUE0); 92 static final NameValuePair PAIR1 = new NameValuePair(NAME1, VALUE1); 93 static final NameValuePair PAIR2 = new NameValuePair(NAME2, VALUE2); 94 95 // ------------------------------------------------------------ Constructor 96 97 public TestMethodsNoHost(String testName) { 98 super(testName); 99 } 100 101 // ------------------------------------------------------- TestCase Methods 102 103 public static Test suite() { 104 return new TestSuite(TestMethodsNoHost.class); 105 } 106 107 // ----------------------------------------------------------------- Tests 108 109 public void testPostParametersEncoding() throws IOException { 110 PostMethod post = new PostMethod(); 111 post.setRequestBody(new NameValuePair[] { PAIR }); 112 assertEquals("name=value", post.getRequestBodyAsString()); 113 114 post.setRequestBody(new NameValuePair[]{ PAIR, PAIR1, PAIR2 }); 115 assertEquals("name=value&name1=value1&name2=value2", 116 post.getRequestBodyAsString()); 117 118 post.setRequestBody(new NameValuePair[]{ PAIR, PAIR1, PAIR2, new NameValuePair("hasSpace", "a b c d") }); 119 assertEquals("name=value&name1=value1&name2=value2&hasSpace=a%20b%20c%20d", 120 post.getRequestBodyAsString()); 121 122 } 123 124 public void testPostSetRequestBody() throws Exception { 125 PostMethod post = new PostMethod("/foo"); 126 String body = "this+is+the+body"; 127 post.setRequestBody(body); 128 assertEquals(body, post.getRequestBodyAsString()); 129 } 130 131 132 public void testHttpMethodBasePaths() throws Exception { 133 HttpMethod simple = new SimpleHttpMethod(); 134 String[] paths = { 135 "/some/absolute/path", 136 "../some/relative/path", 137 "/", 138 "/some/path/with?query=string" 139 }; 140 141 for (int i=0; i<paths.length; i++){ 142 simple.setPath(paths[i]); 143 assertEquals(paths[i], simple.getPath()); 144 } 145 } 146 147 public void testHttpMethodBaseDefaultPath() throws Exception { 148 HttpMethod simple = new SimpleHttpMethod(); 149 assertEquals("/", simple.getPath()); 150 151 simple.setPath(""); 152 assertEquals("/", simple.getPath()); 153 154 simple.setPath(null); 155 assertEquals("/", simple.getPath()); 156 } 157 158 public void testHttpMethodBasePathConstructor() throws Exception { 159 HttpMethod simple = new SimpleHttpMethod(); 160 assertEquals("/", simple.getPath()); 161 162 simple = new SimpleHttpMethod(""); 163 assertEquals("/", simple.getPath()); 164 165 simple = new SimpleHttpMethod("/some/path/"); 166 assertEquals("/some/path/", simple.getPath()); 167 } 168 169 /*** Tests response with a Trasfer-Encoding and Content-Length */ 170 public void testHttpMethodBaseTEandCL() throws Exception { 171 SimpleHttpConnection conn = new SimpleHttpConnection(); 172 String headers = "HTTP/1.1 200 OK\r\n" 173 +"Date: Wed, 28 Mar 2001 05:05:04 GMT\r\n" 174 +"Connection: close\r\n" 175 +"Transfer-Encoding: chunked\r\n" 176 +"Content-Length: 1\r\n"; 177 String body = "0a\r\n1234567890\r\n3\r\n123\r\n0\r\n"; 178 conn.addResponse(headers, body); 179 conn.open(); 180 HttpMethodBase method = new GetMethod("/"); 181 method.execute(new HttpState(), conn); 182 String responseBody = method.getResponseBodyAsString(); 183 // verify that the connection was closed. 184 conn.assertNotOpen(); 185 assertEquals("1234567890123", responseBody); 186 } 187 188 public void testConnectionAutoClose() throws Exception { 189 SimpleHttpConnection conn = new SimpleHttpConnection(); 190 String headers = "HTTP/1.1 200 OK\r\n" 191 +"Date: Wed, 28 Mar 2001 05:05:04 GMT\r\n" 192 +"Connection: close\r\n"; 193 StringBuffer buffer = new StringBuffer(8200); 194 for (int i = 0; i < 8200; i++) { 195 buffer.append('A'); 196 } 197 String body = buffer.toString(); 198 199 conn.addResponse(headers, body); 200 conn.open(); 201 HttpMethodBase method = new GetMethod("/"); 202 method.execute(new HttpState(), conn); 203 Reader response = new InputStreamReader(method.getResponseBodyAsStream()); 204 int c; 205 while ((c = response.read()) != -1) { 206 assertEquals((int) 'A', c); 207 } 208 conn.assertNotOpen(); 209 210 // note - this test is here because the HEAD method handler overrides the 211 // standard behavior for reading a response body. 212 HeadMethod headMethod = new HeadMethod("/"); 213 214 conn.addResponse(headers, ""); 215 216 try { 217 headMethod.execute(new HttpState(), conn); 218 conn.assertNotOpen(); 219 220 } catch (Throwable t) { 221 t.printStackTrace(); 222 fail("Unable to execute method : " + t.toString()); 223 } 224 } 225 226 public void testSetGetQueryString1() { 227 HttpMethod method = new GetMethod(); 228 String qs1 = "name1=value1&name2=value2"; 229 method.setQueryString(qs1); 230 assertEquals(qs1, method.getQueryString()); 231 } 232 233 public void testQueryURIEncoding() { 234 HttpMethod method = new GetMethod("http://server/servlet?foo=bar&baz=schmoo"); 235 assertEquals("foo=bar&baz=schmoo", method.getQueryString()); 236 } 237 238 public void testSetGetQueryString2() { 239 HttpMethod method = new GetMethod(); 240 NameValuePair[] q1 = new NameValuePair[] { 241 new NameValuePair("name1", "value1"), 242 new NameValuePair("name2", "value2") 243 }; 244 method.setQueryString(q1); 245 String qs1 = "name1=value1&name2=value2"; 246 assertEquals(qs1, method.getQueryString()); 247 } 248 249 /*** 250 * Make sure that its OK to call releaseConnection if the connection has not been. 251 */ 252 public void testReleaseConnection() { 253 HttpClient client = new HttpClient(); 254 HttpMethod method = new GetMethod("http://bogus.url/path/"); 255 method.releaseConnection(); 256 } 257 258 }

This page was automatically generated by Maven