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 package org.apache.commons.httpclient;
30
31 import java.io.IOException;
32
33 import org.apache.commons.httpclient.methods.GetMethod;
34 import org.apache.commons.httpclient.methods.PostMethod;
35 import org.apache.commons.httpclient.methods.StringRequestEntity;
36 import org.apache.commons.httpclient.server.HttpRequestHandler;
37 import org.apache.commons.httpclient.server.SimpleHttpServerConnection;
38 import org.apache.commons.httpclient.server.SimpleProxy;
39 import org.apache.commons.httpclient.server.SimpleRequest;
40 import org.apache.commons.httpclient.server.SimpleResponse;
41
42 import junit.framework.Test;
43 import junit.framework.TestSuite;
44
45 /***
46 * Connection persistence tests
47 *
48 * @author Oleg Kalnichevski
49 *
50 * @version $Id: TestConnectionPersistence.java 608014 2008-01-02 05:48:53Z rolandw $
51 */
52 public class TestConnectionPersistence extends HttpClientTestBase {
53
54
55 public TestConnectionPersistence(final String testName) throws IOException {
56 super(testName);
57 }
58
59
60 public static void main(String args[]) {
61 String[] testCaseName = { TestConnectionPersistence.class.getName() };
62 junit.textui.TestRunner.main(testCaseName);
63 }
64
65
66
67 public static Test suite() {
68 return new TestSuite(TestConnectionPersistence.class);
69 }
70
71
72
73 public void testConnPersisenceHTTP10() throws Exception {
74 this.server.setHttpService(new EchoService());
75
76 AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
77
78 this.client.getParams().setVersion(HttpVersion.HTTP_1_0);
79 this.client.setHttpConnectionManager(connman);
80
81 PostMethod httppost = new PostMethod("/test/");
82 httppost.setRequestEntity(new StringRequestEntity("stuff", null, null));
83 try {
84 this.client.executeMethod(httppost);
85 } finally {
86 httppost.releaseConnection();
87 }
88 assertFalse(connman.getConection().isOpen());
89
90 httppost = new PostMethod("/test/");
91 httppost.setRequestEntity(new StringRequestEntity("more stuff", null, null));
92 try {
93 this.client.executeMethod(httppost);
94 } finally {
95 httppost.releaseConnection();
96 }
97 assertFalse(connman.getConection().isOpen());
98 }
99
100 public void testConnPersisenceHTTP11() throws Exception {
101 this.server.setHttpService(new EchoService());
102
103 AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
104
105 this.client.getParams().setVersion(HttpVersion.HTTP_1_1);
106 this.client.setHttpConnectionManager(connman);
107
108 PostMethod httppost = new PostMethod("/test/");
109 httppost.setRequestEntity(new StringRequestEntity("stuff", null, null));
110 try {
111 this.client.executeMethod(httppost);
112 } finally {
113 httppost.releaseConnection();
114 }
115 assertTrue(connman.getConection().isOpen());
116
117 httppost = new PostMethod("/test/");
118 httppost.setRequestEntity(new StringRequestEntity("more stuff", null, null));
119 try {
120 this.client.executeMethod(httppost);
121 } finally {
122 httppost.releaseConnection();
123 }
124 assertTrue(connman.getConection().isOpen());
125 }
126
127 public void testConnClose() throws Exception {
128 this.server.setHttpService(new EchoService());
129
130 AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
131
132 this.client.getParams().setVersion(HttpVersion.HTTP_1_1);
133 this.client.setHttpConnectionManager(connman);
134
135 PostMethod httppost = new PostMethod("/test/");
136 httppost.setRequestEntity(new StringRequestEntity("stuff", null, null));
137 try {
138 this.client.executeMethod(httppost);
139 } finally {
140 httppost.releaseConnection();
141 }
142 assertTrue(connman.getConection().isOpen());
143
144 httppost = new PostMethod("/test/");
145 httppost.setRequestHeader("Connection", "close");
146 httppost.setRequestEntity(new StringRequestEntity("more stuff", null, null));
147 try {
148 this.client.executeMethod(httppost);
149 } finally {
150 httppost.releaseConnection();
151 }
152 assertFalse(connman.getConection().isOpen());
153 }
154
155 public void testConnKeepAlive() throws Exception {
156 this.server.setHttpService(new EchoService());
157
158 AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
159
160 this.client.getParams().setVersion(HttpVersion.HTTP_1_0);
161 this.client.setHttpConnectionManager(connman);
162
163 PostMethod httppost = new PostMethod("/test/");
164 httppost.setRequestEntity(new StringRequestEntity("stuff", null, null));
165 try {
166 this.client.executeMethod(httppost);
167 } finally {
168 httppost.releaseConnection();
169 }
170 assertFalse(connman.getConection().isOpen());
171
172 httppost = new PostMethod("/test/");
173 httppost.setRequestHeader("Connection", "keep-alive");
174 httppost.setRequestEntity(new StringRequestEntity("more stuff", null, null));
175 try {
176 this.client.executeMethod(httppost);
177 } finally {
178 httppost.releaseConnection();
179 }
180 assertTrue(connman.getConection().isOpen());
181 }
182
183 public void testRequestConnClose() throws Exception {
184 this.server.setRequestHandler(new HttpRequestHandler() {
185
186 public boolean processRequest(
187 final SimpleHttpServerConnection conn,
188 final SimpleRequest request) throws IOException {
189
190
191 request.getBodyBytes();
192
193 SimpleResponse response = new SimpleResponse();
194 response.setStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK);
195 response.setBodyString("stuff back");
196
197 conn.setKeepAlive(true);
198 conn.writeResponse(response);
199
200 return true;
201 }
202
203 });
204
205 AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
206
207 this.client.getParams().setVersion(HttpVersion.HTTP_1_0);
208 this.client.setHttpConnectionManager(connman);
209
210 PostMethod httppost = new PostMethod("/test/");
211 httppost.setRequestHeader("Connection", "close");
212 httppost.setRequestEntity(new StringRequestEntity("stuff", null, null));
213 try {
214 this.client.executeMethod(httppost);
215 } finally {
216 httppost.releaseConnection();
217 }
218 assertFalse(connman.getConection().isOpen());
219 }
220
221 public void testProxyConnClose() throws Exception {
222 this.server.setHttpService(new EchoService());
223 this.proxy = new SimpleProxy();
224 this.client.getHostConfiguration().setProxy(
225 proxy.getLocalAddress(),
226 proxy.getLocalPort());
227
228 AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
229
230 this.client.setHttpConnectionManager(connman);
231
232 GetMethod httpget = new GetMethod("/test/");
233 try {
234 this.client.executeMethod(httpget);
235 } finally {
236 httpget.releaseConnection();
237 }
238 assertTrue(connman.getConection().isOpen());
239
240 httpget = new GetMethod("/test/");
241 httpget.setRequestHeader("Proxy-Connection", "Close");
242 try {
243 this.client.executeMethod(httpget);
244 } finally {
245 httpget.releaseConnection();
246 }
247 assertFalse(connman.getConection().isOpen());
248 assertEquals("Close", httpget.getRequestHeader("Proxy-Connection").getValue());
249 }
250
251
252 }
253