Search in sources :

Example 6 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 testHttpMethodFailedWithStatusCodeRetrySucceed.

/**
 * Verify the correct response is returned if first network call fails with internal error,
 * and retry succeeds.
 */
private void testHttpMethodFailedWithStatusCodeRetrySucceed(HttpTestMethod method, int code) throws Exception {
    // Set up two connections, the first is failed with 500, the second one succeeds.
    final HttpURLConnection firstConnection = getMockedConnectionWithFailureResponse(code, getErrorResponse());
    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).getErrorStream();
    inOrder.verify(firstConnection).getResponseCode();
    // no HttpResponse is created, no need to verify getHeaderFields.
    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 7 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 testHttpMethodFailedWithStatusCodeWithoutRetryNoRetryHappens.

private void testHttpMethodFailedWithStatusCodeWithoutRetryNoRetryHappens(HttpTestMethod method, int code) throws Exception {
    // Set up two connections, the first is failed with 500, the second one does not occur.
    final HttpURLConnection firstConnection = getMockedConnectionWithFailureResponse(code, getErrorResponse());
    mockRequestBody(firstConnection);
    final HttpURLConnection secondConnection = MockUtil.getMockedConnectionWithSuccessResponse(getSuccessResponse());
    mockRequestBody(secondConnection);
    addMockedConnection(firstConnection);
    addMockedConnection(secondConnection);
    try {
        assertEquals(2, getMockedConnectionCountInQueue());
        final HttpResponse response = sendMethod(method, false, false);
        assertEquals(code, response.getStatusCode());
    } catch (final IOException e) {
        fail();
    }
    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.verifyNoMoreInteractions();
    clearMockedConnectionQueue();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InOrder(org.mockito.InOrder) HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse) IOException(java.io.IOException)

Example 8 with HttpResponse

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

the class MockTestStrategy method performTokenRequest.

@Override
protected HttpResponse performTokenRequest(final MicrosoftStsTokenRequest tokenRequest) {
    final TokenResult tokenResult = getTokenResult();
    final TokenResponse tokenResponse = tokenResult.getTokenResponse();
    final HttpResponse httpResponse = makeHttpResponseFromResponseObject(tokenResponse);
    return httpResponse;
}
Also used : TokenResponse(com.microsoft.identity.common.internal.providers.oauth2.TokenResponse) MockTokenResponse(com.microsoft.identity.internal.testutils.mocks.MockTokenResponse) TokenResult(com.microsoft.identity.common.internal.providers.oauth2.TokenResult) HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse)

Example 9 with HttpResponse

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

the class MockTestStrategy method makeHttpResponseFromResponseObject.

public HttpResponse makeHttpResponseFromResponseObject(final Object obj) {
    final String httpResponseBody = ObjectMapper.serializeObjectToJsonString(obj);
    final HashMap<String, List<String>> responseHeaders = new HashMap<>();
    responseHeaders.put(AuthenticationConstants.HeaderField.X_MS_CLITELEM, new ArrayList<>(Collections.singleton("1,0,0,,")));
    final HttpResponse httpResponse = new HttpResponse(200, httpResponseBody, responseHeaders);
    return httpResponse;
}
Also used : HashMap(java.util.HashMap) HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse) ArrayList(java.util.ArrayList) List(java.util.List)

Example 10 with HttpResponse

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

the class MockServerResponse method getMockTokenFailureInvalidScopeResponse.

public static HttpResponse getMockTokenFailureInvalidScopeResponse() {
    final MicrosoftTokenErrorResponse tokenErrorResponse = new MicrosoftTokenErrorResponse();
    tokenErrorResponse.setError("invalid_scope");
    tokenErrorResponse.setErrorDescription("AADSTS70000: Provided scope is invalid or malformed");
    tokenErrorResponse.setErrorCodes(new ArrayList<Long>(Arrays.asList(70000L)));
    tokenErrorResponse.setTimeStamp("2019-10-23 21:05:16Z");
    tokenErrorResponse.setTraceId("8497799a-e9f9-402f-a951-7060b5014600");
    tokenErrorResponse.setCorrelationId("390d7507-c607-4f05-bb8a-51a2a7a6282b");
    tokenErrorResponse.setErrorUri("https://login.microsoftonline.com/error?code=70000");
    tokenErrorResponse.setSubError("bad_token");
    final String mockResponse = ObjectMapper.serializeObjectToJsonString(tokenErrorResponse);
    final HttpResponse response = new HttpResponse(400, mockResponse, new HashMap<String, List<String>>());
    return response;
}
Also used : MicrosoftTokenErrorResponse(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenErrorResponse) HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse) List(java.util.List) ArrayList(java.util.ArrayList)

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