1
2
3
4
5
6
7 package org.mule.transport.http.functional;
8
9 import static junit.framework.Assert.assertNotNull;
10 import static org.junit.Assert.assertEquals;
11 import org.mule.api.endpoint.InboundEndpoint;
12 import org.mule.tck.junit4.FunctionalTestCase;
13 import org.mule.tck.junit4.rule.DynamicPort;
14 import org.mule.transport.http.HttpConstants;
15
16 import org.apache.commons.httpclient.Header;
17 import org.apache.commons.httpclient.HttpClient;
18 import org.apache.commons.httpclient.HttpMethod;
19 import org.apache.commons.httpclient.HttpStatus;
20 import org.apache.commons.httpclient.HttpVersion;
21 import org.apache.commons.httpclient.methods.GetMethod;
22 import org.apache.commons.httpclient.params.HttpClientParams;
23 import org.apache.commons.lang.StringUtils;
24 import org.junit.Rule;
25 import org.junit.Test;
26
27
28
29
30 public class HttpKeepAliveFunctionalTestCase extends FunctionalTestCase
31 {
32
33 private static final String IN_CONNECTOR_NO_KEEP_ALIVE_EP_NO_KEEP_ALIVE = "inConnectorNoKeepAliveEpNoKeepAlive";
34 private static final String IN_CONNECTOR_KEEP_ALIVE_EP_KEEP_ALIVE = "inConnectorKeepAliveEpKeepAlive";
35 private static final String IN_CONNECTOR_NO_KEEP_ALIVE_EP_KEEP_ALIVE = "inConnectorNoKeepAliveEpKeepAlive";
36 private static final String IN_CONNECTOR_KEEP_ALIVE_EP_NO_KEEP_ALIVE = "inConnectorKeepAliveEpNoKeepAlive";
37 private static final String IN_CONNECTOR_NO_KEEP_ALIVE_EP_EMPTY = "inConnectorNoKeepAliveEpEmpty";
38 private static final String IN_CONNECTOR_KEEP_ALIVE_EP_EMPTY = "inConnectorKeepAliveEpEmpty";
39
40 private static final String CLOSE = "close";
41 private static final String KEEP_ALIVE = "Keep-Alive";
42 private static final String EMPTY = "";
43
44 @Rule
45 public DynamicPort dynamicPort1 = new DynamicPort("port1");
46
47 @Rule
48 public DynamicPort dynamicPort2 = new DynamicPort("port2");
49
50 @Rule
51 public DynamicPort dynamicPort3 = new DynamicPort("port3");
52
53 @Rule
54 public DynamicPort dynamicPort4 = new DynamicPort("port4");
55
56 @Rule
57 public DynamicPort dynamicPort5 = new DynamicPort("port5");
58
59 @Rule
60 public DynamicPort dynamicPort6 = new DynamicPort("port6");
61
62 @Override
63 protected String getConfigResources()
64 {
65 return "http-keep-alive-config.xml";
66 }
67
68 @Test
69 public void testHttp10ConnectorKeepAliveEpEmpty() throws Exception
70 {
71 doTestKeepAliveInHttp10(getEndpointAddress(IN_CONNECTOR_KEEP_ALIVE_EP_EMPTY));
72 }
73
74 @Test
75 public void testHttp10ConnectorNoKeepAliveEpEmpty() throws Exception
76 {
77 doTestNoKeepAliveInHttp10(getEndpointAddress(IN_CONNECTOR_NO_KEEP_ALIVE_EP_EMPTY));
78 }
79
80 @Test
81 public void testHttp10ConnectorKeepAliveEpNoKeepAlive() throws Exception
82 {
83 doTestNoKeepAliveInHttp10(getEndpointAddress(IN_CONNECTOR_KEEP_ALIVE_EP_NO_KEEP_ALIVE));
84 }
85
86 @Test
87 public void testHttp10ConnectorNoKeepAliveEpKeepAlive() throws Exception
88 {
89 doTestKeepAliveInHttp10(getEndpointAddress(IN_CONNECTOR_NO_KEEP_ALIVE_EP_KEEP_ALIVE));
90 }
91
92 @Test
93 public void testHttp10ConnectorKeepAliveEpKeepAlive() throws Exception
94 {
95 doTestKeepAliveInHttp10(getEndpointAddress(IN_CONNECTOR_KEEP_ALIVE_EP_KEEP_ALIVE));
96 }
97
98 @Test
99 public void testHttp10ConnectorNoKeepAliveEpNoKeepAlive() throws Exception
100 {
101 doTestNoKeepAliveInHttp10(getEndpointAddress(IN_CONNECTOR_NO_KEEP_ALIVE_EP_NO_KEEP_ALIVE));
102 }
103
104 @Test
105 public void testHttp11ConnectorKeepAliveEpEmpty() throws Exception
106 {
107 doTestKeepAliveInHttp11(getEndpointAddress(IN_CONNECTOR_KEEP_ALIVE_EP_EMPTY));
108 }
109
110 @Test
111 public void testHttp11ConnectorNoKeepAliveEpEmpty() throws Exception
112 {
113 doTestNoKeepAliveInHttp11(getEndpointAddress(IN_CONNECTOR_NO_KEEP_ALIVE_EP_EMPTY));
114 }
115
116 @Test
117 public void testHttp11ConnectorKeepAliveEpNoKeepAlive() throws Exception
118 {
119 doTestNoKeepAliveInHttp11(getEndpointAddress(IN_CONNECTOR_KEEP_ALIVE_EP_NO_KEEP_ALIVE));
120 }
121
122 @Test
123 public void testHttp11ConnectorNoKeepAliveEpKeepAlive() throws Exception
124 {
125 doTestKeepAliveInHttp11(getEndpointAddress(IN_CONNECTOR_NO_KEEP_ALIVE_EP_KEEP_ALIVE));
126 }
127
128 @Test
129 public void testHttp11ConnectorKeepAliveEpKeepAlive() throws Exception
130 {
131 doTestKeepAliveInHttp11(getEndpointAddress(IN_CONNECTOR_KEEP_ALIVE_EP_KEEP_ALIVE));
132 }
133
134 @Test
135 public void testHttp11ConnectorNoKeepAliveEpNoKeepAlive() throws Exception
136 {
137 doTestNoKeepAliveInHttp11(getEndpointAddress(IN_CONNECTOR_NO_KEEP_ALIVE_EP_NO_KEEP_ALIVE));
138 }
139
140 private void doTestKeepAliveInHttp10(String endpointAddress) throws Exception
141 {
142 HttpClient httpClient = setupHttpClient(HttpVersion.HTTP_1_0);
143
144 doTestHttp(endpointAddress, EMPTY, CLOSE, httpClient);
145 doTestHttp(endpointAddress, CLOSE, CLOSE, httpClient);
146 doTestHttp(endpointAddress, KEEP_ALIVE, KEEP_ALIVE, httpClient);
147 }
148
149 private void doTestNoKeepAliveInHttp10(String endpointAddress) throws Exception
150 {
151 HttpClient httpClient = setupHttpClient(HttpVersion.HTTP_1_0);
152
153 doTestHttp(endpointAddress, EMPTY, CLOSE, httpClient);
154 doTestHttp(endpointAddress, CLOSE, CLOSE, httpClient);
155 doTestHttp(endpointAddress, KEEP_ALIVE, CLOSE, httpClient);
156 }
157
158 private void doTestKeepAliveInHttp11(String endpointAddress) throws Exception
159 {
160 HttpClient httpClient = setupHttpClient(HttpVersion.HTTP_1_1);
161
162 doTestHttp(endpointAddress, EMPTY, EMPTY, httpClient);
163 doTestHttp(endpointAddress, CLOSE, CLOSE, httpClient);
164 doTestHttp(endpointAddress, KEEP_ALIVE, EMPTY, httpClient);
165 }
166
167 private void doTestNoKeepAliveInHttp11(String endpointAddress) throws Exception
168 {
169 HttpClient httpClient = setupHttpClient(HttpVersion.HTTP_1_1);
170
171 doTestHttp(endpointAddress, EMPTY, CLOSE, httpClient);
172 doTestHttp(endpointAddress, CLOSE, CLOSE, httpClient);
173 doTestHttp(endpointAddress, KEEP_ALIVE, CLOSE, httpClient);
174 }
175
176 private HttpClient setupHttpClient(HttpVersion version)
177 {
178 HttpClientParams params = new HttpClientParams();
179 params.setVersion(version);
180
181 return new HttpClient(params);
182 }
183
184 private void doTestHttp(String url, String inConnectionHeaderValue, String expectedConnectionHeaderValue, HttpClient httpClient) throws Exception
185 {
186 GetMethod request = new GetMethod(url);
187 if (StringUtils.isEmpty(inConnectionHeaderValue))
188 {
189 request.removeRequestHeader(HttpConstants.HEADER_CONNECTION);
190 }
191 else
192 {
193 request.setRequestHeader(HttpConstants.HEADER_CONNECTION, inConnectionHeaderValue);
194 }
195
196 runHttpMethodAndAssertConnectionHeader(request, expectedConnectionHeaderValue, httpClient);
197
198
199 request = new GetMethod(url);
200 request.setRequestHeader(HttpConstants.HEADER_CONNECTION, CLOSE);
201 int status = httpClient.executeMethod(request);
202 assertEquals(HttpStatus.SC_OK, status);
203 }
204
205 private void runHttpMethodAndAssertConnectionHeader(HttpMethod request, String expectedConnectionHeaderValue, HttpClient httpClient) throws Exception
206 {
207 int status = httpClient.executeMethod(request);
208 assertEquals(HttpStatus.SC_OK, status);
209
210 String connectionHeader;
211 if (httpClient.getParams().getVersion().equals(HttpVersion.HTTP_1_0))
212 {
213 connectionHeader = request.getResponseHeader(HttpConstants.HEADER_CONNECTION).getValue();
214 assertNotNull(connectionHeader);
215 }
216 else
217 {
218 Header responseHeader = request.getResponseHeader(HttpConstants.HEADER_CONNECTION);
219 connectionHeader = responseHeader != null ? responseHeader.getValue() : EMPTY;
220 }
221 assertEquals(expectedConnectionHeaderValue, connectionHeader);
222 }
223
224 private InboundEndpoint getEndpoint(String endpointName)
225 {
226 return muleContext.getRegistry().lookupObject(endpointName);
227 }
228
229 private String getEndpointAddress(String endpointName)
230 {
231 return getEndpoint(endpointName).getAddress();
232 }
233 }
234
235