Skip to content

Commit

Permalink
Merge pull request #25 from flipkart-incubator/unit_test
Browse files Browse the repository at this point in the history
New Unit Test Cases added + Refactored
  • Loading branch information
yasirmhd authored Jul 15, 2016
2 parents 1b23f59 + 0f7c5ad commit fb66c87
Show file tree
Hide file tree
Showing 11 changed files with 452 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ public PersistentStatsHandler(Context context) {
this.mCurrentAvgSpeed = mPreferenceManager.getAverageSpeed(getNetworkKey(getActiveNetworkInfo()));
}

@VisibleForTesting
public PersistentStatsHandler(Context context, PreferenceManager preferenceManager) {
this.mPreferenceManager = preferenceManager;
this.MAX_SIZE = DEFAULT_MAX_SIZE;
this.mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
this.mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
this.mNetworkStat = new NetworkStat();
this.mCurrentAvgSpeed = mPreferenceManager.getAverageSpeed(getNetworkKey(getActiveNetworkInfo()));
}

@VisibleForTesting
public Set<OnResponseListener> getOnResponseListeners() {
return mOnResponseListeners;
}

/**
* Client can call this to get the current network info
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import com.flipkart.okhttpstats.interpreter.DefaultInterpreter;
import com.flipkart.okhttpstats.interpreter.NetworkInterpreter;
import com.flipkart.okhttpstats.reporter.NetworkEventReporter;
import com.flipkart.okhttpstats.toolbox.Utils;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

import java.io.ByteArrayOutputStream;
Expand All @@ -28,7 +29,6 @@
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.internal.http.OkHeaders;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okio.Buffer;
Expand All @@ -54,6 +54,19 @@ private static byte[] compress(byte[] data) throws IOException {
return buf.toByteArray();
}

/**
* Test to verify that {@link IllegalStateException} is thrown if {@link NetworkInterpreter} is null
*
* @throws Exception
*/
@Test(expected = IllegalStateException.class)
public void testIfExceptionThrownIfInterpreterNull() throws Exception {
new NetworkInterceptor.Builder()
.setEnabled(true)
.setNetworkInterpreter(null)
.build();
}

/**
* Test to verify the response object before and after interception
*
Expand All @@ -67,7 +80,7 @@ public void testInterceptedResponse() throws IOException {
NetworkInterceptor networkInterceptor = new NetworkInterceptor.Builder()
.setEnabled(true)
.setNetworkInterpreter(networkInterpreter)
.build(RuntimeEnvironment.application);
.build();

Uri requestUri = Uri.parse("http://www.flipkart.com");
String requestText = "Test Request";
Expand Down Expand Up @@ -105,59 +118,7 @@ public void testInterceptedResponse() throws IOException {
}

/**
* Test to verify {@link com.flipkart.okhttpstats.interpreter.DefaultInterpreter.ForwardingResponseBody}
*
* @throws IOException
*/
@Test
public void testResponseBody() throws IOException {
NetworkEventReporter networkEventReporter = mock(NetworkEventReporter.class);
NetworkInterpreter networkInterpreter = new DefaultInterpreter(networkEventReporter);

NetworkInterceptor networkInterceptor = new NetworkInterceptor.Builder()
.setEnabled(true)
.setNetworkInterpreter(networkInterpreter)
.build(RuntimeEnvironment.application);

Uri requestUri = Uri.parse("http://www.flipkart.com");
String requestText = "Test Request";

//creating request
Request request = new Request.Builder()
.url(requestUri.toString())
.method("POST", RequestBody.create(MediaType.parse("text/plain"), requestText))
.build();

//creating response
Response response = new Response.Builder()
.request(request)
.protocol(Protocol.HTTP_1_1)
.code(200)
.addHeader("Content-Length", "20")
.body(ResponseBody.create(MediaType.parse("text/plain"), "any text"))
.build();

CustomChain customChain = new CustomChain(request, response, null);
networkInterceptor.intercept(customChain);

//intercepted request object
Response interceptedResponse = customChain.proceed(request);
DefaultInterpreter.ForwardingResponseBody forwardingResponseBody = new DefaultInterpreter.ForwardingResponseBody(interceptedResponse.body(), interceptedResponse.body().byteStream());

interceptedResponse = interceptedResponse.newBuilder().body(forwardingResponseBody).build();

//assert the response body of response
Assert.assertTrue(interceptedResponse.body() == forwardingResponseBody);
//assert the content type
Assert.assertTrue(interceptedResponse.body().contentType() == forwardingResponseBody.contentType());
//assert the content length
Assert.assertTrue(interceptedResponse.body().contentLength() == forwardingResponseBody.contentLength());
//assert the source
Assert.assertTrue(interceptedResponse.body().source() == forwardingResponseBody.source());
}

/**
* Test for Response With Content Length
* Test for Response With Content Length in their header
*
* @throws IOException
*/
Expand All @@ -169,7 +130,7 @@ public void testInterceptedResponseWithContentLength() throws IOException {
NetworkInterceptor networkInterceptor = new NetworkInterceptor.Builder()
.setEnabled(true)
.setNetworkInterpreter(networkInterpreter)
.build(RuntimeEnvironment.application);
.build();


Uri requestUri = Uri.parse("http://www.flipkart.com");
Expand Down Expand Up @@ -216,7 +177,7 @@ public void testInterceptedRequest() throws IOException {
NetworkInterceptor networkInterceptor = new NetworkInterceptor.Builder()
.setEnabled(true)
.setNetworkInterpreter(networkInterpreter)
.build(RuntimeEnvironment.application);
.build();

Uri requestUri = Uri.parse("http://www.flipkart.com");
String requestText = "Test Request";
Expand Down Expand Up @@ -248,14 +209,15 @@ public void testInterceptedRequest() throws IOException {
Assert.assertTrue(interceptedRequest.url().toString().equals(request.url().toString()));
//assert request body is same
Assert.assertTrue(interceptedRequest.body().equals(request.body()));

}

/**
* Test the request object using {@link MockWebServer}
* Trying to simulate a real example by mocking server
*
* @throws IOException
*/
@Config(sdk = 23)
@Test
public void testRequest() throws IOException {
NetworkEventReporter networkEventReporter = mock(NetworkEventReporter.class);
Expand All @@ -264,10 +226,9 @@ public void testRequest() throws IOException {
NetworkInterceptor networkInterceptor = new NetworkInterceptor.Builder()
.setNetworkInterpreter(networkInterpreter)
.setEnabled(true)
.build(RuntimeEnvironment.application);
.build();

OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.networkInterceptors().add(networkInterceptor);
OkHttpClient okHttpClient = new OkHttpClient.Builder().addNetworkInterceptor(networkInterceptor).build();

MockWebServer server = new MockWebServer();
server.start();
Expand All @@ -294,6 +255,7 @@ public void testRequest() throws IOException {
*
* @throws IOException
*/
@Config(sdk = 23)
@Test
public void testResponseWithoutContentLength() throws IOException {
NetworkEventReporter networkEventReporter = mock(NetworkEventReporter.class);
Expand All @@ -302,10 +264,9 @@ public void testResponseWithoutContentLength() throws IOException {
NetworkInterceptor networkInterceptor = new NetworkInterceptor.Builder()
.setNetworkInterpreter(networkInterpreter)
.setEnabled(true)
.build(RuntimeEnvironment.application);
.build();

OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.networkInterceptors().add(networkInterceptor);
OkHttpClient okHttpClient = new OkHttpClient.Builder().addNetworkInterceptor(networkInterceptor).build();

byte[] uncompressedData = "Dummy Value".getBytes();
byte[] compressedData = compress(uncompressedData);
Expand All @@ -315,7 +276,7 @@ public void testResponseWithoutContentLength() throws IOException {

//removing content length from the response header
server.enqueue(new MockResponse()
.setChunkedBody(new Buffer().write(compressedData),3));
.setChunkedBody(new Buffer().write(compressedData), 3));

Request request = new Request.Builder()
.url(server.url("/"))
Expand All @@ -331,7 +292,9 @@ public void testResponseWithoutContentLength() throws IOException {
ArgumentCaptor<NetworkEventReporter.InspectorResponse> responseArgumentCaptor = ArgumentCaptor.forClass(NetworkEventReporter.InspectorResponse.class);
Mockito.verify(networkEventReporter, times(1))
.responseReceived(any(NetworkEventReporter.InspectorRequest.class), responseArgumentCaptor.capture());
Assert.assertTrue(responseArgumentCaptor.getValue().responseSize()==compressedData.length);

//assert that the bytes received from callback is same as compressed data
Assert.assertTrue(responseArgumentCaptor.getValue().responseSize() == compressedData.length);

server.shutdown();
}
Expand All @@ -353,14 +316,14 @@ public void testOkHttpInspectorRequest() throws Exception {
.addHeader("HOST", "flipkart")
.build();

DefaultInterpreter.OkHttpInspectorRequest okHttpInspectorRequest = new DefaultInterpreter.OkHttpInspectorRequest(1, request.url().url(), request.method(), OkHeaders.contentLength(request), request.header("HOST"));
DefaultInterpreter.OkHttpInspectorRequest okHttpInspectorRequest = new DefaultInterpreter.OkHttpInspectorRequest(1, request.url().url(), request.method(), Utils.contentLength(request), request.header("HOST"));

//assert id is same
Assert.assertTrue(okHttpInspectorRequest.requestId() == 1);
//assert url is same
Assert.assertTrue(okHttpInspectorRequest.url().equals(request.url()));
Assert.assertTrue(okHttpInspectorRequest.url().equals(request.url().url()));
//assert content length is same
Assert.assertTrue(okHttpInspectorRequest.requestSize() == OkHeaders.contentLength(request));
Assert.assertTrue(okHttpInspectorRequest.requestSize() == Utils.contentLength(request));
//assert hostname is same
Assert.assertTrue(okHttpInspectorRequest.hostName().equals(request.header("HOST")));
//assert method is same
Expand All @@ -385,7 +348,10 @@ public void testOkHttpInspectorResponse() throws Exception {
Assert.assertTrue(okHttpInspectorResponse.statusCode() == 200);
}

private static class CustomChain implements Interceptor.Chain {
/**
* Only for testing purposes.
*/
public static class CustomChain implements Interceptor.Chain {
private final Request mRequest;
private final Response mResponse;
@Nullable
Expand Down
Loading

0 comments on commit fb66c87

Please sign in to comment.