HttpClientFactory { public static HttpClient create(CRestConfig crestConfig, Class<?> source) { HttpClient httpClient = crestConfig.get(source.getName() + HTTP_CLIENT); if(httpClient != null){ return httpClient; } int concurrencyLevel = crestConfig.getConcurrencyLevel(); if (concurrencyLevel > 1) { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(concurrencyLevel)); ConnManagerParams.setMaxTotalConnections(params, concurrencyLevel); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), HTTP_PORT)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), HTTPS_PORT)); ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry); httpClient = new DefaultHttpClient(cm, params); } else { httpClient = new DefaultHttpClient(); } ((DefaultHttpClient) httpClient).setRoutePlanner(new ProxySelectorRoutePlanner(httpClient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault())); return httpClient; } private  HttpClientFactory(); static HttpClient create(CRestConfig crestConfig, Class<?> source);  }
@Test public void createWithOneShouldCreateDefaultHttpClient() throws Exception { DefaultHttpClient expected = mock(DefaultHttpClient.class); ProxySelectorRoutePlanner planner = mock(ProxySelectorRoutePlanner.class); ClientConnectionManager clientConnectionManager = mock(ClientConnectionManager.class); SchemeRegistry schemeRegistry = mock(SchemeRegistry.class); ProxySelector proxySelector = mock(ProxySelector.class); when(expected.getConnectionManager()).thenReturn(clientConnectionManager); when(clientConnectionManager.getSchemeRegistry()).thenReturn(schemeRegistry); mockStatic(ProxySelector.class); when(ProxySelector.getDefault()).thenReturn(proxySelector); whenNew(DefaultHttpClient.class).withNoArguments().thenReturn(expected); whenNew(ProxySelectorRoutePlanner.class) .withArguments(schemeRegistry, proxySelector).thenReturn(planner); HttpClient actual = HttpClientFactory.create(crestConfig, getClass()); assertSame(expected, actual); verify(expected).setRoutePlanner(planner); }
@Test public void createUserSpecificHttpClientShouldReturnIt() throws Exception { HttpClient expected = mock(HttpClient.class); when(crestConfig.get(getClass().getName() + HttpClientFactory.HTTP_CLIENT)).thenReturn(expected); assertSame(expected, HttpClientFactory.create(crestConfig, getClass())); }
@Test public void createWithMoreThanOneShouldCreateDefaultHttpClientWithConnectionManagerSetup() throws Exception { DefaultHttpClient expected = mock(DefaultHttpClient.class); ProxySelectorRoutePlanner planner = mock(ProxySelectorRoutePlanner.class); ClientConnectionManager clientConnectionManager = mock(ClientConnectionManager.class); SchemeRegistry schemeRegistry = new SchemeRegistry(); ProxySelector proxySelector = mock(ProxySelector.class); BasicHttpParams httpParams = mock(BasicHttpParams.class); ConnPerRouteBean routeBean = mock(ConnPerRouteBean.class); PlainSocketFactory plainSocketFactory = mock(PlainSocketFactory.class); SSLSocketFactory sslSocketFactory = mock(SSLSocketFactory.class); Scheme plainScheme = new Scheme("http", plainSocketFactory, 80); Scheme sslScheme = new Scheme("https", sslSocketFactory, 443); ThreadSafeClientConnManager threadSafeClientConnManager = mock(ThreadSafeClientConnManager.class); when(expected.getConnectionManager()).thenReturn(clientConnectionManager); when(clientConnectionManager.getSchemeRegistry()).thenReturn(schemeRegistry); mockStatic(ProxySelector.class); when(ProxySelector.getDefault()).thenReturn(proxySelector); mockStatic(PlainSocketFactory.class); when(PlainSocketFactory.getSocketFactory()).thenReturn(plainSocketFactory); mockStatic(SSLSocketFactory.class); when(SSLSocketFactory.getSocketFactory()).thenReturn(sslSocketFactory); whenNew(SchemeRegistry.class).withNoArguments().thenReturn(schemeRegistry); whenNew(Scheme.class).withArguments("http", plainSocketFactory, 80).thenReturn(plainScheme); whenNew(Scheme.class).withArguments("https", sslSocketFactory, 443).thenReturn(sslScheme); whenNew(ThreadSafeClientConnManager.class).withArguments(httpParams, schemeRegistry).thenReturn(threadSafeClientConnManager); whenNew(ConnPerRouteBean.class).withArguments(2).thenReturn(routeBean); whenNew(BasicHttpParams.class).withNoArguments().thenReturn(httpParams); whenNew(DefaultHttpClient.class).withArguments(threadSafeClientConnManager, httpParams).thenReturn(expected); whenNew(ProxySelectorRoutePlanner.class) .withArguments(schemeRegistry, proxySelector).thenReturn(planner); when(crestConfig.getConcurrencyLevel()).thenReturn(2); HttpClient actual = HttpClientFactory.create(crestConfig, getClass()); assertSame(expected, actual); verify(expected).setRoutePlanner(planner); verify(httpParams).setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); verify(httpParams).setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, routeBean); verify(httpParams).setIntParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 2); assertSame(plainScheme, schemeRegistry.getScheme("http")); assertSame(sslScheme, schemeRegistry.getScheme("https")); }