1 30 31 package org.apache.commons.httpclient; 32 33 import java.io.IOException ; 34 import java.io.InputStreamReader ; 35 import java.io.Reader ; 36 37 import junit.framework.Test; 38 import junit.framework.TestCase; 39 import junit.framework.TestSuite; 40 import org.apache.commons.httpclient.methods.GetMethod; 41 import org.apache.commons.httpclient.methods.PostMethod; 42 import org.apache.commons.httpclient.methods.HeadMethod; 43 44 51 public class TestMethodsNoHost extends TestCase { 52 53 static final String NAME = "name", VALUE = "value"; 54 static final String NAME0 = "name0", VALUE0 = "value0"; 55 static final String NAME1 = "name1", VALUE1 = "value1"; 56 static final String NAME2 = "name2", VALUE2 = "value2"; 57 58 static final NameValuePair PAIR = new NameValuePair(NAME, VALUE); 59 static final NameValuePair PAIR0 = new NameValuePair(NAME0, VALUE0); 60 static final NameValuePair PAIR1 = new NameValuePair(NAME1, VALUE1); 61 static final NameValuePair PAIR2 = new NameValuePair(NAME2, VALUE2); 62 63 65 public TestMethodsNoHost(String testName) { 66 super(testName); 67 } 68 69 71 public static Test suite() { 72 return new TestSuite(TestMethodsNoHost.class); 73 } 74 75 77 public void testPostParametersEncoding() throws IOException { 78 PostMethod post = new PostMethod(); 79 post.setRequestBody(new NameValuePair[] { PAIR }); 80 assertEquals("name=value", post.getRequestBodyAsString()); 81 82 post.setRequestBody(new NameValuePair[]{ PAIR, PAIR1, PAIR2 }); 83 assertEquals("name=value&name1=value1&name2=value2", 84 post.getRequestBodyAsString()); 85 86 post.setRequestBody(new NameValuePair[]{ PAIR, PAIR1, PAIR2, new NameValuePair("hasSpace", "a b c d") }); 87 assertEquals("name=value&name1=value1&name2=value2&hasSpace=a+b+c+d", 88 post.getRequestBodyAsString()); 89 90 } 91 92 public void testPostSetRequestBody() throws Exception { 93 PostMethod post = new PostMethod("/foo"); 94 String body = "this+is+the+body"; 95 post.setRequestBody(body); 96 assertEquals(body, post.getRequestBodyAsString()); 97 } 98 99 100 public void testHttpMethodBasePaths() throws Exception { 101 HttpMethod simple = new SimpleHttpMethod(); 102 String [] paths = { 103 "/some/absolute/path", 104 "../some/relative/path", 105 "/", 106 "/some/path/with?query=string" 107 }; 108 109 for (int i=0; i<paths.length; i++){ 110 simple.setPath(paths[i]); 111 assertEquals(paths[i], simple.getPath()); 112 } 113 } 114 115 public void testHttpMethodBaseDefaultPath() throws Exception { 116 HttpMethod simple = new SimpleHttpMethod(); 117 assertEquals("/", simple.getPath()); 118 119 simple.setPath(""); 120 assertEquals("/", simple.getPath()); 121 122 simple.setPath(null); 123 assertEquals("/", simple.getPath()); 124 } 125 126 public void testHttpMethodBasePathConstructor() throws Exception { 127 HttpMethod simple = new SimpleHttpMethod(); 128 assertEquals("/", simple.getPath()); 129 130 simple = new SimpleHttpMethod(""); 131 assertEquals("/", simple.getPath()); 132 133 simple = new SimpleHttpMethod("/some/path/"); 134 assertEquals("/some/path/", simple.getPath()); 135 } 136 137 138 public void testHttpMethodBaseTEandCL() throws Exception { 139 SimpleHttpConnection conn = new SimpleHttpConnection(); 140 String headers = "HTTP/1.1 200 OK\r\n" 141 +"Date: Wed, 28 Mar 2001 05:05:04 GMT\r\n" 142 +"Connection: close\r\n" 143 +"Transfer-Encoding: chunked\r\n" 144 +"Content-Length: 1\r\n"; 145 String body = "0a\r\n1234567890\r\n3\r\n123\r\n0\r\n"; 146 conn.addResponse(headers, body); 147 conn.open(); 148 HttpMethodBase method = new GetMethod("/"); 149 method.execute(new HttpState(), conn); 150 String responseBody = method.getResponseBodyAsString(); 151 conn.assertNotOpen(); 153 assertEquals("1234567890123", responseBody); 154 } 155 156 public void testConnectionAutoClose() throws Exception { 157 SimpleHttpConnection conn = new SimpleHttpConnection(); 158 String headers = "HTTP/1.1 200 OK\r\n" 159 +"Date: Wed, 28 Mar 2001 05:05:04 GMT\r\n" 160 +"Connection: close\r\n"; 161 StringBuffer buffer = new StringBuffer (8200); 162 for (int i = 0; i < 8200; i++) { 163 buffer.append('A'); 164 } 165 String body = buffer.toString(); 166 167 conn.addResponse(headers, body); 168 conn.open(); 169 HttpMethodBase method = new GetMethod("/"); 170 method.execute(new HttpState(), conn); 171 Reader response = new InputStreamReader (method.getResponseBodyAsStream()); 172 int c; 173 while ((c = response.read()) != -1) { 174 assertEquals((int) 'A', c); 175 } 176 conn.assertNotOpen(); 177 178 HeadMethod headMethod = new HeadMethod("/"); 181 182 conn.addResponse(headers, ""); 183 184 try { 185 headMethod.execute(new HttpState(), conn); 186 conn.assertNotOpen(); 187 188 } catch (Throwable t) { 189 t.printStackTrace(); 190 fail("Unable to execute method : " + t.toString()); 191 } 192 } 193 194 public void testSetGetQueryString1() { 195 HttpMethod method = new GetMethod(); 196 String qs1 = "name1=value1&name2=value2"; 197 method.setQueryString(qs1); 198 assertEquals(qs1, method.getQueryString()); 199 } 200 201 public void testQueryURIEncoding() { 202 HttpMethod method = new GetMethod("http://server/servlet?foo=bar&baz=schmoo"); 203 assertEquals("foo=bar&baz=schmoo", method.getQueryString()); 204 } 205 206 public void testSetGetQueryString2() { 207 HttpMethod method = new GetMethod(); 208 NameValuePair[] q1 = new NameValuePair[] { 209 new NameValuePair("name1", "value1"), 210 new NameValuePair("name2", "value2") 211 }; 212 method.setQueryString(q1); 213 String qs1 = "name1=value1&name2=value2"; 214 assertEquals(qs1, method.getQueryString()); 215 } 216 217 220 public void testReleaseConnection() { 221 HttpClient client = new HttpClient(); 222 HttpMethod method = new GetMethod("http://bogus.url/path/"); 223 method.releaseConnection(); 224 } 225 226 227 230 231 public void testEmptyBodyAsString() throws Exception { 232 SimpleHttpConnection conn = new SimpleHttpConnection(); 233 String headers = "HTTP/1.1 200 OK\r\n" 234 +"Connection: close\r\n" 235 +"Transfer-Encoding: chunked\r\n" 236 +"Content-Length: 0\r\n"; 237 String body = ""; 238 conn.addResponse(headers, body); 239 conn.open(); 240 HttpMethodBase method = new GetMethod("/"); 241 method.execute(new HttpState(), conn); 242 243 String response = method.getResponseBodyAsString(); 244 assertNull(response); 245 } 246 247 248 public void testEmptyBodyAsByteArray() throws Exception { 249 SimpleHttpConnection conn = new SimpleHttpConnection(); 250 String headers = "HTTP/1.1 200 OK\r\n" 251 +"Connection: close\r\n" 252 +"Transfer-Encoding: chunked\r\n" 253 +"Content-Length: 0\r\n"; 254 String body = ""; 255 conn.addResponse(headers, body); 256 conn.open(); 257 HttpMethodBase method = new GetMethod("/"); 258 method.execute(new HttpState(), conn); 259 260 byte[] response = method.getResponseBody(); 261 assertNull(response); 262 } 263 264 265 } 266 | Popular Tags |