Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] - allow resetting a custom OkHttp client to the default imp…
Browse files Browse the repository at this point in the history
…lementation
  • Loading branch information
tobrun committed Apr 5, 2019
1 parent 3ceabcd commit 4cc46de
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.text.TextUtils;
import android.util.Log;
import com.mapbox.mapboxsdk.BuildConfig;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.http.HttpRequest;
import com.mapbox.mapboxsdk.http.HttpIdentifier;
import com.mapbox.mapboxsdk.http.HttpLogger;
import com.mapbox.mapboxsdk.http.HttpResponder;
import com.mapbox.mapboxsdk.http.HttpRequest;
import com.mapbox.mapboxsdk.http.HttpRequestUrl;
import com.mapbox.mapboxsdk.http.HttpResponder;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Dispatcher;
Expand Down Expand Up @@ -42,7 +43,11 @@ public class HttpRequestImpl implements HttpRequest {
Build.CPU_ABI)
);

private static OkHttpClient client = new OkHttpClient.Builder().dispatcher(getDispatcher()).build();
@VisibleForTesting
static final OkHttpClient DEFAULT_CLIENT = new OkHttpClient.Builder().dispatcher(getDispatcher()).build();

@VisibleForTesting
static OkHttpClient client = DEFAULT_CLIENT;

private Call call;

Expand Down Expand Up @@ -94,8 +99,12 @@ public static void enableLog(boolean enabled) {
HttpLogger.logEnabled = enabled;
}

public static void setOkHttpClient(OkHttpClient okHttpClient) {
HttpRequestImpl.client = okHttpClient;
public static void setOkHttpClient(@Nullable OkHttpClient okHttpClient) {
if (okHttpClient != null) {
HttpRequestImpl.client = okHttpClient;
} else {
HttpRequestImpl.client = DEFAULT_CLIENT;
}
}

private static class OkHttpCallback implements Callback {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mapbox.mapboxsdk.module.http;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import okhttp3.OkHttpClient;
import okio.Buffer;

Expand Down Expand Up @@ -38,10 +39,14 @@ public static void setPrintRequestUrlOnFailure(boolean enabled) {

/**
* Set the OkHttpClient used for requesting map resources.
* <p>
* This configuration survives across mapView instances.
* Reset the OkHttpClient to the default by passing null as parameter.
* </p>
*
* @param client the OkHttpClient
*/
public static void setOkHttpClient(OkHttpClient client) {
public static void setOkHttpClient(@Nullable OkHttpClient client) {
HttpRequestImpl.setOkHttpClient(client);
}

Expand Down
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();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.mapbox.mapboxsdk;

import android.content.Context;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.lang.reflect.Field;

import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertSame;
import static org.junit.Assert.assertEquals;
Expand All @@ -32,13 +29,13 @@ public void before() {
@Test
public void testGetAccessToken() {
final String accessToken = "pk.0000000001";
injectMapboxSingleton(accessToken);
MapboxInjector.inject(context, accessToken);
assertSame(accessToken, Mapbox.getAccessToken());
}

@Test
public void testApplicationContext() {
injectMapboxSingleton("pk.0000000001");
MapboxInjector.inject(context, "pk.0000000001");
assertNotNull(Mapbox.getApplicationContext());
assertNotEquals(context, appContext);
assertEquals(appContext, appContext);
Expand Down Expand Up @@ -71,27 +68,7 @@ public void testBlaBlaToken() {

@After
public void after() {
clearMapboxSingleton();
MapboxInjector.clear();
}

private void injectMapboxSingleton(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();
}
}

private void clearMapboxSingleton() {
try {
Field field = Mapbox.class.getDeclaredField("INSTANCE");
field.setAccessible(true);
field.set(field, null);
} catch (Exception exception) {
throw new AssertionError();
}
}
}
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()
}
}

0 comments on commit 4cc46de

Please sign in to comment.