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