Skip to content

Commit 7fce9b9

Browse files
committed
Add tests for HTTP options in ConfigCatClient to verify default values and timeout settings
1 parent 3b6a398 commit 7fce9b9

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

src/test/java/com/configcat/ConfigCatClientTest.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.configcat;
22

3+
import okhttp3.OkHttpClient;
34
import okhttp3.mockwebserver.MockResponse;
45
import okhttp3.mockwebserver.MockWebServer;
56
import org.junit.jupiter.api.Test;
@@ -10,6 +11,8 @@
1011
import java.io.File;
1112
import java.io.IOException;
1213
import java.lang.reflect.Field;
14+
import java.net.InetSocketAddress;
15+
import java.net.Proxy;
1316
import java.time.Duration;
1417
import java.time.Instant;
1518
import java.util.*;
@@ -107,6 +110,55 @@ void getValueWithDefaultConfigTimeout() throws IOException {
107110
cl.close();
108111
}
109112

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+
110162
@Test
111163
void getConfigurationWithFailingCache() throws IOException {
112164
MockWebServer server = new MockWebServer();
@@ -935,6 +987,20 @@ private static String getCacheKeyWithReflection(ConfigCatClient cl) throws NoSuc
935987
return (String) cacheKeyField.get(configService);
936988
}
937989

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+
9381004
@Test
9391005
void testSpecialCharactersWorks() throws IOException {
9401006

0 commit comments

Comments
 (0)