Search in sources :

Example 31 with HttpWebResponse

use of com.microsoft.identity.common.adal.internal.net.HttpWebResponse in project azure-activedirectory-library-for-android by AzureAD.

the class OauthTests method testRefreshTokenWebResponseDeviceChallengeHeaderEmpty.

@SuppressWarnings("unchecked")
@Test
public void testRefreshTokenWebResponseDeviceChallengeHeaderEmpty() throws IOException {
    IWebRequestHandler mockWebRequest = mock(IWebRequestHandler.class);
    Map<String, List<String>> headers = getHeader(AuthenticationConstants.Broker.CHALLENGE_REQUEST_HEADER, " ");
    HttpWebResponse responseChallenge = new HttpWebResponse(HttpURLConnection.HTTP_UNAUTHORIZED, null, headers);
    when(mockWebRequest.sendPost(eq(new URL(TEST_AUTHORITY + "/oauth2/token")), Mockito.<String, String>anyMap(), any(byte[].class), eq("application/x-www-form-urlencoded"))).thenReturn(responseChallenge);
    // send request
    MockAuthenticationCallback testResult = refreshToken(getValidAuthenticationRequest(), mockWebRequest, "testRefreshToken");
    // Verify that callback can receive this error
    assertNotNull("Callback has error", testResult.getException());
    assertNotNull(testResult.getException());
    assertTrue(testResult.getException() instanceof AuthenticationException);
    assertEquals("Check error message", "Challenge header is empty", testResult.getException().getMessage());
}
Also used : IWebRequestHandler(com.microsoft.identity.common.adal.internal.net.IWebRequestHandler) ArrayList(java.util.ArrayList) List(java.util.List) URL(java.net.URL) HttpWebResponse(com.microsoft.identity.common.adal.internal.net.HttpWebResponse) Test(org.junit.Test)

Example 32 with HttpWebResponse

use of com.microsoft.identity.common.adal.internal.net.HttpWebResponse in project azure-activedirectory-library-for-android by AzureAD.

the class AuthenticationParamsTests method testParseResponseNegative.

/**
 * test private method to make sure parsing is right
 */
@Test
public void testParseResponseNegative() throws IllegalArgumentException, ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
    callParseResponseForException(new HttpWebResponse(HttpURLConnection.HTTP_OK, null, null), AuthenticationParameters.AUTH_HEADER_WRONG_STATUS);
    callParseResponseForException(new HttpWebResponse(HttpURLConnection.HTTP_UNAUTHORIZED, null, null), AuthenticationParameters.AUTH_HEADER_MISSING);
    callParseResponseForException(new HttpWebResponse(HttpURLConnection.HTTP_UNAUTHORIZED, null, getHeader("WWW-Authenticate", "v")), AuthenticationParameters.AUTH_HEADER_INVALID_FORMAT);
    callParseResponseForException(new HttpWebResponse(HttpURLConnection.HTTP_UNAUTHORIZED, null, getHeader("WWW-Authenticate", "Bearer nonsense")), AuthenticationParameters.AUTH_HEADER_INVALID_FORMAT);
    callParseResponseForException(new HttpWebResponse(HttpURLConnection.HTTP_UNAUTHORIZED, null, getHeader("WWW-Authenticate", "Bearer")), AuthenticationParameters.AUTH_HEADER_INVALID_FORMAT);
    callParseResponseForException(new HttpWebResponse(HttpURLConnection.HTTP_UNAUTHORIZED, null, getHeader("WWW-Authenticate", "Bearer ")), AuthenticationParameters.AUTH_HEADER_INVALID_FORMAT);
    callParseResponseForException(new HttpWebResponse(HttpURLConnection.HTTP_UNAUTHORIZED, null, getHeader("WWW-Authenticate", " Bearer")), AuthenticationParameters.AUTH_HEADER_INVALID_FORMAT);
    callParseResponseForException(new HttpWebResponse(HttpURLConnection.HTTP_UNAUTHORIZED, null, getHeader("WWW-Authenticate", " Bearer ")), AuthenticationParameters.AUTH_HEADER_INVALID_FORMAT);
    callParseResponseForException(new HttpWebResponse(HttpURLConnection.HTTP_UNAUTHORIZED, null, getHeader("WWW-Authenticate", "\t Bearer  ")), AuthenticationParameters.AUTH_HEADER_INVALID_FORMAT);
    callParseResponseForException(new HttpWebResponse(HttpURLConnection.HTTP_UNAUTHORIZED, null, getHeader("WWW-Authenticate", "Bearer test ")), AuthenticationParameters.AUTH_HEADER_INVALID_FORMAT);
    callParseResponseForException(new HttpWebResponse(HttpURLConnection.HTTP_UNAUTHORIZED, null, getHeader("WWW-Authenticate", "Bear gets=honey ")), AuthenticationParameters.AUTH_HEADER_INVALID_FORMAT);
    callParseResponseForException(new HttpWebResponse(HttpURLConnection.HTTP_UNAUTHORIZED, null, getHeader("WWW-Authenticate", "Bearer =,=,")), AuthenticationParameters.AUTH_HEADER_INVALID_FORMAT);
    callParseResponseForException(new HttpWebResponse(HttpURLConnection.HTTP_UNAUTHORIZED, null, getHeader("WWW-Authenticate", "Bearer some text here,")), AuthenticationParameters.AUTH_HEADER_INVALID_FORMAT);
    callParseResponseForException(new HttpWebResponse(HttpURLConnection.HTTP_UNAUTHORIZED, null, getHeader("WWW-Authenticate", "Bearer authorization_uri= ")), AuthenticationParameters.AUTH_HEADER_MISSING_AUTHORITY);
    callParseResponseForException(new HttpWebResponse(HttpURLConnection.HTTP_UNAUTHORIZED, null, getHeader("WWW-Authenticate", "Bearerauthorization_uri=something")), AuthenticationParameters.AUTH_HEADER_INVALID_FORMAT);
    callParseResponseForException(new HttpWebResponse(HttpURLConnection.HTTP_UNAUTHORIZED, null, getHeader("WWW-Authenticate", "Bearerauthorization_uri=\"https://www.something.com\"")), AuthenticationParameters.AUTH_HEADER_INVALID_FORMAT);
    callParseResponseForException(new HttpWebResponse(HttpURLConnection.HTTP_UNAUTHORIZED, null, getHeader("WWW-Authenticate", "Bearer    \t authorization_uri=,something=a ")), AuthenticationParameters.AUTH_HEADER_MISSING_AUTHORITY);
}
Also used : HttpWebResponse(com.microsoft.identity.common.adal.internal.net.HttpWebResponse) Test(org.junit.Test)

Example 33 with HttpWebResponse

use of com.microsoft.identity.common.adal.internal.net.HttpWebResponse in project azure-activedirectory-library-for-android by AzureAD.

the class WebFingerMetadataRequestorTests method testParseMetadata.

@Test
public void testParseMetadata() throws AuthenticationException {
    HttpWebResponse mockWebResponse = Mockito.mock(HttpWebResponse.class);
    Mockito.when(mockWebResponse.getBody()).thenReturn(RESPONSE);
    WebFingerMetadata metadata = new WebFingerMetadataRequestor().parseMetadata(mockWebResponse);
    assertEquals("https://fs.lindft6.com", metadata.getSubject());
    assertNotNull(metadata.getLinks());
    assertEquals(1, metadata.getLinks().size());
    assertEquals("http://schemas.microsoft.com/rel/trusted-realm", metadata.getLinks().get(0).getRel());
    assertEquals("https://fs.lindft6.com", metadata.getLinks().get(0).getHref());
}
Also used : HttpWebResponse(com.microsoft.identity.common.adal.internal.net.HttpWebResponse) Test(org.junit.Test)

Example 34 with HttpWebResponse

use of com.microsoft.identity.common.adal.internal.net.HttpWebResponse 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 35 with HttpWebResponse

use of com.microsoft.identity.common.adal.internal.net.HttpWebResponse 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

HttpWebResponse (com.microsoft.identity.common.adal.internal.net.HttpWebResponse)35 Test (org.junit.Test)29 URL (java.net.URL)21 IWebRequestHandler (com.microsoft.identity.common.adal.internal.net.IWebRequestHandler)19 SmallTest (androidx.test.filters.SmallTest)16 HashMap (java.util.HashMap)6 WebRequestHandler (com.microsoft.identity.common.adal.internal.net.WebRequestHandler)5 HttpURLConnection (java.net.HttpURLConnection)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Handler (android.os.Handler)1 Gson (com.google.gson.Gson)1 ChallengeResponse (com.microsoft.aad.adal.ChallengeResponseBuilder.ChallengeResponse)1 JWSBuilder (com.microsoft.identity.common.adal.internal.JWSBuilder)1 OutputStream (java.io.OutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 UnknownHostException (java.net.UnknownHostException)1