View Javadoc

1   /*
2    * $Id: HttpKeepAliveFunctionalTestCase.java 20297 2010-11-22 18:49:18Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.http.functional;
12  
13  import org.mule.api.endpoint.InboundEndpoint;
14  import org.mule.module.client.MuleClient;
15  import org.mule.tck.DynamicPortTestCase;
16  import org.mule.transport.http.HttpConstants;
17  
18  import org.apache.commons.httpclient.Header;
19  import org.apache.commons.httpclient.HttpClient;
20  import org.apache.commons.httpclient.HttpMethod;
21  import org.apache.commons.httpclient.HttpStatus;
22  import org.apache.commons.httpclient.HttpVersion;
23  import org.apache.commons.httpclient.methods.GetMethod;
24  import org.apache.commons.httpclient.params.HttpClientParams;
25  
26  /**
27   * Tests as per http://www.io.com/~maus/HttpKeepAlive.html
28   */
29  public class HttpKeepAliveFunctionalTestCase extends DynamicPortTestCase
30  {
31      //private static final String URL_WITHOUT_EP_OVERRIDE = "http://localhost:60213/http-in";
32      //private static final String URL_WITH_EP_OVERRIDE = "http://localhost:60216/http-in";
33      
34      private HttpClient http10Client;
35      private HttpClient http11Client;
36      private MuleClient client = null;
37      
38      @Override
39      protected void doSetUp() throws Exception
40      {
41          super.doSetUp();
42  
43          http10Client = setupHttpClient(HttpVersion.HTTP_1_0);
44          http11Client = setupHttpClient(HttpVersion.HTTP_1_1);
45          client = new MuleClient(muleContext);
46      }
47      
48      private HttpClient setupHttpClient(HttpVersion version)
49      {
50          HttpClientParams params = new HttpClientParams();
51          params.setVersion(version);
52          return new HttpClient(params);
53      }
54  
55      @Override
56      protected String getConfigResources()
57      {
58          return "http-keep-alive-config.xml";
59      }
60      
61      public void testHttp10WithoutConnectionHeader() throws Exception
62      {
63          GetMethod request = new GetMethod(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("inWithoutEndpointOverride")).getAddress());
64          request.removeRequestHeader(HttpConstants.HEADER_CONNECTION);
65          runHttp10MethodAndAssertConnectionHeader(request, "close");
66      }
67      
68      public void testHttp10WithCloseConnectionHeader() throws Exception
69      {
70          GetMethod request = new GetMethod(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("inWithoutEndpointOverride")).getAddress());
71          request.setRequestHeader(HttpConstants.HEADER_CONNECTION, "close");
72          runHttp10MethodAndAssertConnectionHeader(request, "close");
73      }
74      
75      public void testHttp10KeepAlive() throws Exception
76      {
77          doTestKeepAlive(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("inWithoutEndpointOverride")).getAddress());
78      }
79      
80      public void testHttp10KeepAliveWitEpOverride() throws Exception
81      {
82          doTestKeepAlive(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("inWithoutEndpointOverride")).getAddress());
83      }
84      
85      private void doTestKeepAlive(String url) throws Exception
86      {
87          GetMethod request = new GetMethod(url);
88          request.addRequestHeader(HttpConstants.HEADER_CONNECTION, "Keep-Alive");
89          runHttp10MethodAndAssertConnectionHeader(request, "Keep-Alive");
90          
91          request.setRequestHeader(HttpConstants.HEADER_CONNECTION, "close");
92          runHttp10MethodAndAssertConnectionHeader(request, "close");
93      }
94      
95      private void runHttp10MethodAndAssertConnectionHeader(HttpMethod request, String expectedConnectionHeaderValue) throws Exception
96      {
97          int status = http10Client.executeMethod(request);
98          assertEquals(HttpStatus.SC_OK, status);
99          String connectionHeader = request.getResponseHeader(HttpConstants.HEADER_CONNECTION).getValue();
100         assertNotNull(connectionHeader);
101         assertEquals(expectedConnectionHeaderValue, connectionHeader);
102     }
103     
104     public void testHttp11KeepAlive() throws Exception
105     {
106         doTestHttp11KeepAlive(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("inWithoutEndpointOverride")).getAddress());
107     }
108     
109     public void testHttp11KeepAliveWithEpOverride() throws Exception
110     {
111         doTestHttp11KeepAlive(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("inWithoutEndpointOverride")).getAddress());
112     }
113     
114     public void doTestHttp11KeepAlive(String url) throws Exception
115     {
116         GetMethod request = new GetMethod(url);
117         runHttp11MethodAndAssert(request);
118         
119         // the connection should be still open, send another request and terminate the connection
120         request = new GetMethod(url);
121         request.setRequestHeader(HttpConstants.HEADER_CONNECTION, "close");
122         runHttp11MethodAndAssert(request);
123         
124         Header connectHeader = request.getResponseHeader(HttpConstants.HEADER_CONNECTION);
125         assertNotNull(connectHeader);
126         assertEquals("close", connectHeader.getValue());
127     }
128         
129     private void runHttp11MethodAndAssert(HttpMethod request) throws Exception
130     {
131         int status = http11Client.executeMethod(request);
132         assertEquals(HttpStatus.SC_OK, status);
133         assertEquals("/http-in", request.getResponseBodyAsString());
134     }
135 
136     @Override
137     protected int getNumPortsToFind()
138     {
139         return 2;
140     }
141 }
142 
143