1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package org.apache.commons.httpclient;
32
33 import java.io.ByteArrayOutputStream;
34 import java.io.InputStreamReader;
35 import java.io.Reader;
36
37 import junit.framework.Test;
38 import junit.framework.TestSuite;
39
40 import org.apache.commons.httpclient.methods.GetMethod;
41 import org.apache.commons.httpclient.methods.HeadMethod;
42 import org.apache.commons.httpclient.methods.PostMethod;
43 import org.apache.commons.httpclient.methods.RequestEntity;
44 import org.apache.commons.httpclient.methods.StringRequestEntity;
45
46 /***
47 * @author Rodney Waldhoff
48 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
49 * @author Ortwin Gl?ck
50 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
51 * @version $Revision: 1.23 $ $Date: 2004/05/12 20:43:54 $
52 */
53 public class TestMethodsNoHost extends TestNoHostBase {
54
55 static final String NAME = "name", VALUE = "value";
56 static final String NAME0 = "name0", VALUE0 = "value0";
57 static final String NAME1 = "name1", VALUE1 = "value1";
58 static final String NAME2 = "name2", VALUE2 = "value2";
59
60 static final NameValuePair PAIR = new NameValuePair(NAME, VALUE);
61 static final NameValuePair PAIR0 = new NameValuePair(NAME0, VALUE0);
62 static final NameValuePair PAIR1 = new NameValuePair(NAME1, VALUE1);
63 static final NameValuePair PAIR2 = new NameValuePair(NAME2, VALUE2);
64
65
66
67 public TestMethodsNoHost(String testName) {
68 super(testName);
69 }
70
71
72
73 public static Test suite() {
74 return new TestSuite(TestMethodsNoHost.class);
75 }
76
77
78
79 private String getRequestAsString(RequestEntity entity) throws Exception {
80 ByteArrayOutputStream bos = new ByteArrayOutputStream();
81 entity.writeRequest(bos);
82 return new String(bos.toByteArray(), "UTF-8");
83 }
84
85 public void testPostParametersEncoding() throws Exception {
86 PostMethod post = new PostMethod();
87 post.setRequestBody(new NameValuePair[] { PAIR });
88 assertEquals("name=value", getRequestAsString(post.getRequestEntity()));
89
90 post.setRequestBody(new NameValuePair[]{ PAIR, PAIR1, PAIR2 });
91 assertEquals("name=value&name1=value1&name2=value2",
92 getRequestAsString(post.getRequestEntity()));
93
94 post.setRequestBody(new NameValuePair[]{ PAIR, PAIR1, PAIR2, new NameValuePair("hasSpace", "a b c d") });
95 assertEquals("name=value&name1=value1&name2=value2&hasSpace=a+b+c+d",
96 getRequestAsString(post.getRequestEntity()));
97
98 }
99
100 public void testPostSetRequestBody() throws Exception {
101 PostMethod post = new PostMethod("/foo");
102 String body = "this+is+the+body";
103 post.setRequestEntity(new StringRequestEntity(body));
104 assertEquals(body, getRequestAsString(post.getRequestEntity()));
105 }
106
107
108 public void testHttpMethodBasePaths() throws Exception {
109 HttpMethod simple = new SimpleHttpMethod();
110 String[] paths = {
111 "/some/absolute/path",
112 "../some/relative/path",
113 "/",
114 "/some/path/with?query=string"
115 };
116
117 for (int i=0; i<paths.length; i++){
118 simple.setPath(paths[i]);
119 assertEquals(paths[i], simple.getPath());
120 }
121 }
122
123 public void testHttpMethodBaseDefaultPath() throws Exception {
124 HttpMethod simple = new SimpleHttpMethod();
125 assertEquals("/", simple.getPath());
126
127 simple.setPath("");
128 assertEquals("/", simple.getPath());
129
130 simple.setPath(null);
131 assertEquals("/", simple.getPath());
132 }
133
134 public void testHttpMethodBasePathConstructor() throws Exception {
135 HttpMethod simple = new SimpleHttpMethod();
136 assertEquals("/", simple.getPath());
137
138 simple = new SimpleHttpMethod("");
139 assertEquals("/", simple.getPath());
140
141 simple = new SimpleHttpMethod("/some/path/");
142 assertEquals("/some/path/", simple.getPath());
143 }
144
145 /*** Tests response with a Trasfer-Encoding and Content-Length */
146 public void testHttpMethodBaseTEandCL() throws Exception {
147 SimpleHttpConnection conn = new SimpleHttpConnection();
148 String headers = "HTTP/1.1 200 OK\r\n"
149 +"Date: Wed, 28 Mar 2001 05:05:04 GMT\r\n"
150 +"Connection: close\r\n"
151 +"Transfer-Encoding: chunked\r\n"
152 +"Content-Length: 1\r\n";
153 String body = "0a\r\n1234567890\r\n3\r\n123\r\n0\r\n";
154 conn.addResponse(headers, body);
155 conn.open();
156 HttpMethodBase method = new GetMethod("/");
157 method.execute(new HttpState(), conn);
158 String responseBody = method.getResponseBodyAsString();
159
160 conn.assertNotOpen();
161 assertEquals("1234567890123", responseBody);
162 }
163
164 public void testConnectionAutoClose() throws Exception {
165 String headers = "HTTP/1.1 200 OK\r\n"
166 +"Date: Wed, 28 Mar 2001 05:05:04 GMT\r\n"
167 +"Connection: close\r\n";
168 StringBuffer buffer = new StringBuffer(8200);
169 for (int i = 0; i < 8200; i++) {
170 buffer.append('A');
171 }
172 String body = buffer.toString();
173
174 conn.addResponse(headers, body);
175 conn.open();
176 HttpMethodBase method = new GetMethod("/");
177 client.executeMethod(method);
178 Reader response = new InputStreamReader(method.getResponseBodyAsStream());
179 int c;
180 while ((c = response.read()) != -1) {
181 assertEquals((int) 'A', c);
182 }
183 conn.assertNotOpen();
184
185
186
187 HeadMethod headMethod = new HeadMethod("/");
188
189 conn.addResponse(headers, "");
190
191 try {
192 client.executeMethod(headMethod);
193 conn.assertNotOpen();
194
195 } catch (Throwable t) {
196 t.printStackTrace();
197 fail("Unable to execute method : " + t.toString());
198 }
199 }
200
201 public void testSetGetQueryString1() {
202 HttpMethod method = new GetMethod();
203 String qs1 = "name1=value1&name2=value2";
204 method.setQueryString(qs1);
205 assertEquals(qs1, method.getQueryString());
206 }
207
208 public void testQueryURIEncoding() {
209 HttpMethod method = new GetMethod("http://server/servlet?foo=bar&baz=schmoo");
210 assertEquals("foo=bar&baz=schmoo", method.getQueryString());
211 }
212
213 public void testSetGetQueryString2() {
214 HttpMethod method = new GetMethod();
215 NameValuePair[] q1 = new NameValuePair[] {
216 new NameValuePair("name1", "value1"),
217 new NameValuePair("name2", "value2")
218 };
219 method.setQueryString(q1);
220 String qs1 = "name1=value1&name2=value2";
221 assertEquals(qs1, method.getQueryString());
222 }
223
224 /***
225 * Make sure that its OK to call releaseConnection if the connection has not been.
226 */
227 public void testReleaseConnection() {
228 HttpMethod method = new GetMethod("http://bogus.url/path/");
229 method.releaseConnection();
230 }
231
232
233 /***
234 * Tests empty body response
235 */
236
237 public void testEmptyBodyAsString() throws Exception {
238 SimpleHttpConnection conn = new SimpleHttpConnection();
239 String headers = "HTTP/1.1 200 OK\r\n"
240 +"Connection: close\r\n"
241 +"Transfer-Encoding: chunked\r\n"
242 +"Content-Length: 0\r\n";
243 String body = "";
244 conn.addResponse(headers, body);
245 conn.open();
246 HttpMethodBase method = new GetMethod("/");
247 method.execute(new HttpState(), conn);
248
249 String response = method.getResponseBodyAsString();
250 assertNull(response);
251 }
252
253
254 public void testEmptyBodyAsByteArray() throws Exception {
255 SimpleHttpConnection conn = new SimpleHttpConnection();
256 String headers = "HTTP/1.1 200 OK\r\n"
257 +"Connection: close\r\n"
258 +"Transfer-Encoding: chunked\r\n"
259 +"Content-Length: 0\r\n";
260 String body = "";
261 conn.addResponse(headers, body);
262 conn.open();
263 HttpMethodBase method = new GetMethod("/");
264 method.execute(new HttpState(), conn);
265
266 byte[] response = method.getResponseBody();
267 assertNull(response);
268 }
269
270
271 }