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 junit.framework.Test;
34 import junit.framework.TestSuite;
35
36 import org.apache.commons.httpclient.methods.GetMethod;
37
38 /***
39 * Simple tests of {@link GetMethod}.
40 *
41 * @author Rodney Waldhoff
42 * @version $Id: TestGetMethodLocal.java,v 1.14 2004/02/22 18:08:49 olegk Exp $
43 */
44 public class TestGetMethodLocal extends TestLocalHostBase {
45
46
47
48 public TestGetMethodLocal(String testName) {
49 super(testName);
50 }
51
52
53
54
55 public static Test suite() {
56 return new TestSuite(TestGetMethodLocal.class);
57 }
58
59
60
61 public void testGetSlash() {
62 HttpClient client = createHttpClient();
63
64 GetMethod method = new GetMethod("/");
65
66 try {
67 client.executeMethod(method);
68 } catch (Throwable t) {
69 t.printStackTrace();
70 fail("Unable to execute method : " + t.toString());
71 }
72
73 try {
74 String data = method.getResponseBodyAsString();
75 assertTrue("No data returned.",(data.length() > 0));
76 } catch (Throwable t) {
77 t.printStackTrace();
78 fail("Unable to execute method : " + t.toString());
79 }
80 assertEquals(200,method.getStatusCode());
81 }
82
83 public void testExecuteMultipleMethods() throws Exception {
84
85 HttpClient client = createHttpClient();
86
87 GetMethod getSlash = new GetMethod("/");
88 for(int i=0;i<10;i++) {
89 assertEquals(200, client.executeMethod(getSlash));
90 String data = getSlash.getResponseBodyAsString();
91 assertTrue(null != data);
92 assertTrue(data.length() > 0);
93 getSlash.recycle();
94 getSlash.setPath("/");
95 }
96 }
97
98 public void testRecycle() {
99 HttpClient client = createHttpClient(null);
100
101 GetMethod method = new GetMethod("/");
102
103 try {
104 client.executeMethod(method);
105 } catch (Throwable t) {
106 t.printStackTrace();
107 fail("Unable to execute method : " + t.toString());
108 }
109
110 try {
111 String data = method.getResponseBodyAsString();
112 assertTrue("No data returned.",(data.length() > 0));
113 } catch (Throwable t) {
114 t.printStackTrace();
115 fail("Unable to execute method : " + t.toString());
116 }
117 assertEquals(200,method.getStatusCode());
118
119 method.recycle();
120 method.setPath("/");
121
122 try {
123 client.executeMethod(method);
124 } catch (Throwable t) {
125 t.printStackTrace();
126 fail("Unable to execute method : " + t.toString());
127 }
128
129 try {
130 String data = method.getResponseBodyAsString();
131 assertTrue("No data returned.",(data.length() > 0));
132 } catch (Throwable t) {
133 t.printStackTrace();
134 fail("Unable to execute method : " + t.toString());
135 }
136 assertEquals(200,method.getStatusCode());
137
138 }
139
140 public void test404() {
141 HttpClient client = createHttpClient(null);
142
143 GetMethod method = new GetMethod("/i/am/assuming/this/path/and/file/doesnt/exist/on/the/web/server.xyzzy");
144
145 try {
146 client.executeMethod(method);
147 } catch (Throwable t) {
148 t.printStackTrace();
149 fail("Unable to execute method : " + t.toString());
150 }
151 assertEquals(404,method.getStatusCode());
152
153 }
154
155 /***
156 * The intent of this test is to allow for the incomplete parsing of a GET
157 * response, and to make it particularly tricky, the GET response issues
158 * a Connection: close".
159 *
160 * <p>This wants to insure that a recoverable exception is not unexpectedly
161 * triggered.</p>
162 */
163 public void testGetResponseNotReadAutoRecover() {
164 HttpClient client = createHttpClient(null);
165
166 try {
167
168 String path = "/";
169 GetMethod method1 = new GetMethod(path);
170 method1.addRequestHeader("Connection", "close");
171 client.executeMethod(method1);
172
173
174 GetMethod method2 = new GetMethod(path);
175 client.executeMethod(method2);
176 }
177 catch (Exception ioe) {
178
179 fail("Problem executing method : " + ioe.toString() );
180 }
181 }
182
183 }