Skip to content

Commit

Permalink
Update OkHttp version to 3.10.0 (#72)
Browse files Browse the repository at this point in the history
* bump okhttp version to 3.10.0

* add hostname verifier to telemetry client settings builder needed to workaround stuck mockwebserver tests after bumping okhttp version to 3.10.0
  • Loading branch information
Guardiola31337 committed Mar 2, 2018
1 parent fbc4db9 commit 30f5b6c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ext {
constraintLayout : '1.0.2',
mockito : '2.11.0',
testRunnerVersion: '1.0.1',
okhttp3 : '3.9.0',
okhttp3 : '3.10.0',
gson : '2.8.2',
espressoVersion : '3.0.1'
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;

Expand All @@ -29,6 +30,7 @@ class TelemetryClientSettings {
private final HttpUrl baseUrl;
private final SSLSocketFactory sslSocketFactory;
private final X509TrustManager x509TrustManager;
private final HostnameVerifier hostnameVerifier;
private boolean debugLoggingEnabled;

TelemetryClientSettings(Builder builder) {
Expand All @@ -37,6 +39,7 @@ class TelemetryClientSettings {
this.baseUrl = builder.baseUrl;
this.sslSocketFactory = builder.sslSocketFactory;
this.x509TrustManager = builder.x509TrustManager;
this.hostnameVerifier = builder.hostnameVerifier;
this.debugLoggingEnabled = builder.debugLoggingEnabled;
}

Expand All @@ -63,6 +66,7 @@ Builder toBuilder() {
.baseUrl(baseUrl)
.sslSocketFactory(sslSocketFactory)
.x509TrustManager(x509TrustManager)
.hostnameVerifier(hostnameVerifier)
.debugLoggingEnabled(debugLoggingEnabled);
}

Expand All @@ -78,6 +82,7 @@ static final class Builder {
HttpUrl baseUrl = null;
SSLSocketFactory sslSocketFactory = null;
X509TrustManager x509TrustManager = null;
HostnameVerifier hostnameVerifier = null;
boolean debugLoggingEnabled = false;

Builder() {
Expand Down Expand Up @@ -112,6 +117,11 @@ Builder x509TrustManager(X509TrustManager x509TrustManager) {
return this;
}

Builder hostnameVerifier(HostnameVerifier hostnameVerifier) {
this.hostnameVerifier = hostnameVerifier;
return this;
}

Builder debugLoggingEnabled(boolean debugLoggingEnabled) {
this.debugLoggingEnabled = debugLoggingEnabled;
return this;
Expand All @@ -135,6 +145,7 @@ private OkHttpClient configureHttpClient() {
.connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS));
if (isSocketFactoryUnset(sslSocketFactory, x509TrustManager)) {
builder.sslSocketFactory(sslSocketFactory, x509TrustManager);
builder.hostnameVerifier(hostnameVerifier);
}

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.Locale;
import java.util.UUID;

import okhttp3.internal.Util;
import okio.Buffer;

class TelemetryUtils {
static final String MAPBOX_SHARED_PREFERENCES = "MapboxSharedPreferences";
Expand All @@ -37,7 +37,7 @@ static String obtainUniversalUniqueIdentifier() {

static String createFullUserAgent(String userAgent, Context context) {
String appIdentifier = TelemetryUtils.obtainApplicationIdentifier(context);
String newUserAgent = Util.toHumanReadableAscii(String.format(DEFAULT_LOCALE, TWO_STRING_FORMAT, appIdentifier,
String newUserAgent = toHumanReadableAscii(String.format(DEFAULT_LOCALE, TWO_STRING_FORMAT, appIdentifier,
userAgent));
String fullUserAgent = TextUtils.isEmpty(appIdentifier) ? userAgent : newUserAgent;

Expand Down Expand Up @@ -90,4 +90,22 @@ private static String obtainApplicationIdentifier(Context context) {
return EMPTY_STRING;
}
}

private static String toHumanReadableAscii(String s) {
for (int i = 0, length = s.length(), c; i < length; i += Character.charCount(c)) {
c = s.codePointAt(i);
if (c > '\u001f' && c < '\u007f') {
continue;
}

Buffer buffer = new Buffer();
buffer.writeUtf8(s, 0, i);
for (int j = i; j < length; j += Character.charCount(c)) {
c = s.codePointAt(j);
buffer.writeUtf8CodePoint(c > '\u001f' && c < '\u007f' ? c : '?');
}
return buffer.readUtf8();
}
return s;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
import java.util.Arrays;
import java.util.List;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.HttpUrl;
import okhttp3.internal.tls.SslClient;
import okhttp3.mockwebserver.internal.tls.SslClient;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
Expand Down Expand Up @@ -172,6 +175,12 @@ private TelemetryClientSettings provideDefaultTelemetryClientSettings() {
.baseUrl(localUrl)
.sslSocketFactory(sslClient.socketFactory)
.x509TrustManager(sslClient.trustManager)
.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
})
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import okhttp3.internal.tls.SslClient;
import okhttp3.mockwebserver.internal.tls.SslClient;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -183,6 +186,12 @@ public void checksRequestTimeoutFailure() throws Exception {
.baseUrl(localUrl)
.sslSocketFactory(sslClient.socketFactory)
.x509TrustManager(sslClient.trustManager)
.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
})
.build();
TelemetryClient telemetryClient = new TelemetryClient("anyAccessToken", "anyUserAgent", telemetryClientSettings,
mock(Logger.class));
Expand Down

0 comments on commit 30f5b6c

Please sign in to comment.