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 155418 2005-02-26 13:01:52Z dirkv $
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 for(int i=0;i<10;i++) {
88 GetMethod getSlash = new GetMethod("/");
89 assertEquals(200, client.executeMethod(getSlash));
90 String data = getSlash.getResponseBodyAsString();
91 assertTrue(null != data);
92 assertTrue(data.length() > 0);
93 }
94 }
95
96 public void test404() {
97 HttpClient client = createHttpClient(null);
98
99 GetMethod method = new GetMethod("/i/am/assuming/this/path/and/file/doesnt/exist/on/the/web/server.xyzzy");
100
101 try {
102 client.executeMethod(method);
103 } catch (Throwable t) {
104 t.printStackTrace();
105 fail("Unable to execute method : " + t.toString());
106 }
107 assertEquals(404,method.getStatusCode());
108
109 }
110
111 /***
112 * The intent of this test is to allow for the incomplete parsing of a GET
113 * response, and to make it particularly tricky, the GET response issues
114 * a Connection: close".
115 *
116 * <p>This wants to insure that a recoverable exception is not unexpectedly
117 * triggered.</p>
118 */
119 public void testGetResponseNotReadAutoRecover() {
120 HttpClient client = createHttpClient(null);
121
122 try {
123
124 String path = "/";
125 GetMethod method1 = new GetMethod(path);
126 method1.addRequestHeader("Connection", "close");
127 client.executeMethod(method1);
128
129
130 GetMethod method2 = new GetMethod(path);
131 client.executeMethod(method2);
132 }
133 catch (Exception ioe) {
134
135 fail("Problem executing method : " + ioe.toString() );
136 }
137 }
138
139 }