Ryan Baxter
7 years ago
17 changed files with 409 additions and 88 deletions
@ -0,0 +1,160 @@
@@ -0,0 +1,160 @@
|
||||
/* |
||||
* |
||||
* * Copyright 2013-2016 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 |
||||
* * |
||||
* * http://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. |
||||
* |
||||
*/ |
||||
|
||||
import feign.Client; |
||||
import okhttp3.ConnectionPool; |
||||
import okhttp3.OkHttpClient; |
||||
|
||||
import java.lang.reflect.Field; |
||||
import java.util.ArrayList; |
||||
import java.util.concurrent.TimeUnit; |
||||
import javax.net.ssl.SSLSocketFactory; |
||||
import javax.net.ssl.X509TrustManager; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.MockingDetails; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
import org.springframework.cloud.commons.httpclient.DefaultOkHttpClientConnectionPoolFactory; |
||||
import org.springframework.cloud.commons.httpclient.DefaultOkHttpClientFactory; |
||||
import org.springframework.cloud.commons.httpclient.OkHttpClientConnectionPoolFactory; |
||||
import org.springframework.cloud.commons.httpclient.OkHttpClientFactory; |
||||
import org.springframework.cloud.netflix.feign.FeignClient; |
||||
import org.springframework.cloud.netflix.feign.ribbon.LoadBalancerFeignClient; |
||||
import org.springframework.cloud.netflix.ribbon.okhttp.OkHttpLoadBalancingClient; |
||||
import org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer; |
||||
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; |
||||
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext; |
||||
import org.springframework.cloud.netflix.zuul.filters.route.okhttp.OkHttpRibbonCommand; |
||||
import org.springframework.cloud.netflix.zuul.filters.route.okhttp.OkHttpRibbonCommandFactory; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.test.annotation.DirtiesContext; |
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
||||
import org.springframework.util.LinkedMultiValueMap; |
||||
import org.springframework.util.ReflectionUtils; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
|
||||
import static org.junit.Assert.assertTrue; |
||||
import static org.mockito.Mockito.mock; |
||||
import static org.mockito.Mockito.mockingDetails; |
||||
|
||||
/** |
||||
* @author Ryan Baxter |
||||
*/ |
||||
@RunWith(SpringJUnit4ClassRunner.class) |
||||
@SpringBootTest(classes = OkHttpClientConfigurationTestApp.class, value = {"feign.okhttp.enabled: true", |
||||
"spring.cloud.httpclient.ok.enabled: true", "ribbon.eureka.enabled = false"}) |
||||
@DirtiesContext |
||||
public class OkHttpClientConfigurationTests { |
||||
|
||||
@Autowired |
||||
OkHttpClientFactory okHttpClientFactory; |
||||
|
||||
@Autowired |
||||
OkHttpClientConnectionPoolFactory connectionPoolFactory; |
||||
|
||||
@Autowired |
||||
LoadBalancerFeignClient feignClient; |
||||
|
||||
@Autowired |
||||
OkHttpRibbonCommandFactory okHttpRibbonCommandFactory; |
||||
|
||||
@Test |
||||
public void testFactories() { |
||||
assertTrue(OkHttpClientConnectionPoolFactory.class.isInstance(connectionPoolFactory)); |
||||
assertTrue(OkHttpClientConfigurationTestApp.MyOkHttpClientConnectionPoolFactory.class.isInstance(connectionPoolFactory)); |
||||
assertTrue(OkHttpClientFactory.class.isInstance(okHttpClientFactory)); |
||||
assertTrue(OkHttpClientConfigurationTestApp.MyOkHttpClientFactory.class.isInstance(okHttpClientFactory)); |
||||
} |
||||
|
||||
@Test |
||||
public void testHttpClientWithFeign() { |
||||
Client delegate = feignClient.getDelegate(); |
||||
assertTrue(feign.okhttp.OkHttpClient.class.isInstance(delegate)); |
||||
feign.okhttp.OkHttpClient okHttpClient = (feign.okhttp.OkHttpClient)delegate; |
||||
OkHttpClient httpClient = getField(okHttpClient, "delegate"); |
||||
MockingDetails httpClientDetails = mockingDetails(httpClient); |
||||
assertTrue(httpClientDetails.isMock()); |
||||
} |
||||
|
||||
@Test |
||||
public void testOkHttpLoadBalancingHttpClient() { |
||||
RibbonCommandContext context = new RibbonCommandContext("foo"," GET", "http://localhost", |
||||
false, new LinkedMultiValueMap<String, String>(), new LinkedMultiValueMap<String, String>(), |
||||
null, new ArrayList<RibbonRequestCustomizer>(), 0l); |
||||
OkHttpRibbonCommand command = okHttpRibbonCommandFactory.create(context); |
||||
OkHttpLoadBalancingClient ribbonClient = command.getClient(); |
||||
OkHttpClient httpClient = getField(ribbonClient, "delegate"); |
||||
MockingDetails httpClientDetails = mockingDetails(httpClient); |
||||
assertTrue(httpClientDetails.isMock()); |
||||
} |
||||
|
||||
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 |
||||
@EnableAutoConfiguration |
||||
@RestController |
||||
//@EnableFeignClients(clients = {})
|
||||
@EnableZuulProxy |
||||
class OkHttpClientConfigurationTestApp { |
||||
|
||||
@RequestMapping |
||||
public String index() { |
||||
return "hello"; |
||||
} |
||||
|
||||
static class MyOkHttpClientConnectionPoolFactory extends DefaultOkHttpClientConnectionPoolFactory { |
||||
@Override |
||||
public ConnectionPool create(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit) { |
||||
return new ConnectionPool(); |
||||
} |
||||
} |
||||
|
||||
static class MyOkHttpClientFactory extends DefaultOkHttpClientFactory { |
||||
@Override |
||||
public OkHttpClient create(boolean disableSslValidation, long connectTimeout, TimeUnit connectTimeoutUnit, boolean followRedirects, long readTimeout, TimeUnit readTimeoutUnit, ConnectionPool connectionPool, SSLSocketFactory sslSocketFactory, X509TrustManager x509TrustManager) { |
||||
return mock(OkHttpClient.class); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class MyConfig { |
||||
@Bean |
||||
public OkHttpClientConnectionPoolFactory connectionPoolFactory() { |
||||
return new MyOkHttpClientConnectionPoolFactory(); |
||||
} |
||||
|
||||
@Bean |
||||
public OkHttpClientFactory clientFactory() { |
||||
return new MyOkHttpClientFactory(); |
||||
} |
||||
|
||||
} |
||||
|
||||
@FeignClient(name="foo", serviceId = "foo") |
||||
static interface FooClient {} |
||||
} |
Loading…
Reference in new issue