use of com.microsoft.identity.common.adal.internal.net.WebRequestHandler in project azure-activedirectory-library-for-android by AzureAD.
the class OauthTests method testProcessTokenResponseNegative.
@Test
public void testProcessTokenResponseNegative() throws IOException {
final AuthenticationRequest request = createAuthenticationRequest(TEST_AUTHORITY, "resource", "client", "redirect", "loginhint", null, null, UUID.randomUUID(), false);
final Oauth2 oauth2 = createOAuthInstance(request, new WebRequestHandler());
final String jsonResponse = "{invalid";
final HttpURLConnection mockedConnection = mock(HttpURLConnection.class);
HttpUrlConnectionFactory.setMockedHttpUrlConnection(mockedConnection);
Util.prepareMockedUrlConnection(mockedConnection);
when(mockedConnection.getOutputStream()).thenReturn(mock(OutputStream.class));
when(mockedConnection.getInputStream()).thenReturn(Util.createInputStream(jsonResponse));
when(mockedConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
// send call with mocks
try {
oauth2.refreshToken("fakeRefreshToken");
fail("must throw exception");
} catch (final AuthenticationException e) {
assertNotNull(e);
assertEquals(e.getCode(), ADALError.SERVER_INVALID_JSON_RESPONSE);
assertNotNull(e.getCause());
assertTrue(e.getCause() instanceof JSONException);
}
}
use of com.microsoft.identity.common.adal.internal.net.WebRequestHandler in project azure-activedirectory-library-for-android by AzureAD.
the class OauthTests method testProcessTokenResponse.
@Test
public void testProcessTokenResponse() throws IOException {
final AuthenticationRequest request = createAuthenticationRequest(TEST_AUTHORITY, "resource", "client", "redirect", "loginhint", null, null, UUID.randomUUID(), false);
final Oauth2 oauth2 = createOAuthInstance(request, new WebRequestHandler());
final String idToken = Util.getIdToken();
final String jsonResponse = "{\"id_token\":\"" + idToken + "\",\"access_token\":\"sometokenhere2343=\",\"token_type\":\"Bearer\"," + "\"expires_in\":\"28799\",\"expires_on\":\"1368768616\",\"refresh_token\":" + "\"refreshfasdfsdf435=\",\"scope\":\"*\"}";
// mock token request response
final HttpURLConnection mockedConnection = mock(HttpURLConnection.class);
HttpUrlConnectionFactory.setMockedHttpUrlConnection(mockedConnection);
Util.prepareMockedUrlConnection(mockedConnection);
when(mockedConnection.getOutputStream()).thenReturn(mock(OutputStream.class));
when(mockedConnection.getInputStream()).thenReturn(Util.createInputStream(jsonResponse));
when(mockedConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
// send call with mocks
try {
final AuthenticationResult result = oauth2.refreshToken("fakeRT");
// verify same token
assertEquals("Same token in parsed result", "sometokenhere2343=", result.getAccessToken());
assertEquals("Same refresh token in parsed result", "refreshfasdfsdf435=", result.getRefreshToken());
assertEquals("Same rawIdToken", idToken, result.getIdToken());
} catch (final AuthenticationException e) {
fail("Unexpected Exception");
}
}
use of com.microsoft.identity.common.adal.internal.net.WebRequestHandler in project azure-activedirectory-library-for-android by AzureAD.
the class WebRequestHandlerTests method testGetRequest.
@Test
public void testGetRequest() throws IOException {
Logger.d(TAG, "test get" + android.os.Process.myTid());
final HttpURLConnection mockedConnection = Mockito.mock(HttpURLConnection.class);
HttpUrlConnectionFactory.setMockedHttpUrlConnection(mockedConnection);
Util.prepareMockedUrlConnection(mockedConnection);
Mockito.when(mockedConnection.getInputStream()).thenReturn(Util.createInputStream("testabc-value123"));
Mockito.when(mockedConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
final WebRequestHandler request = new WebRequestHandler();
HttpWebResponse httpResponse = request.sendGet(getUrl(TEST_WEBAPI_URL), getTestHeaders("testabc", "value123"));
assertNotNull(httpResponse != null);
assertTrue("status is 200", httpResponse.getStatusCode() == HttpURLConnection.HTTP_OK);
String responseMsg = httpResponse.getBody();
assertTrue("request header check", responseMsg.contains("testabc-value123"));
}
use of com.microsoft.identity.common.adal.internal.net.WebRequestHandler in project azure-activedirectory-library-for-android by AzureAD.
the class WebRequestHandlerTests method sendCorrelationIdRequest.
private HttpWebResponse sendCorrelationIdRequest(final String message, final UUID testID, final boolean withoutHeader) throws IOException {
Logger.d(TAG, "test get" + android.os.Process.myTid());
WebRequestHandler request = new WebRequestHandler();
request.setRequestCorrelationId(testID);
final Map<String, String> headers = new HashMap<>();
if (!withoutHeader) {
headers.put("Accept", "application/json");
}
return request.sendPost(getUrl(message), headers, null, "application/x-www-form-urlencoded");
}
use of com.microsoft.identity.common.adal.internal.net.WebRequestHandler in project azure-activedirectory-library-for-android by AzureAD.
the class WebRequestHandlerTests method testClientTraceInHeaders.
/**
* WebService returns the request headers in the response
*/
@Test
public void testClientTraceInHeaders() throws IOException {
Logger.d(TAG, "test get" + android.os.Process.myTid());
final HttpURLConnection mockedConnection = Mockito.mock(HttpURLConnection.class);
HttpUrlConnectionFactory.setMockedHttpUrlConnection(mockedConnection);
Util.prepareMockedUrlConnection(mockedConnection);
Mockito.when(mockedConnection.getInputStream()).thenReturn(Util.createInputStream(AAD.ADAL_ID_PLATFORM + "-Android" + "dummy string" + AAD.ADAL_ID_VERSION + "-" + AuthenticationContext.getVersionName()));
Mockito.when(mockedConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
final WebRequestHandler request = new WebRequestHandler();
HttpWebResponse httpResponse = request.sendGet(getUrl(TEST_WEBAPI_URL), getTestHeaders("testClientTraceInHeaders", "valueYes"));
assertNotNull(httpResponse != null);
assertTrue("status is 200", httpResponse.getStatusCode() == HttpURLConnection.HTTP_OK);
String responseMsg = httpResponse.getBody();
assertTrue("request header check", responseMsg.contains(AAD.ADAL_ID_PLATFORM + "-Android"));
assertTrue("request header check", responseMsg.contains(AAD.ADAL_ID_VERSION + "-" + AuthenticationContext.getVersionName()));
}
Aggregations