9 changed files with 183 additions and 10 deletions
@ -0,0 +1,70 @@
@@ -0,0 +1,70 @@
|
||||
/* |
||||
* Copyright 2013-2018 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. |
||||
*/ |
||||
package org.springframework.cloud.commons.httpclient; |
||||
|
||||
import org.apache.http.impl.client.HttpClientBuilder; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
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 org.springframework.test.context.junit4.SpringRunner; |
||||
|
||||
import static org.junit.Assert.assertTrue; |
||||
|
||||
/** |
||||
* @author Ryan Baxter |
||||
*/ |
||||
@RunWith(SpringRunner.class) |
||||
@SpringBootTest(classes = CustomHttpClientBuilderApplication.class) |
||||
public class CustomHttpClientBuilderConfigurationTests { |
||||
|
||||
@Autowired |
||||
ApacheHttpClientFactory apacheHttpClientFactory; |
||||
|
||||
@Test |
||||
public void testCustomBuilder() { |
||||
HttpClientBuilder builder = apacheHttpClientFactory.createBuilder(); |
||||
assertTrue(CustomHttpClientBuilderApplication.MyHttpClientBuilder.class.isInstance(builder)); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
@EnableAutoConfiguration |
||||
class CustomHttpClientBuilderApplication { |
||||
|
||||
public static void main(String[] args) { |
||||
SpringApplication.run(MyApplication.class, args); |
||||
} |
||||
|
||||
@Configuration |
||||
@AutoConfigureBefore(value = HttpClientConfiguration.class) |
||||
static class MyConfig { |
||||
|
||||
@Bean |
||||
public MyHttpClientBuilder builder() { |
||||
return new MyHttpClientBuilder(); |
||||
} |
||||
|
||||
} |
||||
|
||||
static class MyHttpClientBuilder extends HttpClientBuilder { |
||||
} |
||||
} |
@ -0,0 +1,77 @@
@@ -0,0 +1,77 @@
|
||||
/* |
||||
* Copyright 2013-2018 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. |
||||
*/ |
||||
package org.springframework.cloud.commons.httpclient; |
||||
|
||||
import okhttp3.OkHttpClient; |
||||
|
||||
import java.lang.reflect.Field; |
||||
import java.util.concurrent.TimeUnit; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
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.test.context.junit4.SpringRunner; |
||||
import org.springframework.util.ReflectionUtils; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
|
||||
/** |
||||
* @author Ryan Baxter |
||||
*/ |
||||
@RunWith(SpringRunner.class) |
||||
@SpringBootTest(classes = CustomOkHttpClientBuilderApplication.class) |
||||
public class CustomOkHttpClientBuilderConfigurationTests { |
||||
|
||||
@Autowired |
||||
private OkHttpClientFactory okHttpClientFactory; |
||||
|
||||
@Test |
||||
public void testCustomBuilder() { |
||||
OkHttpClient.Builder builder = okHttpClientFactory.createBuilder(false); |
||||
Integer timeout = getField(builder, "connectTimeout"); |
||||
assertEquals(1, timeout.intValue()); |
||||
} |
||||
|
||||
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 |
||||
class CustomOkHttpClientBuilderApplication { |
||||
|
||||
public static void main(String[] args) { |
||||
SpringApplication.run(MyApplication.class, args); |
||||
} |
||||
|
||||
@Configuration |
||||
static class MyConfig { |
||||
|
||||
@Bean |
||||
public OkHttpClient.Builder builder() { |
||||
return new OkHttpClient.Builder().connectTimeout(1, TimeUnit.MILLISECONDS); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue