Search in sources :

Example 6 with TransportManager

use of com.google.firebase.perf.transport.TransportManager in project firebase-android-sdk by firebase.

the class InstrumentApacheHttpResponseHandlerTest method testHandleResponse.

@Test
public void testHandleResponse() throws IOException {
    // mocks what an app developer would use.  Need to verify that the app's response handler
    // is also called
    ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

        @Override
        public String handleResponse(HttpResponse httpResponse) throws IOException {
            return "testString";
        }
    };
    TransportManager transportManager = mock(TransportManager.class);
    NetworkRequestMetricBuilder builder = NetworkRequestMetricBuilder.builder(transportManager);
    InstrumentApacheHttpResponseHandler<String> instrumentResponseHandler = new InstrumentApacheHttpResponseHandler<>(responseHandler, mockTimer(), builder);
    String response = instrumentResponseHandler.handleResponse(mockHttpResponse());
    // Verify that TransportManager is called with correct argument
    ArgumentCaptor<NetworkRequestMetric> argument = ArgumentCaptor.forClass(NetworkRequestMetric.class);
    verify(transportManager, times(1)).log(argument.capture(), ArgumentMatchers.any(ApplicationProcessState.class));
    NetworkRequestMetric metric = argument.getValue();
    assertThat(metric.getHttpResponseCode()).isEqualTo(200);
    assertThat(metric.getTimeToResponseCompletedUs()).isEqualTo(2000);
    assertThat(metric.getResponseContentType()).isEqualTo("text/html");
    assertThat(metric.getResponsePayloadBytes()).isEqualTo(256);
    // Verify that the app developer's response handler is also called
    assertThat(response).isEqualTo("testString");
}
Also used : ResponseHandler(org.apache.http.client.ResponseHandler) ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) NetworkRequestMetric(com.google.firebase.perf.v1.NetworkRequestMetric) HttpResponse(org.apache.http.HttpResponse) TransportManager(com.google.firebase.perf.transport.TransportManager) NetworkRequestMetricBuilder(com.google.firebase.perf.metrics.NetworkRequestMetricBuilder) Test(org.junit.Test)

Aggregations

TransportManager (com.google.firebase.perf.transport.TransportManager)6 ApplicationProcessState (com.google.firebase.perf.v1.ApplicationProcessState)5 Test (org.junit.Test)4 NetworkRequestMetric (com.google.firebase.perf.v1.NetworkRequestMetric)3 NetworkRequestMetricBuilder (com.google.firebase.perf.metrics.NetworkRequestMetricBuilder)2 TraceMetric (com.google.firebase.perf.v1.TraceMetric)2 Call (okhttp3.Call)2 Callback (okhttp3.Callback)2 HttpUrl (okhttp3.HttpUrl)2 Request (okhttp3.Request)2 RequestBody (okhttp3.RequestBody)2 AppStateMonitor (com.google.firebase.perf.application.AppStateMonitor)1 Trace (com.google.firebase.perf.metrics.Trace)1 GaugeManager (com.google.firebase.perf.session.gauges.GaugeManager)1 Timer (com.google.firebase.perf.util.Timer)1 IOException (java.io.IOException)1 Response (okhttp3.Response)1 ResponseBody (okhttp3.ResponseBody)1 HttpResponse (org.apache.http.HttpResponse)1 ResponseHandler (org.apache.http.client.ResponseHandler)1