Source code: org/apache/http/impl/TestDefaultConnectionReuseStrategy.java
1 /*
2 * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/test/org/apache/http/impl/TestDefaultConnectionReuseStrategy.java $
3 * $Revision: 378645 $
4 * $Date: 2006-02-17 23:34:54 +0100 (Fri, 17 Feb 2006) $
5 * ====================================================================
6 *
7 * Copyright 2002-2004 The Apache Software Foundation
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ====================================================================
21 *
22 * This software consists of voluntary contributions made by many
23 * individuals on behalf of the Apache Software Foundation. For more
24 * information on the Apache Software Foundation, please see
25 * <http://www.apache.org/>.
26 *
27 */
28
29 package org.apache.http.impl;
30
31 import org.apache.http.ConnectionReuseStrategy;
32 import org.apache.http.Header;
33 import org.apache.http.HttpResponse;
34 import org.apache.http.HttpVersion;
35 import org.apache.http.StatusLine;
36 import org.apache.http.entity.BasicHttpEntity;
37 import org.apache.http.message.BasicHttpResponse;
38
39 import junit.framework.Test;
40 import junit.framework.TestCase;
41 import junit.framework.TestSuite;
42
43 public class TestDefaultConnectionReuseStrategy extends TestCase {
44
45 public TestDefaultConnectionReuseStrategy(String testName) {
46 super(testName);
47 }
48
49 // ------------------------------------------------------- TestCase Methods
50
51 public static Test suite() {
52 return new TestSuite(TestDefaultConnectionReuseStrategy.class);
53 }
54
55 // ------------------------------------------------------------------- Main
56 public static void main(String args[]) {
57 String[] testCaseName = { TestDefaultConnectionReuseStrategy.class.getName() };
58 junit.textui.TestRunner.main(testCaseName);
59 }
60
61 public void testIllegalResponseArg() throws Exception {
62 ConnectionReuseStrategy s = new DefaultConnectionReuseStrategy();
63 try {
64 s.keepAlive(null);
65 fail("IllegalArgumentException should have been thrown");
66 } catch (IllegalArgumentException ex) {
67 // expected
68 }
69 }
70
71 public void testNoContentLengthResponseHttp1_0() throws Exception {
72 BasicHttpEntity entity = new BasicHttpEntity();
73 entity.setChunked(false);
74 entity.setContentLength(-1);
75 StatusLine statusline = new StatusLine(HttpVersion.HTTP_1_0, 200, "OK");
76 HttpResponse response = new BasicHttpResponse(statusline);
77 response.setEntity(entity);
78
79 ConnectionReuseStrategy s = new DefaultConnectionReuseStrategy();
80 assertFalse(s.keepAlive(response));
81 }
82
83 public void testNoContentLengthResponseHttp1_1() throws Exception {
84 BasicHttpEntity entity = new BasicHttpEntity();
85 entity.setChunked(false);
86 entity.setContentLength(-1);
87 StatusLine statusline = new StatusLine(HttpVersion.HTTP_1_1, 200, "OK");
88 HttpResponse response = new BasicHttpResponse(statusline);
89 response.setEntity(entity);
90
91 ConnectionReuseStrategy s = new DefaultConnectionReuseStrategy();
92 assertFalse(s.keepAlive(response));
93 }
94
95 public void testChunkedContent() throws Exception {
96 BasicHttpEntity entity = new BasicHttpEntity();
97 entity.setChunked(true);
98 entity.setContentLength(-1);
99 StatusLine statusline = new StatusLine(HttpVersion.HTTP_1_1, 200, "OK");
100 HttpResponse response = new BasicHttpResponse(statusline);
101 response.setEntity(entity);
102
103 ConnectionReuseStrategy s = new DefaultConnectionReuseStrategy();
104 assertTrue(s.keepAlive(response));
105 }
106
107 public void testIgnoreInvalidKeepAlive() throws Exception {
108 BasicHttpEntity entity = new BasicHttpEntity();
109 entity.setChunked(false);
110 entity.setContentLength(-1);
111 StatusLine statusline = new StatusLine(HttpVersion.HTTP_1_0, 200, "OK");
112 HttpResponse response = new BasicHttpResponse(statusline);
113 response.addHeader(new Header("Connection", "keep-alive"));
114 response.setEntity(entity);
115
116 ConnectionReuseStrategy s = new DefaultConnectionReuseStrategy();
117 assertFalse(s.keepAlive(response));
118 }
119
120 public void testExplicitClose() throws Exception {
121 BasicHttpEntity entity = new BasicHttpEntity();
122 entity.setChunked(true);
123 entity.setContentLength(-1);
124 // Use HTTP 1.1
125 StatusLine statusline = new StatusLine(HttpVersion.HTTP_1_1, 200, "OK");
126 HttpResponse response = new BasicHttpResponse(statusline);
127 response.addHeader(new Header("Connection", "close"));
128 response.setEntity(entity);
129
130 ConnectionReuseStrategy s = new DefaultConnectionReuseStrategy();
131 assertFalse(s.keepAlive(response));
132 }
133
134 public void testExplicitKeepAlive() throws Exception {
135 BasicHttpEntity entity = new BasicHttpEntity();
136 entity.setChunked(false);
137 entity.setContentLength(10);
138 // Use HTTP 1.0
139 StatusLine statusline = new StatusLine(HttpVersion.HTTP_1_0, 200, "OK");
140 HttpResponse response = new BasicHttpResponse(statusline);
141 response.addHeader(new Header("Connection", "keep-alive"));
142 response.setEntity(entity);
143
144 ConnectionReuseStrategy s = new DefaultConnectionReuseStrategy();
145 assertTrue(s.keepAlive(response));
146 }
147
148 public void testHTTP10Default() throws Exception {
149 StatusLine statusline = new StatusLine(HttpVersion.HTTP_1_0, 200, "OK");
150 HttpResponse response = new BasicHttpResponse(statusline);
151
152 ConnectionReuseStrategy s = new DefaultConnectionReuseStrategy();
153 assertFalse(s.keepAlive(response));
154 }
155
156 public void testHTTP11Default() throws Exception {
157 StatusLine statusline = new StatusLine(HttpVersion.HTTP_1_1, 200, "OK");
158 HttpResponse response = new BasicHttpResponse(statusline);
159
160 ConnectionReuseStrategy s = new DefaultConnectionReuseStrategy();
161 assertTrue(s.keepAlive(response));
162 }
163
164 public void testFutureHTTP() throws Exception {
165 StatusLine statusline = new StatusLine(new HttpVersion(3, 45), 200, "OK");
166 HttpResponse response = new BasicHttpResponse(statusline);
167
168 ConnectionReuseStrategy s = new DefaultConnectionReuseStrategy();
169 assertTrue(s.keepAlive(response));
170 }
171
172 public void testBrokenConnectionDirective1() throws Exception {
173 // Use HTTP 1.0
174 StatusLine statusline = new StatusLine(HttpVersion.HTTP_1_0, 200, "OK");
175 HttpResponse response = new BasicHttpResponse(statusline);
176 response.addHeader(new Header("Connection", "keep--alive"));
177
178 ConnectionReuseStrategy s = new DefaultConnectionReuseStrategy();
179 assertFalse(s.keepAlive(response));
180 }
181
182 public void testBrokenConnectionDirective2() throws Exception {
183 // Use HTTP 1.0
184 StatusLine statusline = new StatusLine(HttpVersion.HTTP_1_0, 200, "OK");
185 HttpResponse response = new BasicHttpResponse(statusline);
186 response.addHeader(new Header("Connection", null));
187
188 ConnectionReuseStrategy s = new DefaultConnectionReuseStrategy();
189 assertFalse(s.keepAlive(response));
190 }
191 }
192