Search in sources :

Example 6 with WebRequestHandler

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);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) OutputStream(java.io.OutputStream) JSONException(org.json.JSONException) IWebRequestHandler(com.microsoft.identity.common.adal.internal.net.IWebRequestHandler) WebRequestHandler(com.microsoft.identity.common.adal.internal.net.WebRequestHandler) Test(org.junit.Test)

Example 7 with WebRequestHandler

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");
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) OutputStream(java.io.OutputStream) IWebRequestHandler(com.microsoft.identity.common.adal.internal.net.IWebRequestHandler) WebRequestHandler(com.microsoft.identity.common.adal.internal.net.WebRequestHandler) Test(org.junit.Test)

Example 8 with WebRequestHandler

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"));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) WebRequestHandler(com.microsoft.identity.common.adal.internal.net.WebRequestHandler) HttpWebResponse(com.microsoft.identity.common.adal.internal.net.HttpWebResponse) Test(org.junit.Test)

Example 9 with WebRequestHandler

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");
}
Also used : HashMap(java.util.HashMap) WebRequestHandler(com.microsoft.identity.common.adal.internal.net.WebRequestHandler)

Example 10 with WebRequestHandler

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()));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) WebRequestHandler(com.microsoft.identity.common.adal.internal.net.WebRequestHandler) HttpWebResponse(com.microsoft.identity.common.adal.internal.net.HttpWebResponse) Test(org.junit.Test)

Aggregations

WebRequestHandler (com.microsoft.identity.common.adal.internal.net.WebRequestHandler)10 Test (org.junit.Test)8 HttpURLConnection (java.net.HttpURLConnection)7 HttpWebResponse (com.microsoft.identity.common.adal.internal.net.HttpWebResponse)4 OutputStream (java.io.OutputStream)4 IWebRequestHandler (com.microsoft.identity.common.adal.internal.net.IWebRequestHandler)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Gson (com.google.gson.Gson)1 MalformedURLException (java.net.MalformedURLException)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 JSONException (org.json.JSONException)1 Ignore (org.junit.Ignore)1