Search in sources :

Example 1 with HttpResponse

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

the class HttpResponseTest method testHttpResponseWithEmptyBody.

@Test
public void testHttpResponseWithEmptyBody() {
    final HttpResponse response = new HttpResponse(HttpURLConnection.HTTP_OK, "", Collections.<String, List<String>>emptyMap());
    Assert.assertNotNull(response.getBody());
    Assert.assertTrue(response.getBody().isEmpty());
}
Also used : HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse) Test(org.junit.Test)

Example 2 with HttpResponse

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

the class HttpResponseTest method testHttpResponseWithNullBody.

@Test
public void testHttpResponseWithNullBody() {
    final HttpResponse response = new HttpResponse(HttpURLConnection.HTTP_OK, null, Collections.<String, List<String>>emptyMap());
    Assert.assertNull(response.getBody());
    Assert.assertTrue(response.getStatusCode() == HttpURLConnection.HTTP_OK);
    Assert.assertTrue(response.getHeaders().isEmpty());
}
Also used : HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse) Test(org.junit.Test)

Example 3 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 testHttpMethodFailedWithStatusCodeRetryFails.

/**
 * Verify the correct response is returned if first network call fails with internal error,
 * and retry succeeds.
 */
private void testHttpMethodFailedWithStatusCodeRetryFails(HttpTestMethod method, int code) throws Exception {
    // Set up three connections, all with the specified code. all failures.
    final HttpURLConnection firstConnection = getMockedConnectionWithFailureResponse(code, getErrorResponse());
    mockRequestBody(firstConnection);
    final HttpURLConnection secondConnection = getMockedConnectionWithFailureResponse(code, getErrorResponse());
    mockRequestBody(secondConnection);
    final HttpURLConnection thirdConnection = getMockedConnectionWithFailureResponse(code, getErrorResponse());
    mockRequestBody(thirdConnection);
    addMockedConnection(firstConnection);
    addMockedConnection(secondConnection);
    addMockedConnection(thirdConnection);
    assertEquals(3, getMockedConnectionCountInQueue());
    try {
        final HttpResponse response = sendWithMethod(method);
        fail();
    } finally {
        assertEquals(1, getMockedConnectionCountInQueue());
        final InOrder inOrder = Mockito.inOrder(firstConnection, secondConnection);
        inOrder.verify(firstConnection).getInputStream();
        inOrder.verify(firstConnection).getErrorStream();
        inOrder.verify(firstConnection).getResponseCode();
        inOrder.verify(firstConnection).getDate();
        inOrder.verify(firstConnection).getHeaderFields();
        inOrder.verify(secondConnection).getInputStream();
        inOrder.verify(secondConnection).getErrorStream();
        inOrder.verify(secondConnection).getResponseCode();
        inOrder.verify(secondConnection).getDate();
        inOrder.verify(secondConnection).getHeaderFields();
        inOrder.verifyNoMoreInteractions();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InOrder(org.mockito.InOrder) HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse)

Example 4 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 testHttpMethodFailedNoRetry.

/**
 * Verify that http request fails with {@link HttpURLConnection#HTTP_UNAUTHORIZED},
 * no retry happens.
 */
private void testHttpMethodFailedNoRetry(HttpTestMethod method, int code) throws Exception {
    final HttpURLConnection mockedFailureConnection = getMockedConnectionWithFailureResponse(code, getErrorResponse());
    mockRequestBody(mockedFailureConnection);
    addMockedConnection(mockedFailureConnection);
    // send a post request
    try {
        assertEquals(1, getMockedConnectionCountInQueue());
        final HttpResponse response = sendWithMethod(method);
        assertNotNull(response);
        assertEquals(response.getStatusCode(), code);
        assertEquals(response.getBody(), getErrorResponse());
    } catch (final IOException e) {
        fail();
    }
    assertEquals(0, getMockedConnectionCountInQueue());
    final InOrder inOrder = Mockito.inOrder(mockedFailureConnection);
    inOrder.verify(mockedFailureConnection).getInputStream();
    inOrder.verify(mockedFailureConnection).getErrorStream();
    inOrder.verify(mockedFailureConnection).getResponseCode();
    inOrder.verify(mockedFailureConnection).getDate();
    inOrder.verify(mockedFailureConnection).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 5 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 testHttpMethodFailedNoRetryNoResponseBody.

/**
 * Verify that http get request fails with {@link HttpURLConnection#HTTP_BAD_METHOD}
 * and no response body, no retry happens.
 */
public void testHttpMethodFailedNoRetryNoResponseBody(HttpTestMethod method) throws Exception {
    final HttpURLConnection mockedFailureConnection = getMockedConnectionWithFailureResponse(HttpURLConnection.HTTP_BAD_METHOD, null);
    addMockedConnection(mockedFailureConnection);
    try {
        assertEquals(1, getMockedConnectionCountInQueue());
        final HttpResponse response = sendWithMethod(method);
        assertNotNull(response);
        assertEquals(response.getStatusCode(), HttpURLConnection.HTTP_BAD_METHOD);
        assertTrue(response.getBody().isEmpty());
    } catch (final IOException e) {
        fail();
    }
    assertEquals(0, getMockedConnectionCountInQueue());
    final InOrder inOrder = Mockito.inOrder(mockedFailureConnection);
    inOrder.verify(mockedFailureConnection).getInputStream();
    inOrder.verify(mockedFailureConnection).getErrorStream();
    inOrder.verify(mockedFailureConnection).getResponseCode();
    inOrder.verify(mockedFailureConnection).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