This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[android] - allow resetting a custom OkHttp client to the default imp…
…lementation
- Loading branch information
Showing
5 changed files
with
87 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/MapboxInjector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.mapbox.mapboxsdk; | ||
|
||
import android.content.Context; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
public class MapboxInjector { | ||
|
||
public static void inject(Context context, String accessToken) { | ||
Mapbox mapbox = new Mapbox(context, accessToken); | ||
try { | ||
Field field = Mapbox.class.getDeclaredField("INSTANCE"); | ||
field.setAccessible(true); | ||
field.set(mapbox, mapbox); | ||
} catch (Exception exception) { | ||
throw new AssertionError(); | ||
} | ||
} | ||
|
||
public static void clear() { | ||
try { | ||
Field field = Mapbox.class.getDeclaredField("INSTANCE"); | ||
field.setAccessible(true); | ||
field.set(field, null); | ||
} catch (Exception exception) { | ||
throw new AssertionError(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
.../MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/module/http/HttpRequestUtilTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.mapbox.mapboxsdk.module.http | ||
|
||
import com.mapbox.mapboxsdk.MapboxInjector | ||
import io.mockk.mockk | ||
import junit.framework.Assert.assertEquals | ||
import okhttp3.OkHttpClient | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class HttpRequestUtilTest { | ||
|
||
@Test | ||
fun replaceHttpClient() { | ||
MapboxInjector.inject(mockk(relaxed = true), "") | ||
|
||
assertEquals(HttpRequestImpl.DEFAULT_CLIENT, HttpRequestImpl.client) | ||
|
||
val httpMock = mockk<OkHttpClient>() | ||
HttpRequestUtil.setOkHttpClient(httpMock) | ||
assertEquals("Http client should have set to the mocked client", | ||
httpMock, | ||
HttpRequestImpl.client | ||
) | ||
|
||
HttpRequestUtil.setOkHttpClient(null) | ||
assertEquals("Http client should have been reset to the default client", | ||
HttpRequestImpl.DEFAULT_CLIENT, | ||
HttpRequestImpl.client | ||
) | ||
|
||
MapboxInjector.clear() | ||
} | ||
} |