Ryan Baxter
2 years ago
21 changed files with 16 additions and 1214 deletions
@ -1,55 +0,0 @@
@@ -1,55 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2020 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.commons.httpclient; |
||||
|
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
import org.apache.http.config.RegistryBuilder; |
||||
import org.apache.http.conn.HttpClientConnectionManager; |
||||
|
||||
/** |
||||
* Interface for creating an {@link HttpClientConnectionManager}. |
||||
* |
||||
* @author Ryan Baxter |
||||
*/ |
||||
public interface ApacheHttpClientConnectionManagerFactory { |
||||
|
||||
/** |
||||
* Scheme for HTTP based communication. |
||||
*/ |
||||
String HTTP_SCHEME = "http"; |
||||
|
||||
/** |
||||
* Scheme for HTTPS based communication. |
||||
*/ |
||||
String HTTPS_SCHEME = "https"; |
||||
|
||||
/** |
||||
* Creates a new {@link HttpClientConnectionManager}. |
||||
* @param disableSslValidation If true, SSL validation will be disabled. |
||||
* @param maxTotalConnections The total number of connections. |
||||
* @param maxConnectionsPerRoute The total number of connections per route. |
||||
* @param timeToLive The time a connection is allowed to exist. |
||||
* @param timeUnit The time unit for the time-to-live value. |
||||
* @param registryBuilder The {@link RegistryBuilder} to use in the connection |
||||
* manager. |
||||
* @return A new {@link HttpClientConnectionManager}. |
||||
*/ |
||||
HttpClientConnectionManager newConnectionManager(boolean disableSslValidation, int maxTotalConnections, |
||||
int maxConnectionsPerRoute, long timeToLive, TimeUnit timeUnit, RegistryBuilder registryBuilder); |
||||
|
||||
} |
@ -1,36 +0,0 @@
@@ -1,36 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2020 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.commons.httpclient; |
||||
|
||||
import org.apache.http.impl.client.CloseableHttpClient; |
||||
import org.apache.http.impl.client.HttpClientBuilder; |
||||
|
||||
/** |
||||
* Factory for creating a new {@link CloseableHttpClient}. |
||||
* |
||||
* @author Ryan Baxter |
||||
*/ |
||||
public interface ApacheHttpClientFactory { |
||||
|
||||
/** |
||||
* Creates an {@link HttpClientBuilder} that can be used to create a new |
||||
* {@link CloseableHttpClient}. |
||||
* @return A {@link HttpClientBuilder}. |
||||
*/ |
||||
HttpClientBuilder createBuilder(); |
||||
|
||||
} |
@ -1,104 +0,0 @@
@@ -1,104 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2020 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.commons.httpclient; |
||||
|
||||
import java.security.KeyManagementException; |
||||
import java.security.NoSuchAlgorithmException; |
||||
import java.security.SecureRandom; |
||||
import java.security.cert.X509Certificate; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
import javax.net.ssl.SSLContext; |
||||
import javax.net.ssl.TrustManager; |
||||
import javax.net.ssl.X509TrustManager; |
||||
|
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
import org.apache.http.config.Registry; |
||||
import org.apache.http.config.RegistryBuilder; |
||||
import org.apache.http.conn.HttpClientConnectionManager; |
||||
import org.apache.http.conn.socket.ConnectionSocketFactory; |
||||
import org.apache.http.conn.socket.PlainConnectionSocketFactory; |
||||
import org.apache.http.conn.ssl.NoopHostnameVerifier; |
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; |
||||
|
||||
/** |
||||
* Default implementation of {@link ApacheHttpClientConnectionManagerFactory}. |
||||
* |
||||
* @author Ryan Baxter |
||||
* @author Michael Wirth |
||||
*/ |
||||
public class DefaultApacheHttpClientConnectionManagerFactory implements ApacheHttpClientConnectionManagerFactory { |
||||
|
||||
private static final Log LOG = LogFactory.getLog(DefaultApacheHttpClientConnectionManagerFactory.class); |
||||
|
||||
public HttpClientConnectionManager newConnectionManager(boolean disableSslValidation, int maxTotalConnections, |
||||
int maxConnectionsPerRoute) { |
||||
return newConnectionManager(disableSslValidation, maxTotalConnections, maxConnectionsPerRoute, -1, |
||||
TimeUnit.MILLISECONDS, null); |
||||
} |
||||
|
||||
@Override |
||||
public HttpClientConnectionManager newConnectionManager(boolean disableSslValidation, int maxTotalConnections, |
||||
int maxConnectionsPerRoute, long timeToLive, TimeUnit timeUnit, RegistryBuilder registryBuilder) { |
||||
if (registryBuilder == null) { |
||||
registryBuilder = RegistryBuilder.<ConnectionSocketFactory>create().register(HTTP_SCHEME, |
||||
PlainConnectionSocketFactory.INSTANCE); |
||||
} |
||||
if (disableSslValidation) { |
||||
try { |
||||
final SSLContext sslContext = SSLContext.getInstance("SSL"); |
||||
sslContext.init(null, new TrustManager[] { new DisabledValidationTrustManager() }, new SecureRandom()); |
||||
registryBuilder.register(HTTPS_SCHEME, |
||||
new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE)); |
||||
} |
||||
catch (NoSuchAlgorithmException | KeyManagementException e) { |
||||
LOG.warn("Error creating SSLContext", e); |
||||
} |
||||
} |
||||
else { |
||||
registryBuilder.register("https", SSLConnectionSocketFactory.getSocketFactory()); |
||||
} |
||||
final Registry<ConnectionSocketFactory> registry = registryBuilder.build(); |
||||
|
||||
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry, null, |
||||
null, null, timeToLive, timeUnit); |
||||
connectionManager.setMaxTotal(maxTotalConnections); |
||||
connectionManager.setDefaultMaxPerRoute(maxConnectionsPerRoute); |
||||
|
||||
return connectionManager; |
||||
} |
||||
|
||||
static class DisabledValidationTrustManager implements X509TrustManager { |
||||
|
||||
@Override |
||||
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) { |
||||
} |
||||
|
||||
@Override |
||||
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) { |
||||
} |
||||
|
||||
@Override |
||||
public X509Certificate[] getAcceptedIssuers() { |
||||
return null; |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
@ -1,44 +0,0 @@
@@ -1,44 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2020 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.commons.httpclient; |
||||
|
||||
import org.apache.http.impl.client.HttpClientBuilder; |
||||
|
||||
/** |
||||
* Default implementation of {@link ApacheHttpClientFactory}. |
||||
* |
||||
* @author Ryan Baxter |
||||
*/ |
||||
public class DefaultApacheHttpClientFactory implements ApacheHttpClientFactory { |
||||
|
||||
private HttpClientBuilder builder; |
||||
|
||||
public DefaultApacheHttpClientFactory(HttpClientBuilder builder) { |
||||
this.builder = builder; |
||||
} |
||||
|
||||
/** |
||||
* A default {@link HttpClientBuilder}. The {@link HttpClientBuilder} returned will |
||||
* have content compression disabled, have cookie management disabled, and use system |
||||
* properties. |
||||
*/ |
||||
@Override |
||||
public HttpClientBuilder createBuilder() { |
||||
return this.builder.disableContentCompression().disableCookieManagement().useSystemProperties(); |
||||
} |
||||
|
||||
} |
@ -1,35 +0,0 @@
@@ -1,35 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2020 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.commons.httpclient; |
||||
|
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
import okhttp3.ConnectionPool; |
||||
|
||||
/** |
||||
* Default implementation of {@link OkHttpClientConnectionPoolFactory}. |
||||
* |
||||
* @author Ryan Baxter |
||||
*/ |
||||
public class DefaultOkHttpClientConnectionPoolFactory implements OkHttpClientConnectionPoolFactory { |
||||
|
||||
@Override |
||||
public ConnectionPool create(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit) { |
||||
return new ConnectionPool(maxIdleConnections, keepAliveDuration, timeUnit); |
||||
} |
||||
|
||||
} |
@ -1,66 +0,0 @@
@@ -1,66 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2020 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.commons.httpclient; |
||||
|
||||
import java.security.KeyManagementException; |
||||
import java.security.NoSuchAlgorithmException; |
||||
|
||||
import javax.net.ssl.SSLContext; |
||||
import javax.net.ssl.SSLSocketFactory; |
||||
import javax.net.ssl.TrustManager; |
||||
import javax.net.ssl.X509TrustManager; |
||||
|
||||
import okhttp3.OkHttpClient; |
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
|
||||
/** |
||||
* Default implementation of {@link OkHttpClientFactory}. |
||||
* |
||||
* @author Ryan Baxter |
||||
*/ |
||||
public class DefaultOkHttpClientFactory implements OkHttpClientFactory { |
||||
|
||||
private static final Log LOG = LogFactory.getLog(DefaultOkHttpClientFactory.class); |
||||
|
||||
private OkHttpClient.Builder builder; |
||||
|
||||
public DefaultOkHttpClientFactory(OkHttpClient.Builder builder) { |
||||
this.builder = builder; |
||||
} |
||||
|
||||
@Override |
||||
public OkHttpClient.Builder createBuilder(boolean disableSslValidation) { |
||||
if (disableSslValidation) { |
||||
try { |
||||
X509TrustManager disabledTrustManager = new DisableValidationTrustManager(); |
||||
TrustManager[] trustManagers = new TrustManager[1]; |
||||
trustManagers[0] = disabledTrustManager; |
||||
SSLContext sslContext = SSLContext.getInstance("SSL"); |
||||
sslContext.init(null, trustManagers, new java.security.SecureRandom()); |
||||
SSLSocketFactory disabledSSLSocketFactory = sslContext.getSocketFactory(); |
||||
this.builder.sslSocketFactory(disabledSSLSocketFactory, disabledTrustManager); |
||||
this.builder.hostnameVerifier(new TrustAllHostnames()); |
||||
} |
||||
catch (NoSuchAlgorithmException | KeyManagementException e) { |
||||
LOG.warn("Error setting SSLSocketFactory in OKHttpClient", e); |
||||
} |
||||
} |
||||
return this.builder; |
||||
} |
||||
|
||||
} |
@ -1,85 +0,0 @@
@@ -1,85 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2020 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.commons.httpclient; |
||||
|
||||
import okhttp3.OkHttpClient; |
||||
import org.apache.http.client.HttpClient; |
||||
import org.apache.http.impl.client.HttpClientBuilder; |
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
/** |
||||
* @author Ryan Baxter |
||||
*/ |
||||
@Configuration(proxyBeanMethods = false) |
||||
public class HttpClientConfiguration { |
||||
|
||||
@Configuration(proxyBeanMethods = false) |
||||
@ConditionalOnProperty(name = "spring.cloud.httpclientfactories.apache.enabled", matchIfMissing = true) |
||||
@ConditionalOnClass(HttpClient.class) |
||||
static class ApacheHttpClientConfiguration { |
||||
|
||||
@Bean |
||||
@ConditionalOnMissingBean |
||||
public ApacheHttpClientConnectionManagerFactory connManFactory() { |
||||
return new DefaultApacheHttpClientConnectionManagerFactory(); |
||||
} |
||||
|
||||
@Bean |
||||
@ConditionalOnMissingBean |
||||
public HttpClientBuilder apacheHttpClientBuilder() { |
||||
return HttpClientBuilder.create(); |
||||
} |
||||
|
||||
@Bean |
||||
@ConditionalOnMissingBean |
||||
public ApacheHttpClientFactory apacheHttpClientFactory(HttpClientBuilder builder) { |
||||
return new DefaultApacheHttpClientFactory(builder); |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration(proxyBeanMethods = false) |
||||
@ConditionalOnProperty(name = "spring.cloud.httpclientfactories.ok.enabled", matchIfMissing = true) |
||||
@ConditionalOnClass(OkHttpClient.class) |
||||
static class OkHttpClientConfiguration { |
||||
|
||||
@Bean |
||||
@ConditionalOnMissingBean |
||||
public OkHttpClientConnectionPoolFactory connPoolFactory() { |
||||
return new DefaultOkHttpClientConnectionPoolFactory(); |
||||
} |
||||
|
||||
@Bean |
||||
@ConditionalOnMissingBean |
||||
public OkHttpClient.Builder okHttpClientBuilder() { |
||||
return new OkHttpClient.Builder(); |
||||
} |
||||
|
||||
@Bean |
||||
@ConditionalOnMissingBean |
||||
public OkHttpClientFactory okHttpClientFactory(OkHttpClient.Builder builder) { |
||||
return new DefaultOkHttpClientFactory(builder); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
@ -1,39 +0,0 @@
@@ -1,39 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2020 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.commons.httpclient; |
||||
|
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
import okhttp3.ConnectionPool; |
||||
|
||||
/** |
||||
* Creates {@link ConnectionPool}s for {@link okhttp3.OkHttpClient}s. |
||||
* |
||||
* @author Ryan Baxter |
||||
*/ |
||||
public interface OkHttpClientConnectionPoolFactory { |
||||
|
||||
/** |
||||
* Creates a new {@link ConnectionPool}. |
||||
* @param maxIdleConnections Number of max idle connections to allow. |
||||
* @param keepAliveDuration Amount of time to keep connections alive. |
||||
* @param timeUnit The time unit for the keep-alive duration. |
||||
* @return A new {@link ConnectionPool}. |
||||
*/ |
||||
ConnectionPool create(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit); |
||||
|
||||
} |
@ -1,73 +0,0 @@
@@ -1,73 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2020 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.commons.httpclient; |
||||
|
||||
import java.security.cert.X509Certificate; |
||||
|
||||
import javax.net.ssl.HostnameVerifier; |
||||
import javax.net.ssl.SSLSession; |
||||
import javax.net.ssl.X509TrustManager; |
||||
|
||||
import okhttp3.OkHttpClient; |
||||
|
||||
/** |
||||
* Creates new {@link OkHttpClient}s. |
||||
* |
||||
* @author Ryan Baxter |
||||
*/ |
||||
public interface OkHttpClientFactory { |
||||
|
||||
/** |
||||
* Creates a {@link OkHttpClient.Builder} used to build an {@link OkHttpClient}. |
||||
* @param disableSslValidation Disables SSL validation |
||||
* @return A new {@link OkHttpClient.Builder} |
||||
*/ |
||||
OkHttpClient.Builder createBuilder(boolean disableSslValidation); |
||||
|
||||
/** |
||||
* A {@link X509TrustManager} that does not validate SSL certificates. |
||||
*/ |
||||
class DisableValidationTrustManager implements X509TrustManager { |
||||
|
||||
@Override |
||||
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) { |
||||
} |
||||
|
||||
@Override |
||||
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) { |
||||
} |
||||
|
||||
@Override |
||||
public X509Certificate[] getAcceptedIssuers() { |
||||
return new X509Certificate[0]; |
||||
} |
||||
|
||||
} |
||||
|
||||
/** |
||||
* A {@link HostnameVerifier} that does not validate any hostnames. |
||||
*/ |
||||
class TrustAllHostnames implements HostnameVerifier { |
||||
|
||||
@Override |
||||
public boolean verify(String s, SSLSession sslSession) { |
||||
return true; |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
@ -1,72 +0,0 @@
@@ -1,72 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2020 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.commons.httpclient; |
||||
|
||||
import org.apache.http.impl.client.HttpClientBuilder; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.SpringApplication; |
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore; |
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
import static org.assertj.core.api.BDDAssertions.then; |
||||
|
||||
/** |
||||
* @author Ryan Baxter |
||||
*/ |
||||
@SpringBootTest(classes = CustomHttpClientBuilderApplication.class) |
||||
public class CustomHttpClientBuilderConfigurationTests { |
||||
|
||||
@Autowired |
||||
ApacheHttpClientFactory apacheHttpClientFactory; |
||||
|
||||
@Test |
||||
public void testCustomBuilder() { |
||||
HttpClientBuilder builder = this.apacheHttpClientFactory.createBuilder(); |
||||
then(CustomHttpClientBuilderApplication.MyHttpClientBuilder.class.isInstance(builder)).isTrue(); |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration(proxyBeanMethods = false) |
||||
@EnableAutoConfiguration |
||||
class CustomHttpClientBuilderApplication { |
||||
|
||||
public static void main(String[] args) { |
||||
SpringApplication.run(MyApplication.class, args); |
||||
} |
||||
|
||||
@Configuration(proxyBeanMethods = false) |
||||
@AutoConfigureBefore(HttpClientConfiguration.class) |
||||
static class MyConfig { |
||||
|
||||
@Bean |
||||
public MyHttpClientBuilder builder() { |
||||
return new MyHttpClientBuilder(); |
||||
} |
||||
|
||||
} |
||||
|
||||
static class MyHttpClientBuilder extends HttpClientBuilder { |
||||
|
||||
} |
||||
|
||||
} |
@ -1,153 +0,0 @@
@@ -1,153 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2020 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.commons.httpclient; |
||||
|
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
import okhttp3.ConnectionPool; |
||||
import okhttp3.OkHttpClient; |
||||
import org.apache.http.config.RegistryBuilder; |
||||
import org.apache.http.conn.HttpClientConnectionManager; |
||||
import org.apache.http.impl.client.HttpClientBuilder; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.SpringApplication; |
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
import static org.assertj.core.api.BDDAssertions.then; |
||||
|
||||
/** |
||||
* @author Ryan Baxter |
||||
*/ |
||||
@SpringBootTest(classes = CustomApplication.class, properties = { "spring.cloud.httpclient.ok.enabled: true" }) |
||||
public class CustomHttpClientConfigurationTests { |
||||
|
||||
@Autowired |
||||
ApacheHttpClientFactory httpClientFactory; |
||||
|
||||
@Autowired |
||||
ApacheHttpClientConnectionManagerFactory connectionManagerFactory; |
||||
|
||||
@Autowired |
||||
OkHttpClientFactory okHttpClientFactory; |
||||
|
||||
@Autowired |
||||
OkHttpClientConnectionPoolFactory okHttpClientConnectionPoolFactory; |
||||
|
||||
@Test |
||||
public void connManFactory() { |
||||
then(ApacheHttpClientConnectionManagerFactory.class.isInstance(this.connectionManagerFactory)).isTrue(); |
||||
then(CustomApplication.MyApacheHttpClientConnectionManagerFactory.class |
||||
.isInstance(this.connectionManagerFactory)).isTrue(); |
||||
} |
||||
|
||||
@Test |
||||
public void apacheHttpClientFactory() { |
||||
then(ApacheHttpClientFactory.class.isInstance(this.httpClientFactory)).isTrue(); |
||||
then(CustomApplication.MyApacheHttpClientFactory.class.isInstance(this.httpClientFactory)).isTrue(); |
||||
} |
||||
|
||||
@Test |
||||
public void connectionPoolFactory() { |
||||
then(OkHttpClientConnectionPoolFactory.class.isInstance(this.okHttpClientConnectionPoolFactory)).isTrue(); |
||||
then(CustomApplication.MyOkHttpConnectionPoolFactory.class.isInstance(this.okHttpClientConnectionPoolFactory)) |
||||
.isTrue(); |
||||
} |
||||
|
||||
@Test |
||||
public void okHttpClientFactory() { |
||||
then(OkHttpClientFactory.class.isInstance(this.okHttpClientFactory)).isTrue(); |
||||
then(CustomApplication.MyOkHttpClientFactory.class.isInstance(this.okHttpClientFactory)).isTrue(); |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration(proxyBeanMethods = false) |
||||
@EnableAutoConfiguration |
||||
class CustomApplication { |
||||
|
||||
public static void main(String[] args) { |
||||
SpringApplication.run(MyApplication.class, args); |
||||
} |
||||
|
||||
@Configuration(proxyBeanMethods = false) |
||||
static class MyConfig { |
||||
|
||||
@Bean |
||||
public ApacheHttpClientFactory clientFactory() { |
||||
return new MyApacheHttpClientFactory(); |
||||
} |
||||
|
||||
@Bean |
||||
public ApacheHttpClientConnectionManagerFactory connectionManagerFactory() { |
||||
return new MyApacheHttpClientConnectionManagerFactory(); |
||||
} |
||||
|
||||
@Bean |
||||
public OkHttpClientConnectionPoolFactory connectionPoolFactory() { |
||||
return new MyOkHttpConnectionPoolFactory(); |
||||
} |
||||
|
||||
@Bean |
||||
public OkHttpClientFactory okHttpClientFactory() { |
||||
return new MyOkHttpClientFactory(); |
||||
} |
||||
|
||||
} |
||||
|
||||
static class MyApacheHttpClientFactory implements ApacheHttpClientFactory { |
||||
|
||||
@Override |
||||
public HttpClientBuilder createBuilder() { |
||||
return HttpClientBuilder.create(); |
||||
} |
||||
|
||||
} |
||||
|
||||
static class MyApacheHttpClientConnectionManagerFactory implements ApacheHttpClientConnectionManagerFactory { |
||||
|
||||
@Override |
||||
public HttpClientConnectionManager newConnectionManager(boolean disableSslValidation, int maxTotalConnections, |
||||
int maxConnectionsPerRoute, long timeToLive, TimeUnit timeUnit, RegistryBuilder registryBuilder) { |
||||
return null; |
||||
} |
||||
|
||||
} |
||||
|
||||
static class MyOkHttpClientFactory implements OkHttpClientFactory { |
||||
|
||||
@Override |
||||
public OkHttpClient.Builder createBuilder(boolean disableSslValidation) { |
||||
return new OkHttpClient.Builder(); |
||||
} |
||||
|
||||
} |
||||
|
||||
static class MyOkHttpConnectionPoolFactory implements OkHttpClientConnectionPoolFactory { |
||||
|
||||
@Override |
||||
public ConnectionPool create(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit) { |
||||
return null; |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
@ -1,78 +0,0 @@
@@ -1,78 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2020 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.commons.httpclient; |
||||
|
||||
import java.lang.reflect.Field; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
import okhttp3.OkHttpClient; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.SpringApplication; |
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.util.ReflectionUtils; |
||||
|
||||
import static org.assertj.core.api.BDDAssertions.then; |
||||
|
||||
/** |
||||
* @author Ryan Baxter |
||||
*/ |
||||
@SpringBootTest(classes = CustomOkHttpClientBuilderApplication.class) |
||||
public class CustomOkHttpClientBuilderConfigurationTests { |
||||
|
||||
@Autowired |
||||
private OkHttpClientFactory okHttpClientFactory; |
||||
|
||||
@Test |
||||
public void testCustomBuilder() { |
||||
OkHttpClient.Builder builder = this.okHttpClientFactory.createBuilder(false); |
||||
Integer timeout = getField(builder, "connectTimeout"); |
||||
then(timeout.intValue()).isEqualTo(1); |
||||
} |
||||
|
||||
protected <T> T getField(Object target, String name) { |
||||
Field field = ReflectionUtils.findField(target.getClass(), name); |
||||
ReflectionUtils.makeAccessible(field); |
||||
Object value = ReflectionUtils.getField(field, target); |
||||
return (T) value; |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration(proxyBeanMethods = false) |
||||
@EnableAutoConfiguration |
||||
class CustomOkHttpClientBuilderApplication { |
||||
|
||||
public static void main(String[] args) { |
||||
SpringApplication.run(MyApplication.class, args); |
||||
} |
||||
|
||||
@Configuration(proxyBeanMethods = false) |
||||
static class MyConfig { |
||||
|
||||
@Bean |
||||
public OkHttpClient.Builder builder() { |
||||
return new OkHttpClient.Builder().connectTimeout(1, TimeUnit.MILLISECONDS); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
@ -1,115 +0,0 @@
@@ -1,115 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2020 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.commons.httpclient; |
||||
|
||||
import java.lang.reflect.Field; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
import javax.net.ssl.SSLContextSpi; |
||||
import javax.net.ssl.SSLSocketFactory; |
||||
import javax.net.ssl.X509TrustManager; |
||||
|
||||
import org.apache.http.config.Lookup; |
||||
import org.apache.http.conn.HttpClientConnectionManager; |
||||
import org.apache.http.conn.socket.ConnectionSocketFactory; |
||||
import org.apache.http.impl.conn.DefaultHttpClientConnectionOperator; |
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.condition.DisabledForJreRange; |
||||
import org.junit.jupiter.api.condition.JRE; |
||||
|
||||
import org.springframework.util.ReflectionUtils; |
||||
|
||||
import static org.assertj.core.api.BDDAssertions.then; |
||||
|
||||
/** |
||||
* @author Ryan Baxter |
||||
* @author Michael Wirth |
||||
*/ |
||||
public class DefaultApacheHttpClientConnectionManagerFactoryTests { |
||||
|
||||
@Test |
||||
public void newConnectionManager() { |
||||
HttpClientConnectionManager connectionManager = new DefaultApacheHttpClientConnectionManagerFactory() |
||||
.newConnectionManager(false, 2, 6); |
||||
then(((PoolingHttpClientConnectionManager) connectionManager).getDefaultMaxPerRoute()).isEqualTo(6); |
||||
then(((PoolingHttpClientConnectionManager) connectionManager).getMaxTotal()).isEqualTo(2); |
||||
Object pool = getField((connectionManager), "pool"); |
||||
then((Long) getField(pool, "timeToLive")).isEqualTo(new Long(-1)); |
||||
TimeUnit timeUnit = getField(pool, "timeUnit"); |
||||
then(timeUnit).isEqualTo(TimeUnit.MILLISECONDS); |
||||
} |
||||
|
||||
@Test |
||||
public void newConnectionManagerWithTTL() { |
||||
HttpClientConnectionManager connectionManager = new DefaultApacheHttpClientConnectionManagerFactory() |
||||
.newConnectionManager(false, 2, 6, 56L, TimeUnit.DAYS, null); |
||||
then(((PoolingHttpClientConnectionManager) connectionManager).getDefaultMaxPerRoute()).isEqualTo(6); |
||||
then(((PoolingHttpClientConnectionManager) connectionManager).getMaxTotal()).isEqualTo(2); |
||||
Object pool = getField((connectionManager), "pool"); |
||||
then((Long) getField(pool, "timeToLive")).isEqualTo(new Long(56)); |
||||
TimeUnit timeUnit = getField(pool, "timeUnit"); |
||||
then(timeUnit).isEqualTo(TimeUnit.DAYS); |
||||
} |
||||
|
||||
@Test |
||||
@DisabledForJreRange(min = JRE.JAVA_16) |
||||
public void newConnectionManagerWithSSL() { |
||||
HttpClientConnectionManager connectionManager = new DefaultApacheHttpClientConnectionManagerFactory() |
||||
.newConnectionManager(false, 2, 6); |
||||
|
||||
Lookup<ConnectionSocketFactory> socketFactoryRegistry = getConnectionSocketFactoryLookup(connectionManager); |
||||
then(socketFactoryRegistry.lookup("https")).isNotNull(); |
||||
then(getX509TrustManager(socketFactoryRegistry).getAcceptedIssuers()).isNotNull(); |
||||
} |
||||
|
||||
@Test |
||||
@DisabledForJreRange(min = JRE.JAVA_16) |
||||
public void newConnectionManagerWithDisabledSSLValidation() { |
||||
HttpClientConnectionManager connectionManager = new DefaultApacheHttpClientConnectionManagerFactory() |
||||
.newConnectionManager(true, 2, 6); |
||||
|
||||
Lookup<ConnectionSocketFactory> socketFactoryRegistry = getConnectionSocketFactoryLookup(connectionManager); |
||||
then(socketFactoryRegistry.lookup("https")).isNotNull(); |
||||
then(getX509TrustManager(socketFactoryRegistry).getAcceptedIssuers()).isNull(); |
||||
} |
||||
|
||||
private Lookup<ConnectionSocketFactory> getConnectionSocketFactoryLookup( |
||||
HttpClientConnectionManager connectionManager) { |
||||
DefaultHttpClientConnectionOperator connectionOperator = getField(connectionManager, "connectionOperator"); |
||||
return getField(connectionOperator, "socketFactoryRegistry"); |
||||
} |
||||
|
||||
private X509TrustManager getX509TrustManager(Lookup<ConnectionSocketFactory> socketFactoryRegistry) { |
||||
ConnectionSocketFactory connectionSocketFactory = socketFactoryRegistry.lookup("https"); |
||||
SSLSocketFactory sslSocketFactory = getField(connectionSocketFactory, "socketfactory"); |
||||
SSLContextSpi sslContext = getField(sslSocketFactory, "context"); |
||||
return getField(sslContext, "trustManager"); |
||||
} |
||||
|
||||
@SuppressWarnings("unchecked") |
||||
protected <T> T getField(Object target, String name) { |
||||
Field field = ReflectionUtils.findField(target.getClass(), name); |
||||
if (field == null) { |
||||
throw new IllegalArgumentException("Can not find field " + name + " in " + target.getClass()); |
||||
} |
||||
ReflectionUtils.makeAccessible(field); |
||||
Object value = ReflectionUtils.getField(field, target); |
||||
return (T) value; |
||||
} |
||||
|
||||
} |
@ -1,61 +0,0 @@
@@ -1,61 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2020 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.commons.httpclient; |
||||
|
||||
import java.lang.reflect.Field; |
||||
|
||||
import org.apache.http.client.config.CookieSpecs; |
||||
import org.apache.http.client.config.RequestConfig; |
||||
import org.apache.http.client.methods.Configurable; |
||||
import org.apache.http.conn.HttpClientConnectionManager; |
||||
import org.apache.http.impl.client.CloseableHttpClient; |
||||
import org.apache.http.impl.client.HttpClientBuilder; |
||||
import org.assertj.core.api.BDDAssertions; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.util.ReflectionUtils; |
||||
|
||||
import static org.assertj.core.api.BDDAssertions.then; |
||||
import static org.mockito.Mockito.mock; |
||||
|
||||
/** |
||||
* @author Ryan Baxter |
||||
*/ |
||||
public class DefaultApacheHttpClientFactoryTests { |
||||
|
||||
@Test |
||||
public void createClient() { |
||||
final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(100).setConnectTimeout(200) |
||||
.setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); |
||||
CloseableHttpClient httpClient = new DefaultApacheHttpClientFactory(HttpClientBuilder.create()).createBuilder() |
||||
.setConnectionManager(mock(HttpClientConnectionManager.class)).setDefaultRequestConfig(requestConfig) |
||||
.build(); |
||||
BDDAssertions.then(httpClient).isInstanceOf(Configurable.class); |
||||
RequestConfig config = ((Configurable) httpClient).getConfig(); |
||||
then(config.getSocketTimeout()).isEqualTo(100); |
||||
then(config.getConnectTimeout()).isEqualTo(200); |
||||
then(config.getCookieSpec()).isEqualTo(CookieSpecs.IGNORE_COOKIES); |
||||
} |
||||
|
||||
protected <T> T getField(Object target, String name) { |
||||
Field field = ReflectionUtils.findField(target.getClass(), name); |
||||
ReflectionUtils.makeAccessible(field); |
||||
Object value = ReflectionUtils.getField(field, target); |
||||
return (T) value; |
||||
} |
||||
|
||||
} |
@ -1,82 +0,0 @@
@@ -1,82 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2020 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.commons.httpclient; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.SpringApplication; |
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
import static org.assertj.core.api.BDDAssertions.then; |
||||
|
||||
/** |
||||
* @author Ryan Baxter |
||||
*/ |
||||
@SpringBootTest(classes = MyApplication.class, properties = { "spring.cloud.httpclient.ok.enabled: true" }) |
||||
public class DefaultHttpClientConfigurationTests { |
||||
|
||||
@Autowired |
||||
ApacheHttpClientFactory httpClientFactory; |
||||
|
||||
@Autowired |
||||
ApacheHttpClientConnectionManagerFactory connectionManagerFactory; |
||||
|
||||
@Autowired |
||||
OkHttpClientFactory okHttpClientFactory; |
||||
|
||||
@Autowired |
||||
OkHttpClientConnectionPoolFactory okHttpClientConnectionPoolFactory; |
||||
|
||||
@Test |
||||
public void connManFactory() { |
||||
then(ApacheHttpClientConnectionManagerFactory.class.isInstance(this.connectionManagerFactory)).isTrue(); |
||||
then(DefaultApacheHttpClientConnectionManagerFactory.class.isInstance(this.connectionManagerFactory)).isTrue(); |
||||
} |
||||
|
||||
@Test |
||||
public void apacheHttpClientFactory() { |
||||
then(ApacheHttpClientFactory.class.isInstance(this.httpClientFactory)).isTrue(); |
||||
then(DefaultApacheHttpClientFactory.class.isInstance(this.httpClientFactory)).isTrue(); |
||||
} |
||||
|
||||
@Test |
||||
public void connPoolFactory() { |
||||
then(OkHttpClientConnectionPoolFactory.class.isInstance(this.okHttpClientConnectionPoolFactory)).isTrue(); |
||||
then(DefaultOkHttpClientConnectionPoolFactory.class.isInstance(this.okHttpClientConnectionPoolFactory)) |
||||
.isTrue(); |
||||
} |
||||
|
||||
@Test |
||||
public void setOkHttpClientFactory() { |
||||
then(OkHttpClientFactory.class.isInstance(this.okHttpClientFactory)).isTrue(); |
||||
then(DefaultOkHttpClientFactory.class.isInstance(this.okHttpClientFactory)).isTrue(); |
||||
} |
||||
|
||||
} |
||||
|
||||
@Configuration(proxyBeanMethods = false) |
||||
@EnableAutoConfiguration |
||||
class MyApplication { |
||||
|
||||
public static void main(String[] args) { |
||||
SpringApplication.run(MyApplication.class, args); |
||||
} |
||||
|
||||
} |
@ -1,56 +0,0 @@
@@ -1,56 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2020 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.commons.httpclient; |
||||
|
||||
import java.lang.reflect.Field; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
import okhttp3.ConnectionPool; |
||||
import okhttp3.internal.connection.RealConnectionPool; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.util.ReflectionUtils; |
||||
|
||||
import static org.assertj.core.api.BDDAssertions.then; |
||||
|
||||
/** |
||||
* @author Ryan Baxter |
||||
*/ |
||||
public class DefaultOkHttpClientConnectionPoolFactoryTest { |
||||
|
||||
@Test |
||||
public void create() { |
||||
DefaultOkHttpClientConnectionPoolFactory connectionPoolFactory = new DefaultOkHttpClientConnectionPoolFactory(); |
||||
ConnectionPool connectionPool = connectionPoolFactory.create(2, 3, TimeUnit.MILLISECONDS); |
||||
RealConnectionPool delegate = getField(connectionPool, "delegate"); |
||||
int idleConnections = getField(delegate, "maxIdleConnections"); |
||||
long keepAliveDuration = getField(delegate, "keepAliveDurationNs"); |
||||
then(idleConnections).isEqualTo(2); |
||||
then(keepAliveDuration).isEqualTo(TimeUnit.MILLISECONDS.toNanos(3)); |
||||
} |
||||
|
||||
protected <T> T getField(Object target, String name) { |
||||
Field field = ReflectionUtils.findField(target.getClass(), name); |
||||
if (field == null) { |
||||
throw new IllegalArgumentException("Can not find field " + name + " in " + target.getClass()); |
||||
} |
||||
ReflectionUtils.makeAccessible(field); |
||||
Object value = ReflectionUtils.getField(field, target); |
||||
return (T) value; |
||||
} |
||||
|
||||
} |
@ -1,46 +0,0 @@
@@ -1,46 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2020 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.commons.httpclient; |
||||
|
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
import okhttp3.ConnectionPool; |
||||
import okhttp3.OkHttpClient; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import static org.assertj.core.api.BDDAssertions.then; |
||||
|
||||
/** |
||||
* @author Ryan Baxter |
||||
*/ |
||||
public class DefaultOkHttpClientFactoryTest { |
||||
|
||||
@Test |
||||
public void create() throws Exception { |
||||
DefaultOkHttpClientFactory okHttpClientFactory = new DefaultOkHttpClientFactory(new OkHttpClient.Builder()); |
||||
DefaultOkHttpClientConnectionPoolFactory poolFactory = new DefaultOkHttpClientConnectionPoolFactory(); |
||||
ConnectionPool pool = poolFactory.create(4, 5, TimeUnit.DAYS); |
||||
OkHttpClient httpClient = okHttpClientFactory.createBuilder(true).connectTimeout(2, TimeUnit.MILLISECONDS) |
||||
.readTimeout(3, TimeUnit.HOURS).followRedirects(true).connectionPool(pool).build(); |
||||
then(httpClient.connectTimeoutMillis()).isEqualTo(2); |
||||
then(httpClient.readTimeoutMillis()).isEqualTo(TimeUnit.HOURS.toMillis(3)); |
||||
then(httpClient.followRedirects()).isTrue(); |
||||
then(httpClient.connectionPool()).isEqualTo(pool); |
||||
then(OkHttpClientFactory.TrustAllHostnames.class.isInstance(httpClient.hostnameVerifier())).isTrue(); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue