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