001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestConnectionPersistence.java,v 1.2 2004/12/20 11:42:30 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: */
028:
029: package org.apache.commons.httpclient;
030:
031: import java.io.IOException;
032:
033: import org.apache.commons.httpclient.methods.GetMethod;
034: import org.apache.commons.httpclient.methods.PostMethod;
035: import org.apache.commons.httpclient.methods.StringRequestEntity;
036: import org.apache.commons.httpclient.server.HttpRequestHandler;
037: import org.apache.commons.httpclient.server.SimpleHttpServerConnection;
038: import org.apache.commons.httpclient.server.SimpleProxy;
039: import org.apache.commons.httpclient.server.SimpleRequest;
040: import org.apache.commons.httpclient.server.SimpleResponse;
041:
042: import junit.framework.Test;
043: import junit.framework.TestSuite;
044:
045: /**
046: * Connection persistence tests
047: *
048: * @author Oleg Kalnichevski
049: *
050: * @version $Id: TestConnectionPersistence.java 480424 2006-11-29 05:56:49Z bayard $
051: */
052: public class TestConnectionPersistence extends HttpClientTestBase {
053:
054: // ------------------------------------------------------------ Constructor
055: public TestConnectionPersistence(final String testName)
056: throws IOException {
057: super (testName);
058: }
059:
060: // ------------------------------------------------------------------- Main
061: public static void main(String args[]) {
062: String[] testCaseName = { TestConnectionPersistence.class
063: .getName() };
064: junit.textui.TestRunner.main(testCaseName);
065: }
066:
067: // ------------------------------------------------------- TestCase Methods
068:
069: public static Test suite() {
070: return new TestSuite(TestConnectionPersistence.class);
071: }
072:
073: // ----------------------------------------------------------- Test Methods
074:
075: public void testConnPersisenceHTTP10() throws Exception {
076: this .server.setHttpService(new EchoService());
077:
078: AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
079:
080: this .client.getParams().setVersion(HttpVersion.HTTP_1_0);
081: this .client.setHttpConnectionManager(connman);
082:
083: PostMethod httppost = new PostMethod("/test/");
084: httppost.setRequestEntity(new StringRequestEntity("stuff",
085: null, null));
086: try {
087: this .client.executeMethod(httppost);
088: } finally {
089: httppost.releaseConnection();
090: }
091: assertFalse(connman.getConection().isOpen());
092:
093: httppost = new PostMethod("/test/");
094: httppost.setRequestEntity(new StringRequestEntity("more stuff",
095: null, null));
096: try {
097: this .client.executeMethod(httppost);
098: } finally {
099: httppost.releaseConnection();
100: }
101: assertFalse(connman.getConection().isOpen());
102: }
103:
104: public void testConnPersisenceHTTP11() throws Exception {
105: this .server.setHttpService(new EchoService());
106:
107: AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
108:
109: this .client.getParams().setVersion(HttpVersion.HTTP_1_1);
110: this .client.setHttpConnectionManager(connman);
111:
112: PostMethod httppost = new PostMethod("/test/");
113: httppost.setRequestEntity(new StringRequestEntity("stuff",
114: null, null));
115: try {
116: this .client.executeMethod(httppost);
117: } finally {
118: httppost.releaseConnection();
119: }
120: assertTrue(connman.getConection().isOpen());
121:
122: httppost = new PostMethod("/test/");
123: httppost.setRequestEntity(new StringRequestEntity("more stuff",
124: null, null));
125: try {
126: this .client.executeMethod(httppost);
127: } finally {
128: httppost.releaseConnection();
129: }
130: assertTrue(connman.getConection().isOpen());
131: }
132:
133: public void testConnClose() throws Exception {
134: this .server.setHttpService(new EchoService());
135:
136: AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
137:
138: this .client.getParams().setVersion(HttpVersion.HTTP_1_1);
139: this .client.setHttpConnectionManager(connman);
140:
141: PostMethod httppost = new PostMethod("/test/");
142: httppost.setRequestEntity(new StringRequestEntity("stuff",
143: null, null));
144: try {
145: this .client.executeMethod(httppost);
146: } finally {
147: httppost.releaseConnection();
148: }
149: assertTrue(connman.getConection().isOpen());
150:
151: httppost = new PostMethod("/test/");
152: httppost.setRequestHeader("Connection", "close");
153: httppost.setRequestEntity(new StringRequestEntity("more stuff",
154: null, null));
155: try {
156: this .client.executeMethod(httppost);
157: } finally {
158: httppost.releaseConnection();
159: }
160: assertFalse(connman.getConection().isOpen());
161: }
162:
163: public void testConnKeepAlive() throws Exception {
164: this .server.setHttpService(new EchoService());
165:
166: AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
167:
168: this .client.getParams().setVersion(HttpVersion.HTTP_1_0);
169: this .client.setHttpConnectionManager(connman);
170:
171: PostMethod httppost = new PostMethod("/test/");
172: httppost.setRequestEntity(new StringRequestEntity("stuff",
173: null, null));
174: try {
175: this .client.executeMethod(httppost);
176: } finally {
177: httppost.releaseConnection();
178: }
179: assertFalse(connman.getConection().isOpen());
180:
181: httppost = new PostMethod("/test/");
182: httppost.setRequestHeader("Connection", "keep-alive");
183: httppost.setRequestEntity(new StringRequestEntity("more stuff",
184: null, null));
185: try {
186: this .client.executeMethod(httppost);
187: } finally {
188: httppost.releaseConnection();
189: }
190: assertTrue(connman.getConection().isOpen());
191: }
192:
193: public void testRequestConnClose() throws Exception {
194: this .server.setRequestHandler(new HttpRequestHandler() {
195:
196: public boolean processRequest(
197: final SimpleHttpServerConnection conn,
198: final SimpleRequest request) throws IOException {
199:
200: // Make sure the request if fully consumed
201: request.getBodyBytes();
202:
203: SimpleResponse response = new SimpleResponse();
204: response.setStatusLine(HttpVersion.HTTP_1_1,
205: HttpStatus.SC_OK);
206: response.setBodyString("stuff back");
207:
208: conn.setKeepAlive(true);
209: conn.writeResponse(response);
210:
211: return true;
212: }
213:
214: });
215:
216: AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
217:
218: this .client.getParams().setVersion(HttpVersion.HTTP_1_0);
219: this .client.setHttpConnectionManager(connman);
220:
221: PostMethod httppost = new PostMethod("/test/");
222: httppost.setRequestHeader("Connection", "close");
223: httppost.setRequestEntity(new StringRequestEntity("stuff",
224: null, null));
225: try {
226: this .client.executeMethod(httppost);
227: } finally {
228: httppost.releaseConnection();
229: }
230: assertFalse(connman.getConection().isOpen());
231: }
232:
233: public void testProxyConnClose() throws Exception {
234: this .server.setHttpService(new EchoService());
235: this .proxy = new SimpleProxy();
236: this .client.getHostConfiguration().setProxy(
237: proxy.getLocalAddress(), proxy.getLocalPort());
238:
239: AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
240:
241: this .client.setHttpConnectionManager(connman);
242:
243: GetMethod httpget = new GetMethod("/test/");
244: try {
245: this .client.executeMethod(httpget);
246: } finally {
247: httpget.releaseConnection();
248: }
249: assertTrue(connman.getConection().isOpen());
250:
251: httpget = new GetMethod("/test/");
252: httpget.setRequestHeader("Proxy-Connection", "Close");
253: try {
254: this .client.executeMethod(httpget);
255: } finally {
256: httpget.releaseConnection();
257: }
258: assertFalse(connman.getConection().isOpen());
259: assertEquals("Close", httpget.getRequestHeader(
260: "Proxy-Connection").getValue());
261: }
262:
263: }
|