View Javadoc

1   /*
2    * $Id: MuleHostConfigurationTestCase.java 22387 2011-07-12 03:53:36Z dirk.olmes $
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;
12  
13  import org.mule.tck.junit4.AbstractMuleTestCase;
14  
15  import java.io.IOException;
16  import java.net.InetAddress;
17  import java.net.Socket;
18  import java.net.UnknownHostException;
19  
20  import org.apache.commons.httpclient.ConnectTimeoutException;
21  import org.apache.commons.httpclient.HostConfiguration;
22  import org.apache.commons.httpclient.HttpHost;
23  import org.apache.commons.httpclient.URI;
24  import org.apache.commons.httpclient.params.HttpConnectionParams;
25  import org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory;
26  import org.apache.commons.httpclient.protocol.Protocol;
27  import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
28  import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
29  import org.junit.Test;
30  
31  import static org.junit.Assert.assertEquals;
32  import static org.junit.Assert.assertTrue;
33  
34  public class MuleHostConfigurationTestCase extends AbstractMuleTestCase
35  {
36      
37      private static final String HTTPX = "httpx";
38      
39      @Test
40      public void testSetHostViaUri() throws Exception
41      {
42          HostConfiguration hostConfig = createHostConfiguration();
43          
44          URI uri = new URI("http://www.mulesoft.org:8080", false);
45          hostConfig.setHost(uri);
46          
47          assertMockSocketFactory(hostConfig);
48          assertEquals("www.mulesoft.org", hostConfig.getHost());
49          assertEquals(8080, hostConfig.getPort());
50      }
51  
52      @Test
53      public void testSetHostViaUriWithDifferentProtocol() throws Exception
54      {
55          new DifferentProtocolTemplate()
56          {
57              protected void doTest() throws Exception
58              {
59                  HostConfiguration hostConfig = createHostConfiguration();
60                  
61                  URI uri = new URI("httpx://www.mulesoft.org:8080", false);
62                  hostConfig.setHost(uri);
63                  
64                  assertTrue(hostConfig.getProtocol().getSocketFactory() instanceof DefaultProtocolSocketFactory);
65                  assertEquals("www.mulesoft.org", hostConfig.getHost());
66                  assertEquals(8080, hostConfig.getPort());
67              }
68          }.test();
69      }
70  
71      @Test
72      public void testSetHostViaHttpHost()
73      {
74          HostConfiguration hostConfig = createHostConfiguration();
75          
76          HttpHost host = new HttpHost("www.mulesoft.org", 8080);
77          hostConfig.setHost(host);
78          
79          assertMockSocketFactory(hostConfig);
80          assertEquals("www.mulesoft.org", hostConfig.getHost());
81          assertEquals(8080, hostConfig.getPort());
82      }
83  
84      @Test
85      public void testSetHostViaHostAndPortAndProtocolName()
86      {
87          HostConfiguration hostConfig = createHostConfiguration();
88          
89          hostConfig.setHost("www.mulesoft.org", 8080, "http");
90          
91          assertMockSocketFactory(hostConfig);
92          assertEquals("www.mulesoft.org", hostConfig.getHost());
93          assertEquals(8080, hostConfig.getPort());
94      }
95  
96      @Test
97      public void testSetHostViaHostAndPortAndProtocolNameWithDifferentProtocol() throws Exception
98      {
99          new DifferentProtocolTemplate()
100         {
101             protected void doTest() throws Exception
102             {
103                 HostConfiguration hostConfig = createHostConfiguration();
104                 
105                 hostConfig.setHost("www.mulesoft.org", 8080, "httpx");
106                 
107                 assertDefaultSocketFactory(hostConfig);
108                 assertEquals("www.mulesoft.org", hostConfig.getHost());
109                 assertEquals(8080, hostConfig.getPort());
110             }
111         }.test();
112     }
113 
114     @SuppressWarnings("deprecation")
115     @Test
116     public void testSetHostViaHostAndVirtualHostAndPortAndProtocol()
117     {
118         HostConfiguration hostConfig = createHostConfiguration();
119 
120         Protocol protocol = Protocol.getProtocol("http");
121         hostConfig.setHost("www.mulesoft.org", "www.mulesoft.com", 8080, protocol);
122         
123         assertMockSocketFactory(hostConfig);
124         assertEquals("www.mulesoft.org", hostConfig.getHost());
125         assertEquals(8080, hostConfig.getPort());
126         assertEquals("www.mulesoft.com", hostConfig.getVirtualHost());
127     }
128 
129     @SuppressWarnings("deprecation")
130     @Test
131     public void testSetHostViaHostAndVirtualHostAndPortAndProtocolWithDifferentProtocol() throws Exception
132     {
133         new DifferentProtocolTemplate()
134         {
135             protected void doTest() throws Exception
136             {
137                 HostConfiguration hostConfig = createHostConfiguration();
138 
139                 Protocol protocol = Protocol.getProtocol("httpx");
140                 hostConfig.setHost("www.mulesoft.org", "www.mulesoft.com", 8080, protocol);
141                 
142                 assertDefaultSocketFactory(hostConfig);
143                 assertEquals("www.mulesoft.org", hostConfig.getHost());
144                 assertEquals(8080, hostConfig.getPort());
145                 assertEquals("www.mulesoft.com", hostConfig.getVirtualHost());
146             }
147         }.test();
148     }
149 
150     @Test
151     public void testSetHostViaHostAndPort()
152     {
153         HostConfiguration hostConfig = createHostConfiguration();
154 
155         hostConfig.setHost("www.mulesoft.org", 8080);
156 
157         assertMockSocketFactory(hostConfig);
158         assertEquals("www.mulesoft.org", hostConfig.getHost());
159         assertEquals(8080, hostConfig.getPort());
160     }
161 
162     @Test
163     public void testSetHostViaHost()
164     {
165         HostConfiguration hostConfig = createHostConfiguration();
166         
167         hostConfig.setHost("www.mulesoft.org");
168         
169         assertEquals("www.mulesoft.org", hostConfig.getHost());
170         assertMockSocketFactory(hostConfig);
171     }
172 
173     @Test
174     public void testClone()
175     {
176         HostConfiguration hostConfig = createHostConfiguration();
177         HostConfiguration clone = (HostConfiguration) hostConfig.clone();
178         assertMockSocketFactory(clone);
179     }
180     
181     private MuleHostConfiguration createHostConfiguration()
182     {
183         MuleHostConfiguration hostConfig = new MuleHostConfiguration();
184         ProtocolSocketFactory socketFactory = new MockSecureProtocolFactory();
185         Protocol protocol = new Protocol("http", socketFactory, 80);
186         hostConfig.setHost("localhost", 80, protocol);
187         
188         // since we're using a setHost variant here, too let's assert that it actually worked
189         assertMockSocketFactory(hostConfig);
190         
191         return hostConfig;
192     }
193     
194     private void assertMockSocketFactory(HostConfiguration hostConfig)
195     {
196         assertTrue(hostConfig.getProtocol().getSocketFactory() instanceof MockSecureProtocolFactory);
197     }
198     
199     private void assertDefaultSocketFactory(HostConfiguration hostConfig)
200     {
201         assertTrue(hostConfig.getProtocol().getSocketFactory() instanceof DefaultProtocolSocketFactory);
202     }
203     
204     private static abstract class DifferentProtocolTemplate
205     {
206         public DifferentProtocolTemplate()
207         {
208             super();
209         }
210         
211         @Test
212     public void test() throws Exception
213         {
214             try
215             {
216                 Protocol httpxProtocol = new Protocol(HTTPX, new DefaultProtocolSocketFactory(), 81);
217                 Protocol.registerProtocol(HTTPX, httpxProtocol);
218                 
219                 doTest();
220             }
221             finally
222             {
223                 Protocol.unregisterProtocol(HTTPX);
224             }
225         }
226         
227         protected abstract void doTest() throws Exception;
228     }
229 
230     private static class MockSecureProtocolFactory implements SecureProtocolSocketFactory
231     {
232         public MockSecureProtocolFactory()
233         {
234             super();
235         }
236         
237         public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
238             throws IOException, UnknownHostException
239         {
240             throw new UnsupportedOperationException();
241         }
242 
243         public Socket createSocket(String host, int port) throws IOException, UnknownHostException
244         {
245             throw new UnsupportedOperationException();
246         }
247 
248         public Socket createSocket(String host, int port, InetAddress localAddress, int localPort)
249             throws IOException, UnknownHostException
250         {
251             throw new UnsupportedOperationException();
252         }
253 
254         public Socket createSocket(String host, int port, InetAddress localAddress, int localPort,
255             HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException
256         {
257             throw new UnsupportedOperationException();
258         }
259     }
260     
261 }
262 
263