diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java b/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java index 5b82aa027a3fad..49920b7977c059 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java @@ -31,6 +31,7 @@ public class OkHttpClientProvider { // Centralized OkHttpClient for all networking requests. private static @Nullable OkHttpClient sClient; + private static @Nullable OkHttpClient.Builder sClientBuilder; public static OkHttpClient getOkHttpClient() { if (sClient == null) { @@ -38,22 +39,37 @@ public static OkHttpClient getOkHttpClient() { } return sClient; } - - // okhttp3 OkHttpClient is immutable - // This allows app to init an OkHttpClient with custom settings. + + /** + * okhttp3 OkHttpClient is immutable + * This allows app to init an OkHttpClient with custom settings. + * @deprecated Use {@link #replaceOkHttpClientBuilder(OkHttpClient.Builder)} instead. + */ + @Deprecated public static void replaceOkHttpClient(OkHttpClient client) { sClient = client; } + /** + * Replace the builder used by {@link #createClient()} + */ + public static void replaceOkHttpClientBuilder(OkHttpClient.Builder builder) { + sClientBuilder = builder; + } + public static OkHttpClient createClient() { - // No timeouts by default - OkHttpClient.Builder client = new OkHttpClient.Builder() - .connectTimeout(0, TimeUnit.MILLISECONDS) - .readTimeout(0, TimeUnit.MILLISECONDS) - .writeTimeout(0, TimeUnit.MILLISECONDS) - .cookieJar(new ReactCookieJarContainer()); - - return enableTls12OnPreLollipop(client).build(); + if (sClientBuilder != null) { + return sClientBuilder.build(); + } else { + // No timeouts by default + OkHttpClient.Builder client = new OkHttpClient.Builder() + .connectTimeout(0, TimeUnit.MILLISECONDS) + .readTimeout(0, TimeUnit.MILLISECONDS) + .writeTimeout(0, TimeUnit.MILLISECONDS) + .cookieJar(new ReactCookieJarContainer()); + + return enableTls12OnPreLollipop(client).build(); + } } /* diff --git a/ReactAndroid/src/test/java/com/facebook/react/modules/network/OkHttpClientProviderTest.java b/ReactAndroid/src/test/java/com/facebook/react/modules/network/OkHttpClientProviderTest.java new file mode 100644 index 00000000000000..9648ef79ccf79f --- /dev/null +++ b/ReactAndroid/src/test/java/com/facebook/react/modules/network/OkHttpClientProviderTest.java @@ -0,0 +1,48 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package com.facebook.react.modules.network; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.robolectric.RobolectricTestRunner; + +import okhttp3.OkHttpClient; + +import static org.fest.assertions.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Tests for {@link OkHttpClientProvider}. + */ +@PrepareForTest({ + OkHttpClient.class, + OkHttpClient.Builder.class +}) +@RunWith(RobolectricTestRunner.class) +@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"}) +public class OkHttpClientProviderTest { + + @Test + public void testReplaceOkHttpClientBuilder() throws Exception { + OkHttpClient httpClient = mock(OkHttpClient.class); + OkHttpClient.Builder clientBuilder = mock(OkHttpClient.Builder.class); + when(clientBuilder.build()).thenReturn(httpClient); + when(httpClient.newBuilder()).thenReturn(clientBuilder); + + OkHttpClientProvider.replaceOkHttpClientBuilder(clientBuilder); + assertThat(OkHttpClientProvider.createClient()).isEqualTo(httpClient); + + OkHttpClientProvider.replaceOkHttpClientBuilder(null); + assertThat(OkHttpClientProvider.createClient()).isNotEqualTo(httpClient); + } +}