Search in sources :

Example 16 with HttpResponse

use of com.microsoft.identity.common.internal.net.HttpResponse in project microsoft-authentication-library-common-for-android by AzureAD.

the class HttpRequestTest method testHttpMethodFailedWithSocketTimeoutRetrySucceed.

/**
 * Verify that the initial post request failed with {@link SocketTimeoutException} and retry
 * succeeds.
 */
private void testHttpMethodFailedWithSocketTimeoutRetrySucceed(HttpTestMethod method) throws Exception {
    // Set up two connections, the first is failed with SocketTimeout, the second one succeeds.
    final HttpURLConnection firstConnection = MockUtil.getMockedConnectionWithSocketTimeout();
    mockRequestBody(firstConnection);
    final HttpURLConnection secondConnection = MockUtil.getMockedConnectionWithSuccessResponse(getSuccessResponse());
    mockRequestBody(secondConnection);
    addMockedConnection(firstConnection);
    addMockedConnection(secondConnection);
    try {
        assertEquals(2, getMockedConnectionCountInQueue());
        final HttpResponse response = sendWithMethod(method);
        verifySuccessHttpResponse(response);
    } catch (final IOException e) {
        fail();
    }
    assertEquals(0, getMockedConnectionCountInQueue());
    final InOrder inOrder = Mockito.inOrder(firstConnection, secondConnection);
    inOrder.verify(firstConnection).getInputStream();
    inOrder.verify(firstConnection, Mockito.never()).getErrorStream();
    inOrder.verify(firstConnection, Mockito.never()).getResponseCode();
    inOrder.verify(secondConnection).getInputStream();
    inOrder.verify(secondConnection, Mockito.never()).getErrorStream();
    inOrder.verify(secondConnection).getResponseCode();
    inOrder.verify(secondConnection).getHeaderFields();
    inOrder.verifyNoMoreInteractions();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InOrder(org.mockito.InOrder) HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse) IOException(java.io.IOException)

Example 17 with HttpResponse

use of com.microsoft.identity.common.internal.net.HttpResponse in project microsoft-authentication-library-common-for-android by AzureAD.

the class HttpRequestTest method testHttpPostFailedWithSocketTimeoutRetryFailedWithNonRetryableCode.

/**
 * Verify that initial http post fails with {@link SocketTimeoutException}, retry fails with
 * {@link HttpURLConnection#HTTP_BAD_REQUEST}(non retryable status code).
 */
@Test
public void testHttpPostFailedWithSocketTimeoutRetryFailedWithNonRetryableCode() throws Exception {
    // The first connection fails with retryable SocketTimeout, the retry connection fails with 400.
    final HttpURLConnection firstConnection = MockUtil.getMockedConnectionWithSocketTimeout();
    mockRequestBody(firstConnection);
    final HttpURLConnection secondConnection = getMockedConnectionWithFailureResponse(HttpURLConnection.HTTP_BAD_REQUEST, getErrorResponse());
    mockRequestBody(secondConnection);
    addMockedConnection(firstConnection);
    addMockedConnection(secondConnection);
    try {
        assertEquals(2, getMockedConnectionCountInQueue());
        final HttpResponse response = sendWithMethod(HttpTestMethod.POST);
        assertNotNull(response);
        assertEquals(response.getStatusCode(), HttpURLConnection.HTTP_BAD_REQUEST);
        assertEquals(response.getBody(), getErrorResponse());
    } catch (final IOException e) {
        fail();
    }
    assertEquals(0, getMockedConnectionCountInQueue());
    final InOrder inOrder = Mockito.inOrder(firstConnection, secondConnection);
    inOrder.verify(firstConnection).getInputStream();
    inOrder.verify(firstConnection, Mockito.never()).getErrorStream();
    inOrder.verify(firstConnection, Mockito.never()).getResponseCode();
    inOrder.verify(secondConnection).getInputStream();
    inOrder.verify(secondConnection).getErrorStream();
    inOrder.verify(secondConnection).getResponseCode();
    inOrder.verify(secondConnection).getHeaderFields();
    inOrder.verifyNoMoreInteractions();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InOrder(org.mockito.InOrder) HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse) IOException(java.io.IOException) Test(org.junit.Test)

Example 18 with HttpResponse

use of com.microsoft.identity.common.internal.net.HttpResponse in project microsoft-authentication-library-common-for-android by AzureAD.

the class HttpRequestTest method testHttpMethodSucceed.

/**
 * Verify that when an HTTP method succeeds, no retry happens.
 */
private void testHttpMethodSucceed(HttpTestMethod method, boolean specific, boolean retries) throws Exception {
    // prepare the connection, only one connection will be made.
    final HttpURLConnection mockedSuccessConnection = MockUtil.getMockedConnectionWithSuccessResponse(getSuccessResponse());
    mockRequestBody(mockedSuccessConnection);
    addMockedConnection(mockedSuccessConnection);
    try {
        assertEquals(1, getMockedConnectionCountInQueue());
        final HttpResponse response = sendMethod(method, specific, retries);
        verifySuccessHttpResponse(response);
    } catch (final IOException e) {
        fail();
    }
    assertEquals(0, getMockedConnectionCountInQueue());
    final InOrder inOrder = Mockito.inOrder(mockedSuccessConnection);
    // default times for verify is 1.
    inOrder.verify(mockedSuccessConnection).getInputStream();
    inOrder.verify(mockedSuccessConnection, Mockito.never()).getErrorStream();
    inOrder.verify(mockedSuccessConnection).getResponseCode();
    inOrder.verify(mockedSuccessConnection).getHeaderFields();
    inOrder.verifyNoMoreInteractions();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InOrder(org.mockito.InOrder) HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse) IOException(java.io.IOException)

Example 19 with HttpResponse

use of com.microsoft.identity.common.internal.net.HttpResponse in project microsoft-authentication-library-common-for-android by AzureAD.

the class HttpRequestTest method testHttpPostFailedWithSocketTimeoutNoRetriesDoesNotRetry.

/**
 * Verify that initial http post fails with {@link SocketTimeoutException}, when we're not
 * running in legacy retry mode the socket timeout exception propagates to the caller.
 */
@Test(expected = SocketTimeoutException.class)
public void testHttpPostFailedWithSocketTimeoutNoRetriesDoesNotRetry() throws Exception {
    // The first connection fails with retryable SocketTimeout, the retry connection fails with 400.
    final HttpURLConnection firstConnection = MockUtil.getMockedConnectionWithSocketTimeout();
    mockRequestBody(firstConnection);
    final HttpURLConnection secondConnection = MockUtil.getMockedConnectionWithSocketTimeout();
    mockRequestBody(secondConnection);
    addMockedConnection(firstConnection);
    addMockedConnection(secondConnection);
    try {
        assertEquals(2, getMockedConnectionCountInQueue());
        final HttpResponse response = sendWithMethodNoRetry(HttpTestMethod.POST);
    } catch (final IOException e) {
        if (!(e instanceof SocketTimeoutException)) {
            fail();
        }
        throw e;
    } finally {
        assertEquals(1, getMockedConnectionCountInQueue());
        final InOrder inOrder = Mockito.inOrder(firstConnection, secondConnection);
        inOrder.verify(firstConnection).getInputStream();
        inOrder.verify(firstConnection, Mockito.never()).getErrorStream();
        inOrder.verify(firstConnection, Mockito.never()).getResponseCode();
        inOrder.verifyNoMoreInteractions();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) SocketTimeoutException(java.net.SocketTimeoutException) InOrder(org.mockito.InOrder) HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse) IOException(java.io.IOException) Test(org.junit.Test)

Example 20 with HttpResponse

use of com.microsoft.identity.common.internal.net.HttpResponse in project microsoft-authentication-library-common-for-android by AzureAD.

the class HttpRequestTest method sendMethodWithRetryableStatusCodeRetryFailsWithNonRetryableCode.

private void sendMethodWithRetryableStatusCodeRetryFailsWithNonRetryableCode(HttpTestMethod method) throws Exception {
    // The first connection fails with retryable status code 500, the retry connection fails with 401.
    final HttpURLConnection firstConnection = getMockedConnectionWithFailureResponse(HttpURLConnection.HTTP_INTERNAL_ERROR, getErrorResponse());
    mockRequestBody(firstConnection);
    final HttpURLConnection secondConnection = getMockedConnectionWithFailureResponse(HttpURLConnection.HTTP_UNAUTHORIZED, getErrorResponse());
    mockRequestBody(secondConnection);
    addMockedConnection(firstConnection);
    addMockedConnection(secondConnection);
    try {
        assertEquals(2, getMockedConnectionCountInQueue());
        final HttpResponse response = sendWithMethod(method);
        assertNotNull(response);
        assertEquals(response.getStatusCode(), HttpURLConnection.HTTP_UNAUTHORIZED);
        assertEquals(response.getBody(), getErrorResponse());
    } catch (final IOException e) {
        fail();
    }
    assertEquals(0, getMockedConnectionCountInQueue());
    final InOrder inOrder = Mockito.inOrder(firstConnection, secondConnection);
    inOrder.verify(firstConnection).getInputStream();
    inOrder.verify(firstConnection).getErrorStream();
    inOrder.verify(firstConnection).getResponseCode();
    inOrder.verify(secondConnection).getInputStream();
    inOrder.verify(secondConnection).getErrorStream();
    inOrder.verify(secondConnection).getResponseCode();
    inOrder.verify(secondConnection).getHeaderFields();
    inOrder.verifyNoMoreInteractions();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InOrder(org.mockito.InOrder) HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse) IOException(java.io.IOException)

Aggregations

HttpResponse (com.microsoft.identity.common.internal.net.HttpResponse)29 IOException (java.io.IOException)10 HttpURLConnection (java.net.HttpURLConnection)10 InOrder (org.mockito.InOrder)10 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Test (org.junit.Test)5 MicrosoftTokenErrorResponse (com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenErrorResponse)4 URL (java.net.URL)3 MicrosoftTokenResponse (com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenResponse)2 TokenResponse (com.microsoft.identity.common.internal.providers.oauth2.TokenResponse)2 TokenResult (com.microsoft.identity.common.internal.providers.oauth2.TokenResult)2 MockTokenResponse (com.microsoft.identity.internal.testutils.mocks.MockTokenResponse)2 HashMap (java.util.HashMap)2 TreeMap (java.util.TreeMap)2 Uri (android.net.Uri)1 Gson (com.google.gson.Gson)1 TypeToken (com.google.gson.reflect.TypeToken)1 ServiceException (com.microsoft.identity.common.exception.ServiceException)1 MicrosoftTokenRequest (com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenRequest)1