|
1 | 1 | package com.configcat; |
2 | 2 |
|
| 3 | +import okhttp3.OkHttpClient; |
3 | 4 | import okhttp3.mockwebserver.MockResponse; |
4 | 5 | import okhttp3.mockwebserver.MockWebServer; |
5 | 6 | import org.junit.jupiter.api.Test; |
|
10 | 11 | import java.io.File; |
11 | 12 | import java.io.IOException; |
12 | 13 | import java.lang.reflect.Field; |
| 14 | +import java.net.InetSocketAddress; |
| 15 | +import java.net.Proxy; |
13 | 16 | import java.time.Duration; |
14 | 17 | import java.time.Instant; |
15 | 18 | import java.util.*; |
@@ -107,6 +110,55 @@ void getValueWithDefaultConfigTimeout() throws IOException { |
107 | 110 | cl.close(); |
108 | 111 | } |
109 | 112 |
|
| 113 | + @Test |
| 114 | + void httpOptionsDefaultValuesPreserveOkHttpDefaults() throws Exception { |
| 115 | + |
| 116 | + ConfigCatClient client = ConfigCatClient.get(Helpers.SDK_KEY, options -> { |
| 117 | + options.pollingMode(PollingModes.manualPoll()); |
| 118 | + }); |
| 119 | + |
| 120 | + OkHttpClient fetcherClient = getFetcherClientWithReflection(client); |
| 121 | + OkHttpClient baselineClient = new OkHttpClient.Builder().build(); |
| 122 | + |
| 123 | + assertEquals(baselineClient.connectTimeoutMillis(), fetcherClient.connectTimeoutMillis()); |
| 124 | + assertEquals(baselineClient.readTimeoutMillis(), fetcherClient.readTimeoutMillis()); |
| 125 | + |
| 126 | + client.close(); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void httpOptionsTimeoutsAppliedToClient() throws Exception { |
| 131 | + |
| 132 | + ConfigCatClient client = ConfigCatClient.get(Helpers.SDK_KEY, options -> { |
| 133 | + options.pollingMode(PollingModes.manualPoll()); |
| 134 | + options.httpOptions().connectTimeoutMillis(1234).readTimeoutMillis(5678); |
| 135 | + }); |
| 136 | + |
| 137 | + OkHttpClient fetcherClient = getFetcherClientWithReflection(client); |
| 138 | + |
| 139 | + assertEquals(1234, fetcherClient.connectTimeoutMillis()); |
| 140 | + assertEquals(5678, fetcherClient.readTimeoutMillis()); |
| 141 | + |
| 142 | + client.close(); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + void httpOptionsProxyAppliedToClient() throws Exception { |
| 147 | + |
| 148 | + Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8080)); |
| 149 | + |
| 150 | + ConfigCatClient client = ConfigCatClient.get(Helpers.SDK_KEY, options -> { |
| 151 | + options.pollingMode(PollingModes.manualPoll()); |
| 152 | + options.httpOptions().proxy(proxy); |
| 153 | + }); |
| 154 | + |
| 155 | + OkHttpClient fetcherClient = getFetcherClientWithReflection(client); |
| 156 | + |
| 157 | + assertSame(proxy, fetcherClient.proxy()); |
| 158 | + |
| 159 | + client.close(); |
| 160 | + } |
| 161 | + |
110 | 162 | @Test |
111 | 163 | void getConfigurationWithFailingCache() throws IOException { |
112 | 164 | MockWebServer server = new MockWebServer(); |
@@ -935,6 +987,20 @@ private static String getCacheKeyWithReflection(ConfigCatClient cl) throws NoSuc |
935 | 987 | return (String) cacheKeyField.get(configService); |
936 | 988 | } |
937 | 989 |
|
| 990 | + private static OkHttpClient getFetcherClientWithReflection(ConfigCatClient client) throws NoSuchFieldException, IllegalAccessException { |
| 991 | + Field configServiceField = ConfigCatClient.class.getDeclaredField("configService"); |
| 992 | + configServiceField.setAccessible(true); |
| 993 | + ConfigService configService = (ConfigService) configServiceField.get(client); |
| 994 | + |
| 995 | + Field configFetcherField = ConfigService.class.getDeclaredField("configFetcher"); |
| 996 | + configFetcherField.setAccessible(true); |
| 997 | + ConfigFetcher configFetcher = (ConfigFetcher) configFetcherField.get(configService); |
| 998 | + |
| 999 | + Field httpClientField = ConfigFetcher.class.getDeclaredField("httpClient"); |
| 1000 | + httpClientField.setAccessible(true); |
| 1001 | + return (OkHttpClient) httpClientField.get(configFetcher); |
| 1002 | + } |
| 1003 | + |
938 | 1004 | @Test |
939 | 1005 | void testSpecialCharactersWorks() throws IOException { |
940 | 1006 |
|
|
0 commit comments