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();
}
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();
}
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;
}
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;
}
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;
}
Aggregations