Search in sources :

Example 6 with NetworkRequestMetric

use of com.google.firebase.perf.v1.NetworkRequestMetric in project firebase-android-sdk by firebase.

the class InstrURLConnectionBaseTest method testGetContentClassesThrowsIOException.

@Test
public void testGetContentClassesThrowsIOException() throws IOException {
    HttpURLConnection urlConnection = mockHttpUrlConnection();
    when(urlConnection.getDoOutput()).thenReturn(true);
    @SuppressWarnings("rawtypes") Class[] classes = { TransportManager.class };
    doThrow(IOException.class).when(urlConnection).getContent(classes);
    assertThrows(IOException.class, () -> new InstrURLConnectionBase(urlConnection, timer, networkMetricBuilder).getContent(classes));
    verify(urlConnection).getContent(classes);
    verify(transportManager).log(networkArgumentCaptor.capture(), ArgumentMatchers.any(ApplicationProcessState.class));
    NetworkRequestMetric metric = networkArgumentCaptor.getValue();
    assertThat(metric.getTimeToResponseCompletedUs()).isEqualTo(2000);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) NetworkRequestMetric(com.google.firebase.perf.v1.NetworkRequestMetric) Test(org.junit.Test)

Example 7 with NetworkRequestMetric

use of com.google.firebase.perf.v1.NetworkRequestMetric in project firebase-android-sdk by firebase.

the class InstrURLConnectionBaseTest method testGetOutputStreamThrowsIOException.

@Test
public void testGetOutputStreamThrowsIOException() throws IOException {
    HttpURLConnection urlConnection = mockHttpUrlConnection();
    doThrow(IOException.class).when(urlConnection).getOutputStream();
    assertThrows(IOException.class, () -> new InstrURLConnectionBase(urlConnection, timer, networkMetricBuilder).getOutputStream());
    verify(transportManager).log(networkArgumentCaptor.capture(), ArgumentMatchers.any(ApplicationProcessState.class));
    NetworkRequestMetric metric = networkArgumentCaptor.getValue();
    assertThat(metric.getTimeToResponseCompletedUs()).isEqualTo(2000);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) NetworkRequestMetric(com.google.firebase.perf.v1.NetworkRequestMetric) Test(org.junit.Test)

Example 8 with NetworkRequestMetric

use of com.google.firebase.perf.v1.NetworkRequestMetric in project firebase-android-sdk by firebase.

the class InstrURLConnectionBaseTest method testGetResponseCodeThrowsIOException.

@Test
public void testGetResponseCodeThrowsIOException() throws IOException {
    HttpURLConnection urlConnection = mockHttpUrlConnection();
    doThrow(IOException.class).when(urlConnection).getResponseCode();
    assertThrows(IOException.class, () -> new InstrURLConnectionBase(urlConnection, timer, networkMetricBuilder).getResponseCode());
    verify(transportManager).log(networkArgumentCaptor.capture(), ArgumentMatchers.any(ApplicationProcessState.class));
    NetworkRequestMetric metric = networkArgumentCaptor.getValue();
    assertThat(metric.getTimeToResponseCompletedUs()).isEqualTo(2000);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) NetworkRequestMetric(com.google.firebase.perf.v1.NetworkRequestMetric) Test(org.junit.Test)

Example 9 with NetworkRequestMetric

use of com.google.firebase.perf.v1.NetworkRequestMetric in project firebase-android-sdk by firebase.

the class InstrumentOkHttpEnqueueCallbackTest method testOnResponse.

@Test
public void testOnResponse() throws IOException {
    String urlStr = "www.google.com";
    String scheme = "https";
    String fullUrlStr = "https://www.google.com/";
    String method = "POST";
    long requestContentLength = 240;
    long responseContentLength = 353;
    int responseCode = 300;
    String message = "";
    long startTimeMicros = 1;
    HttpUrl url = new HttpUrl.Builder().scheme(scheme).host(urlStr).build();
    RequestBody requestBody = mock(RequestBody.class);
    when(requestBody.contentLength()).thenReturn(requestContentLength);
    Request request = new Request.Builder().url(url).method(method, requestBody).build();
    ResponseBody responseBody = mock(ResponseBody.class);
    when(responseBody.contentLength()).thenReturn(responseContentLength);
    Response response = new Response.Builder().code(responseCode).message(message).body(responseBody).request(request).protocol(Protocol.HTTP_2).build();
    TransportManager transportManager = mock(TransportManager.class);
    Callback callback = mock(Callback.class);
    Call call = mock(Call.class);
    InstrumentOkHttpEnqueueCallback enqueueCallback = new InstrumentOkHttpEnqueueCallback(callback, transportManager, mockTimer(), startTimeMicros);
    enqueueCallback.onResponse(call, response);
    ArgumentCaptor<NetworkRequestMetric> argument = ArgumentCaptor.forClass(NetworkRequestMetric.class);
    verify(transportManager).log(argument.capture(), ArgumentMatchers.any(ApplicationProcessState.class));
    verify(callback).onResponse(call, response);
    NetworkRequestMetric metric = argument.getValue();
    assertEquals(fullUrlStr, metric.getUrl());
    assertEquals(HttpMethod.POST, metric.getHttpMethod());
    assertEquals(requestContentLength, metric.getRequestPayloadBytes());
    assertEquals(responseContentLength, metric.getResponsePayloadBytes());
    assertEquals(responseCode, metric.getHttpResponseCode());
    assertEquals(startTimeMicros, metric.getClientStartTimeUs());
    assertEquals(2000, metric.getTimeToResponseCompletedUs());
}
Also used : Call(okhttp3.Call) ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) NetworkRequestMetric(com.google.firebase.perf.v1.NetworkRequestMetric) Request(okhttp3.Request) HttpUrl(okhttp3.HttpUrl) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) Callback(okhttp3.Callback) TransportManager(com.google.firebase.perf.transport.TransportManager) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 10 with NetworkRequestMetric

use of com.google.firebase.perf.v1.NetworkRequestMetric in project firebase-android-sdk by firebase.

the class InstrumentOkHttpEnqueueCallbackTest method testOnFailure.

@Test
public void testOnFailure() throws IOException {
    String urlStr = "www.google.com";
    String scheme = "https";
    String fullUrlStr = "https://www.google.com/";
    String method = "POST";
    HttpUrl url = new HttpUrl.Builder().scheme(scheme).host(urlStr).build();
    RequestBody requestBody = mock(RequestBody.class);
    TransportManager transportManager = mock(TransportManager.class);
    Callback callback = mock(Callback.class);
    Call call = mock(Call.class);
    Request request = new Request.Builder().url(url).method(method, requestBody).build();
    when(call.request()).thenReturn(request);
    IOException e = new IOException();
    long startTime = 1;
    InstrumentOkHttpEnqueueCallback enqueueCallback = new InstrumentOkHttpEnqueueCallback(callback, transportManager, mockTimer(), startTime);
    enqueueCallback.onFailure(call, e);
    ArgumentCaptor<NetworkRequestMetric> argument = ArgumentCaptor.forClass(NetworkRequestMetric.class);
    verify(transportManager).log(argument.capture(), ArgumentMatchers.any(ApplicationProcessState.class));
    verify(callback).onFailure(call, e);
    NetworkRequestMetric metric = argument.getValue();
    assertEquals(fullUrlStr, metric.getUrl());
    assertEquals(HttpMethod.POST, metric.getHttpMethod());
    assertEquals(NetworkClientErrorReason.GENERIC_CLIENT_ERROR, metric.getNetworkClientErrorReason());
    assertEquals(startTime, metric.getClientStartTimeUs());
    assertEquals(2000, metric.getTimeToResponseCompletedUs());
}
Also used : Call(okhttp3.Call) ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) NetworkRequestMetric(com.google.firebase.perf.v1.NetworkRequestMetric) Request(okhttp3.Request) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Callback(okhttp3.Callback) TransportManager(com.google.firebase.perf.transport.TransportManager) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)81 NetworkRequestMetric (com.google.firebase.perf.v1.NetworkRequestMetric)80 ApplicationProcessState (com.google.firebase.perf.v1.ApplicationProcessState)45 NetworkRequestMetricBuilder (com.google.firebase.perf.metrics.NetworkRequestMetricBuilder)13 IOException (java.io.IOException)13 HttpResponse (org.apache.http.HttpResponse)13 HttpClient (org.apache.http.client.HttpClient)12 HttpURLConnection (java.net.HttpURLConnection)11 PerfMetric (com.google.firebase.perf.v1.PerfMetric)10 HttpHost (org.apache.http.HttpHost)6 HttpRequest (org.apache.http.HttpRequest)6 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)6 HttpContext (org.apache.http.protocol.HttpContext)6 HttpUrl (okhttp3.HttpUrl)4 Request (okhttp3.Request)4 RequestBody (okhttp3.RequestBody)4 TransportManager (com.google.firebase.perf.transport.TransportManager)3 URLWrapper (com.google.firebase.perf.util.URLWrapper)3 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)3 Response (okhttp3.Response)3