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 package org.apache.commons.httpclient;
29
30 import java.io.IOException;
31 import java.io.InputStreamReader;
32 import java.io.Reader;
33
34 import org.apache.commons.httpclient.methods.GetMethod;
35 import org.apache.commons.httpclient.server.HttpService;
36 import org.apache.commons.httpclient.server.SimpleRequest;
37 import org.apache.commons.httpclient.server.SimpleResponse;
38
39 import junit.framework.Test;
40 import junit.framework.TestSuite;
41
42 /***
43 * Tests basic method functionality.
44 *
45 * @author Remy Maucherat
46 * @author Rodney Waldhoff
47 * @author Oleg Kalnichevski
48 *
49 * @version $Id: TestHttpMethodFundamentals.java 169849 2005-05-12 17:05:07Z olegk $
50 */
51 public class TestHttpMethodFundamentals extends HttpClientTestBase {
52
53 public TestHttpMethodFundamentals(final String testName) throws IOException {
54 super(testName);
55 }
56
57 public static Test suite() {
58 TestSuite suite = new TestSuite(TestHttpMethodFundamentals.class);
59 ProxyTestDecorator.addTests(suite);
60 return suite;
61 }
62
63 public static void main(String args[]) {
64 String[] testCaseName = { TestHttpMethodFundamentals.class.getName() };
65 junit.textui.TestRunner.main(testCaseName);
66 }
67
68 class ManyAService implements HttpService {
69
70 public ManyAService() {
71 super();
72 }
73
74 public boolean process(final SimpleRequest request, final SimpleResponse response)
75 throws IOException
76 {
77 HttpVersion httpversion = request.getRequestLine().getHttpVersion();
78 response.setStatusLine(httpversion, HttpStatus.SC_OK);
79 response.addHeader(new Header("Content-Type", "text/plain"));
80 response.addHeader(new Header("Connection", "close"));
81 StringBuffer buffer = new StringBuffer(1024);
82 for (int i = 0; i < 1024; i++) {
83 buffer.append('A');
84 }
85 response.setBodyString(buffer.toString());
86 return true;
87 }
88 }
89
90 class SimpleChunkedService implements HttpService {
91
92 public SimpleChunkedService() {
93 super();
94 }
95
96 public boolean process(final SimpleRequest request, final SimpleResponse response)
97 throws IOException
98 {
99 HttpVersion httpversion = request.getRequestLine().getHttpVersion();
100 response.setStatusLine(httpversion, HttpStatus.SC_OK);
101 response.addHeader(new Header("Content-Type", "text/plain"));
102 response.addHeader(new Header("Content-Length", "garbage"));
103 response.addHeader(new Header("Transfer-Encoding", "chunked"));
104 response.addHeader(new Header("Connection", "close"));
105 response.setBodyString("1234567890123");
106 return true;
107 }
108 }
109
110 class EmptyResponseService implements HttpService {
111
112 public EmptyResponseService() {
113 super();
114 }
115
116 public boolean process(final SimpleRequest request, final SimpleResponse response)
117 throws IOException
118 {
119 HttpVersion httpversion = request.getRequestLine().getHttpVersion();
120 response.setStatusLine(httpversion, HttpStatus.SC_OK);
121 response.addHeader(new Header("Content-Type", "text/plain"));
122 response.addHeader(new Header("Transfer-Encoding", "chunked"));
123 response.addHeader(new Header("Connection", "close"));
124 return true;
125 }
126 }
127
128 public void testHttpMethodBasePaths() throws Exception {
129 HttpMethod simple = new FakeHttpMethod();
130 String[] paths = {
131 "/some/absolute/path",
132 "../some/relative/path",
133 "/",
134 "/some/path/with?query=string"
135 };
136
137 for (int i=0; i<paths.length; i++){
138 simple.setPath(paths[i]);
139 assertEquals(paths[i], simple.getPath());
140 }
141 }
142
143 public void testHttpMethodBaseDefaultPath() throws Exception {
144 HttpMethod simple = new FakeHttpMethod();
145 assertEquals("/", simple.getPath());
146
147 simple.setPath("");
148 assertEquals("/", simple.getPath());
149
150 simple.setPath(null);
151 assertEquals("/", simple.getPath());
152 }
153
154 public void testHttpMethodBasePathConstructor() throws Exception {
155 HttpMethod simple = new FakeHttpMethod();
156 assertEquals("/", simple.getPath());
157
158 simple = new FakeHttpMethod("");
159 assertEquals("/", simple.getPath());
160
161 simple = new FakeHttpMethod("/some/path/");
162 assertEquals("/some/path/", simple.getPath());
163 }
164
165 /***
166 * Tests response with a Trasfer-Encoding and Content-Length
167 */
168 public void testHttpMethodBaseTEandCL() throws Exception {
169 this.server.setHttpService(new SimpleChunkedService());
170
171 GetMethod httpget = new GetMethod("/test/");
172 try {
173 this.client.executeMethod(httpget);
174 assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
175 assertEquals("1234567890123", httpget.getResponseBodyAsString());
176 assertTrue(this.client.getHttpConnectionManager() instanceof SimpleHttpConnectionManager);
177 HttpConnection conn = this.client.getHttpConnectionManager().
178 getConnection(this.client.getHostConfiguration());
179 assertNotNull(conn);
180 conn.assertNotOpen();
181 } finally {
182 httpget.releaseConnection();
183 }
184 }
185
186 public void testConnectionAutoClose() throws Exception {
187 this.server.setHttpService(new ManyAService());
188
189 GetMethod httpget = new GetMethod("/test/");
190 try {
191 this.client.executeMethod(httpget);
192 assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
193 Reader response = new InputStreamReader(httpget.getResponseBodyAsStream());
194 int c;
195 while ((c = response.read()) != -1) {
196 assertEquals((int) 'A', c);
197 }
198 assertTrue(this.client.getHttpConnectionManager() instanceof SimpleHttpConnectionManager);
199 HttpConnection conn = this.client.getHttpConnectionManager().
200 getConnection(this.client.getHostConfiguration());
201 assertNotNull(conn);
202 conn.assertNotOpen();
203 } finally {
204 httpget.releaseConnection();
205 }
206 }
207
208 public void testSetGetQueryString1() {
209 HttpMethod method = new GetMethod();
210 String qs1 = "name1=value1&name2=value2";
211 method.setQueryString(qs1);
212 assertEquals(qs1, method.getQueryString());
213 }
214
215 public void testQueryURIEncoding() {
216 HttpMethod method = new GetMethod("http://server/servlet?foo=bar&baz=schmoo");
217 assertEquals("foo=bar&baz=schmoo", method.getQueryString());
218 }
219
220 public void testSetGetQueryString2() {
221 HttpMethod method = new GetMethod();
222 NameValuePair[] q1 = new NameValuePair[] {
223 new NameValuePair("name1", "value1"),
224 new NameValuePair("name2", "value2")
225 };
226 method.setQueryString(q1);
227 String qs1 = "name1=value1&name2=value2";
228 assertEquals(qs1, method.getQueryString());
229 }
230
231 /***
232 * Make sure that its OK to call releaseConnection if the connection has not been.
233 */
234 public void testReleaseConnection() {
235 HttpMethod method = new GetMethod("http://bogus.url/path/");
236 method.releaseConnection();
237 }
238
239 /***
240 * Tests empty body response
241 */
242 public void testEmptyBodyAsString() throws Exception {
243 this.server.setHttpService(new EmptyResponseService());
244
245 GetMethod httpget = new GetMethod("/test/");
246 try {
247 this.client.executeMethod(httpget);
248 assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
249 String response = httpget.getResponseBodyAsString();
250 assertNull(response);
251 } finally {
252 httpget.releaseConnection();
253 }
254 }
255
256
257 public void testEmptyBodyAsByteArray() throws Exception {
258 this.server.setHttpService(new EmptyResponseService());
259
260 GetMethod httpget = new GetMethod("/test/");
261 try {
262 this.client.executeMethod(httpget);
263 assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
264 byte[] response = httpget.getResponseBody();
265 assertNull(response);
266 } finally {
267 httpget.releaseConnection();
268 }
269 }
270
271 public void testUrlGetMethodWithPathQuery() {
272 GetMethod method = new GetMethod("http://www.fubar.com/path1/path2?query=string");
273 try {
274 assertEquals(
275 "Get URL",
276 "http://www.fubar.com/path1/path2?query=string",
277 method.getURI().toString()
278 );
279 } catch ( URIException e ) {
280 fail( "trouble getting URI: " + e );
281 }
282 assertEquals("Get Path", "/path1/path2", method.getPath());
283 assertEquals("Get query string", "query=string", method.getQueryString());
284
285 }
286
287 public void testUrlGetMethodWithPath() {
288 GetMethod method = new GetMethod("http://www.fubar.com/path1/path2");
289 try {
290 assertEquals(
291 "Get URL",
292 "http://www.fubar.com/path1/path2",
293 method.getURI().toString()
294 );
295 } catch ( URIException e ) {
296 fail( "trouble getting URI: " + e );
297 }
298 assertEquals("Get Path", "/path1/path2", method.getPath());
299 assertEquals("Get query string", null, method.getQueryString());
300 }
301
302 public void testUrlGetMethod() {
303 GetMethod method = new GetMethod("http://www.fubar.com/");
304 try {
305 assertEquals(
306 "Get URL",
307 "http://www.fubar.com/",
308 method.getURI().toString()
309 );
310 } catch ( URIException e ) {
311 fail( "trouble getting URI: " + e );
312 }
313 assertEquals("Get Path", "/", method.getPath());
314 assertEquals("Get query string", null, method.getQueryString());
315
316 }
317
318
319 public void testUrlGetMethodWithInvalidProtocol() {
320 try {
321 GetMethod method = new GetMethod("crap://www.fubar.com/");
322 fail("The use of invalid protocol must have resulted in an IllegalStateException");
323 }
324 catch(IllegalStateException expected) {
325 }
326 }
327 }