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