View Javadoc

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