Search in sources :

Example 31 with Authenticator

use of okhttp3.Authenticator in project edx-app-android by edx.

the class AuthenticationTests method testAuthenticate_withoutRefreshToken.

@Test
public void testAuthenticate_withoutRefreshToken() throws Exception {
    loginPrefs.storeAuthTokenResponse(MockDataUtil.getMockResponse("post_oauth2_access_token_no_refresh_token", AuthResponse.class), LoginPrefs.AuthBackend.PASSWORD);
    client = client.newBuilder().authenticator(new OauthRefreshTokenAuthenticator(context)).build();
    Request request = new Request.Builder().url(mockServer.url("/dummy/endpoint/")).header("Authorization", "expired_token").build();
    Response response = client.newCall(request).execute();
    assertEquals(HttpStatus.UNAUTHORIZED, response.code());
    assertEquals("expired_token", response.request().header("Authorization"));
}
Also used : OauthRefreshTokenAuthenticator(org.edx.mobile.http.authenticator.OauthRefreshTokenAuthenticator) AuthResponse(org.edx.mobile.authentication.AuthResponse) Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) AuthResponse(org.edx.mobile.authentication.AuthResponse) Test(org.junit.Test)

Example 32 with Authenticator

use of okhttp3.Authenticator in project okhttp-digest by rburgst.

the class AuthenticationCacheInterceptorTest method givenCachedAuthenticationFor.

private void givenCachedAuthenticationFor(String url, Map<String, CachingAuthenticator> authCache) throws IOException {
    Authenticator decorator = new CachingAuthenticatorDecorator(new BasicAuthenticator(new Credentials("user1", "user1")), authCache);
    Request dummyRequest = new Request.Builder().url(url).get().build();
    Response response = new Response.Builder().request(dummyRequest).protocol(Protocol.HTTP_1_1).code(HTTP_UNAUTHORIZED).message("Unauthorized").header("WWW-Authenticate", "Basic realm=\"myrealm\"").build();
    decorator.authenticate(null, response);
}
Also used : Response(okhttp3.Response) BasicAuthenticator(com.burgstaller.okhttp.basic.BasicAuthenticator) Request(okhttp3.Request) Authenticator(okhttp3.Authenticator) CachingAuthenticator(com.burgstaller.okhttp.digest.CachingAuthenticator) BasicAuthenticator(com.burgstaller.okhttp.basic.BasicAuthenticator) Credentials(com.burgstaller.okhttp.digest.Credentials)

Example 33 with Authenticator

use of okhttp3.Authenticator in project okhttp-digest by rburgst.

the class DispatchingAuthenticatorTest method testCaching_withBasicAuthenticatorPreferredOrder.

@Test
public void testCaching_withBasicAuthenticatorPreferredOrder() throws Exception {
    final Credentials credentials = new Credentials("user", "pwd");
    final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(credentials);
    final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
    DispatchingAuthenticator authenticator = new DispatchingAuthenticator.Builder().with("basic", basicAuthenticator).with("digest", digestAuthenticator).build();
    Request request = authenticator.authenticate(mockRoute, createUnauthorizedServerResponse());
    assertNotNull(request);
    request = authenticator.authenticateWithState(mockRoute, createDummyRequest());
    assertNotNull(request);
}
Also used : BasicAuthenticator(com.burgstaller.okhttp.basic.BasicAuthenticator) DigestAuthenticator(com.burgstaller.okhttp.digest.DigestAuthenticator) Request(okhttp3.Request) Credentials(com.burgstaller.okhttp.digest.Credentials) Test(org.junit.Test)

Example 34 with Authenticator

use of okhttp3.Authenticator in project okhttp-digest by rburgst.

the class DispatchingAuthenticatorTest method testCaching_withDigestAuthenticatorPreferredOrder.

@Test
public void testCaching_withDigestAuthenticatorPreferredOrder() throws Exception {
    final Credentials credentials = new Credentials("user", "pwd");
    final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(credentials);
    final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
    DispatchingAuthenticator authenticator = new DispatchingAuthenticator.Builder().with("digest", digestAuthenticator).with("basic", basicAuthenticator).build();
    Request request = authenticator.authenticate(mockRoute, createUnauthorizedServerResponse());
    assertNotNull(request);
    String authorizationHeader = request.header("Authorization");
    assertThat(authorizationHeader, CoreMatchers.startsWith("Basic"));
    request = authenticator.authenticateWithState(mockRoute, createDummyRequest());
    assertNotNull(request);
}
Also used : BasicAuthenticator(com.burgstaller.okhttp.basic.BasicAuthenticator) DigestAuthenticator(com.burgstaller.okhttp.digest.DigestAuthenticator) Request(okhttp3.Request) Credentials(com.burgstaller.okhttp.digest.Credentials) Test(org.junit.Test)

Example 35 with Authenticator

use of okhttp3.Authenticator in project okhttp-digest by rburgst.

the class ProxyAuthenticationManualTest method testConnection_WithProxyDigestAuthWithNotAllowdSites_Expect403.

@Test
public void testConnection_WithProxyDigestAuthWithNotAllowdSites_Expect403() throws IOException {
    final DigestAuthenticator authenticator = givenDigestAuthenticator();
    final OkHttpClient client = givenHttpClientWithProxyAuth(authenticator);
    final Request request = new Request.Builder().url("http://www.youtube.com").build();
    Response response = client.newCall(request).execute();
    assertEquals(403, response.code());
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) DigestAuthenticator(com.burgstaller.okhttp.digest.DigestAuthenticator) Request(okhttp3.Request) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)37 Request (okhttp3.Request)27 OkHttpClient (okhttp3.OkHttpClient)26 Response (okhttp3.Response)24 MockResponse (okhttp3.mockwebserver.MockResponse)23 Authenticator (okhttp3.Authenticator)20 IOException (java.io.IOException)15 RecordingOkAuthenticator (okhttp3.internal.RecordingOkAuthenticator)15 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)13 Route (okhttp3.Route)12 BasicAuthenticator (com.burgstaller.okhttp.basic.BasicAuthenticator)9 DigestAuthenticator (com.burgstaller.okhttp.digest.DigestAuthenticator)8 CachingAuthenticator (com.burgstaller.okhttp.digest.CachingAuthenticator)7 InetSocketAddress (java.net.InetSocketAddress)7 RecordingAuthenticator (okhttp3.internal.RecordingAuthenticator)6 Credentials (com.burgstaller.okhttp.digest.Credentials)5 InetAddress (java.net.InetAddress)5 Proxy (java.net.Proxy)5 Interceptor (okhttp3.Interceptor)5 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)5