Skip to content

Commit

Permalink
Merge pull request #151 from amplitude/update-okhttp3-3.9
Browse files Browse the repository at this point in the history
Update okhttp3 3.9
  • Loading branch information
djih authored Oct 4, 2017
2 parents 353e2b9 + c355a50 commit 48d2f71
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

* Updating to latest version of OkHttp3 ([3.9.0](https://github.com/square/okhttp/blob/master/CHANGELOG.md#version-390))

## 2.14.1 (July 27, 2017)

* Switch to an internal implementation of `isEmptyString` instead of Android TextUtils.
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.0.1</version>
<version>3.9.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<version>3.0.1</version>
<version>3.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
36 changes: 36 additions & 0 deletions test/com/amplitude/api/AmplitudeClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowLooper;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.RecordedRequest;

Expand Down Expand Up @@ -1519,4 +1526,33 @@ public void testSendNullEvents() throws JSONException {
assertEquals(events.length(), 1);
assertEquals(events.optJSONObject(0).optString("event_type"), "test event");
}

@Test
@PrepareForTest(OkHttpClient.class)
public void testHandleUploadExceptions() throws Exception {
ShadowLooper logLooper = Shadows.shadowOf(amplitude.logThread.getLooper());
ShadowLooper httpLooper = Shadows.shadowOf(amplitude.httpThread.getLooper());
IOException error = new IOException("test IO Exception");

// mock out client
OkHttpClient oldClient = amplitude.httpClient;
OkHttpClient mockClient = PowerMockito.mock(OkHttpClient.class);

// need to have mock client return mock call that throws exception
Call mockCall = PowerMockito.mock(Call.class);
PowerMockito.when(mockCall.execute()).thenThrow(error);
PowerMockito.when(mockClient.newCall(Matchers.any(Request.class))).thenReturn(mockCall);

// attach mock client to amplitude
amplitude.httpClient = mockClient;
amplitude.logEvent("test event");
logLooper.runToEndOfTasks();
logLooper.runToEndOfTasks();
httpLooper.runToEndOfTasks();

assertEquals(amplitude.lastError, error);

// restore old client
amplitude.httpClient = oldClient;
}
}

0 comments on commit 48d2f71

Please sign in to comment.