View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.http;
8   
9   import static junit.framework.Assert.assertEquals;
10  
11  import org.mule.tck.junit4.AbstractMuleContextTestCase;
12  import org.mule.tck.junit4.rule.DynamicPort;
13  import org.mule.transport.tcp.TcpConnector;
14  
15  import java.net.ServerSocket;
16  import java.net.Socket;
17  
18  import org.junit.Rule;
19  import org.junit.Test;
20  
21  /**
22   * Test class for the {@link HttpServerConnection}.
23   */
24  public class HttpServerConnectionTestCase extends AbstractMuleContextTestCase
25  {
26      @Rule
27      public DynamicPort port1 = new DynamicPort("port1");
28  
29      private final static boolean SEND_TCP_NO_DELAY = false;
30      private final static boolean KEEP_ALIVE = true;
31      private final static int RECEIVE_BUFFER_SIZE = 1024;
32      private final static int SERVER_SO_TIMEOUT = 5000;
33  
34      @Test
35      public void testCorrectHttpConnectorPropertiesPropagation() throws Exception
36      {
37          // Build http connector and initialise it.
38          HttpConnector httpConnector = new HttpConnector(muleContext);
39          httpConnector.setSendTcpNoDelay(SEND_TCP_NO_DELAY);
40          httpConnector.setKeepAlive(KEEP_ALIVE);
41          httpConnector.setReceiveBufferSize(RECEIVE_BUFFER_SIZE);
42          httpConnector.setServerSoTimeout(SERVER_SO_TIMEOUT);
43          httpConnector.initialise();
44  
45          ServerSocket serverSocket = null;
46          Socket clientServerSocket = null;
47          Socket serverClientSocket = null;
48          try
49          {
50              // Establish server and client connections.
51              serverSocket = httpConnector.getServerSocketFactory().createServerSocket(port1.getNumber(), TcpConnector.DEFAULT_BACKLOG, true);
52              clientServerSocket = new Socket("localhost", port1.getNumber());
53              serverClientSocket = serverSocket.accept();
54  
55              // Build HTTP server connection.
56              HttpServerConnection conn = new HttpServerConnection(serverClientSocket, muleContext.getConfiguration().getDefaultEncoding(), httpConnector);
57  
58              // Assert that properties were propagated correctly from the connector.
59              assertEquals(SEND_TCP_NO_DELAY, conn.isSocketTcpNoDelay());
60              assertEquals(KEEP_ALIVE, conn.isSocketKeepAlive());
61              assertEquals(RECEIVE_BUFFER_SIZE, conn.getSocketReceiveBufferSize());
62              assertEquals(SERVER_SO_TIMEOUT, conn.getSocketTimeout());
63          }
64          finally
65          {
66              // Close connections.
67              if (clientServerSocket != null)
68              {
69                  clientServerSocket.close();
70              }
71              if (serverClientSocket != null)
72              {
73                  serverClientSocket.close();
74              }
75              if (serverSocket != null)
76              {
77                  serverSocket.close();
78              }
79          }
80      }
81  
82  }