@Test public void testResponseReceivedCalledForEventReporter() throws Exception { NetworkEventReporter networkEventReporter = mock(NetworkEventReporter.class); DefaultInterpreter defaultInterpreter = new DefaultInterpreter(networkEventReporter); NetworkInterceptor.TimeInfo info = new NetworkInterceptor.TimeInfo(); info.mStartTime = 10; info.mEndTime = 20; Request request = new Request.Builder() .url("http: .build(); Response response = new Response.Builder() .code(200) .request(request) .message("") .protocol(Protocol.HTTP_1_1) .addHeader("Content-Length", "20") .build(); defaultInterpreter.interpretResponseStream(1, info, request, response); verify(networkEventReporter, times(1)).responseReceived(any(NetworkEventReporter.InspectorRequest.class), any(NetworkEventReporter.InspectorResponse.class)); }
@Override public Response interpretResponseStream(int requestId, NetworkInterceptor.TimeInfo timeInfo, Request request, Response response) { ResponseBody responseBody = response.body(); final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request.headers()), request.url().host(), request.body()); final OkHttpInspectorResponse okHttpInspectorResponse = new OkHttpInspectorResponse(requestId, response.code(), Utils.contentLength(response.headers()), timeInfo.mStartTime, timeInfo.mEndTime, responseBody); if (response.header(CONTENT_LENGTH) == null) { InputStream responseStream = null; if (responseBody != null) { try { responseStream = responseBody.byteStream(); } catch (Exception e) { if (Utils.isLoggingEnabled) { Log.d("Error reading IS : ", e.getMessage()); } mEventReporter.responseInputStreamError(okHttpInspectorRequest, okHttpInspectorResponse, e); throw e; } } responseStream = new CountingInputStream(responseStream, new DefaultResponseHandler(new DefaultResponseHandler.ResponseCallback() { @Override public void onEOF(long bytesRead) { okHttpInspectorResponse.mResponseSize = bytesRead; mEventReporter.responseReceived(okHttpInspectorRequest, okHttpInspectorResponse); } })); response = response.newBuilder().body(new ForwardingResponseBody(responseBody, responseStream)).build(); } else { mEventReporter.responseReceived(okHttpInspectorRequest, okHttpInspectorResponse); } return response; }
DefaultInterpreter implements NetworkInterpreter { @Override public Response interpretResponseStream(int requestId, NetworkInterceptor.TimeInfo timeInfo, Request request, Response response) { ResponseBody responseBody = response.body(); final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request.headers()), request.url().host(), request.body()); final OkHttpInspectorResponse okHttpInspectorResponse = new OkHttpInspectorResponse(requestId, response.code(), Utils.contentLength(response.headers()), timeInfo.mStartTime, timeInfo.mEndTime, responseBody); if (response.header(CONTENT_LENGTH) == null) { InputStream responseStream = null; if (responseBody != null) { try { responseStream = responseBody.byteStream(); } catch (Exception e) { if (Utils.isLoggingEnabled) { Log.d("Error reading IS : ", e.getMessage()); } mEventReporter.responseInputStreamError(okHttpInspectorRequest, okHttpInspectorResponse, e); throw e; } } responseStream = new CountingInputStream(responseStream, new DefaultResponseHandler(new DefaultResponseHandler.ResponseCallback() { @Override public void onEOF(long bytesRead) { okHttpInspectorResponse.mResponseSize = bytesRead; mEventReporter.responseReceived(okHttpInspectorRequest, okHttpInspectorResponse); } })); response = response.newBuilder().body(new ForwardingResponseBody(responseBody, responseStream)).build(); } else { mEventReporter.responseReceived(okHttpInspectorRequest, okHttpInspectorResponse); } return response; } }
DefaultInterpreter implements NetworkInterpreter { @Override public Response interpretResponseStream(int requestId, NetworkInterceptor.TimeInfo timeInfo, Request request, Response response) { ResponseBody responseBody = response.body(); final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request.headers()), request.url().host(), request.body()); final OkHttpInspectorResponse okHttpInspectorResponse = new OkHttpInspectorResponse(requestId, response.code(), Utils.contentLength(response.headers()), timeInfo.mStartTime, timeInfo.mEndTime, responseBody); if (response.header(CONTENT_LENGTH) == null) { InputStream responseStream = null; if (responseBody != null) { try { responseStream = responseBody.byteStream(); } catch (Exception e) { if (Utils.isLoggingEnabled) { Log.d("Error reading IS : ", e.getMessage()); } mEventReporter.responseInputStreamError(okHttpInspectorRequest, okHttpInspectorResponse, e); throw e; } } responseStream = new CountingInputStream(responseStream, new DefaultResponseHandler(new DefaultResponseHandler.ResponseCallback() { @Override public void onEOF(long bytesRead) { okHttpInspectorResponse.mResponseSize = bytesRead; mEventReporter.responseReceived(okHttpInspectorRequest, okHttpInspectorResponse); } })); response = response.newBuilder().body(new ForwardingResponseBody(responseBody, responseStream)).build(); } else { mEventReporter.responseReceived(okHttpInspectorRequest, okHttpInspectorResponse); } return response; } DefaultInterpreter(NetworkEventReporter mEventReporter); }
DefaultInterpreter implements NetworkInterpreter { @Override public Response interpretResponseStream(int requestId, NetworkInterceptor.TimeInfo timeInfo, Request request, Response response) { ResponseBody responseBody = response.body(); final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request.headers()), request.url().host(), request.body()); final OkHttpInspectorResponse okHttpInspectorResponse = new OkHttpInspectorResponse(requestId, response.code(), Utils.contentLength(response.headers()), timeInfo.mStartTime, timeInfo.mEndTime, responseBody); if (response.header(CONTENT_LENGTH) == null) { InputStream responseStream = null; if (responseBody != null) { try { responseStream = responseBody.byteStream(); } catch (Exception e) { if (Utils.isLoggingEnabled) { Log.d("Error reading IS : ", e.getMessage()); } mEventReporter.responseInputStreamError(okHttpInspectorRequest, okHttpInspectorResponse, e); throw e; } } responseStream = new CountingInputStream(responseStream, new DefaultResponseHandler(new DefaultResponseHandler.ResponseCallback() { @Override public void onEOF(long bytesRead) { okHttpInspectorResponse.mResponseSize = bytesRead; mEventReporter.responseReceived(okHttpInspectorRequest, okHttpInspectorResponse); } })); response = response.newBuilder().body(new ForwardingResponseBody(responseBody, responseStream)).build(); } else { mEventReporter.responseReceived(okHttpInspectorRequest, okHttpInspectorResponse); } return response; } DefaultInterpreter(NetworkEventReporter mEventReporter); @Override Response interpretResponseStream(int requestId, NetworkInterceptor.TimeInfo timeInfo, Request request, Response response); @Override void interpretError(int requestId, NetworkInterceptor.TimeInfo timeInfo, Request request, IOException e); }
DefaultInterpreter implements NetworkInterpreter { @Override public Response interpretResponseStream(int requestId, NetworkInterceptor.TimeInfo timeInfo, Request request, Response response) { ResponseBody responseBody = response.body(); final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request.headers()), request.url().host(), request.body()); final OkHttpInspectorResponse okHttpInspectorResponse = new OkHttpInspectorResponse(requestId, response.code(), Utils.contentLength(response.headers()), timeInfo.mStartTime, timeInfo.mEndTime, responseBody); if (response.header(CONTENT_LENGTH) == null) { InputStream responseStream = null; if (responseBody != null) { try { responseStream = responseBody.byteStream(); } catch (Exception e) { if (Utils.isLoggingEnabled) { Log.d("Error reading IS : ", e.getMessage()); } mEventReporter.responseInputStreamError(okHttpInspectorRequest, okHttpInspectorResponse, e); throw e; } } responseStream = new CountingInputStream(responseStream, new DefaultResponseHandler(new DefaultResponseHandler.ResponseCallback() { @Override public void onEOF(long bytesRead) { okHttpInspectorResponse.mResponseSize = bytesRead; mEventReporter.responseReceived(okHttpInspectorRequest, okHttpInspectorResponse); } })); response = response.newBuilder().body(new ForwardingResponseBody(responseBody, responseStream)).build(); } else { mEventReporter.responseReceived(okHttpInspectorRequest, okHttpInspectorResponse); } return response; } DefaultInterpreter(NetworkEventReporter mEventReporter); @Override Response interpretResponseStream(int requestId, NetworkInterceptor.TimeInfo timeInfo, Request request, Response response); @Override void interpretError(int requestId, NetworkInterceptor.TimeInfo timeInfo, Request request, IOException e);  }