Search in sources :

Example 76 with Credentials

use of okhttp3.Credentials in project JavaSDK by OpenGamma.

the class BasicTest method testAuthGood.

public void testAuthGood() {
    AuthClient mockAuth = new TestingAuthClient();
    @SuppressWarnings("resource") ServiceInvoker invoker = ServiceInvoker.builder(CREDENTIALS).authClientFactory(inv -> mockAuth).build();
    assertEquals(invoker.getServiceUrl(), SERVICE_URL);
    assertEquals(invoker.getHttpClient().interceptors().size(), 3);
    assertEquals(invoker.getHttpClient().followRedirects(), true);
    assertEquals(invoker.getExecutor().isShutdown(), false);
    invoker.close();
    assertEquals(invoker.getExecutor().isShutdown(), true);
}
Also used : AuthenticationException(com.opengamma.sdk.common.auth.AuthenticationException) Credentials(com.opengamma.sdk.common.auth.Credentials) Request(okhttp3.Request) OkHttpClient(okhttp3.OkHttpClient) SERVICE_URL(com.opengamma.sdk.common.ServiceInvoker.SERVICE_URL) Assert.assertThrows(org.testng.Assert.assertThrows) AuthClient(com.opengamma.sdk.common.auth.AuthClient) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) AuthClient(com.opengamma.sdk.common.auth.AuthClient)

Example 77 with Credentials

use of okhttp3.Credentials in project azure-tools-for-java by Microsoft.

the class IntegrationTestBase method setUpStep.

public void setUpStep() throws Exception {
    if (currentTestName == null) {
        currentTestName = name.getMethodName();
    } else {
        throw new Exception("Setting up another test in middle of a test");
    }
    addTextReplacementRule(GLOBAL_ENDPOINT, MOCK_URI + "/");
    ApplicationTokenCredentials credentials = new TestCredentials();
    String defaultSubscription = "";
    if (IS_MOCKED) {
        defaultSubscription = MOCK_SUBSCRIPTION;
        File recordFile = getRecordFile();
        ObjectMapper mapper = new ObjectMapper();
        try {
            testRecord = mapper.readValue(recordFile, TestRecord.class);
        } catch (Exception e) {
            throw new Exception("Fail read test record: " + e.getMessage());
        }
    } else {
        try {
            credentials = ApplicationTokenCredentials.fromFile(new File(azureAuthFile));
        } catch (Exception e) {
            throw new Exception("Fail to open auth file:" + azureAuthFile);
        }
        defaultSubscription = credentials.defaultSubscriptionId();
    }
    interceptor = new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            if (IS_MOCKED) {
                return registerRecordedResponse(chain);
            } else {
                return chain.proceed(chain.request());
            }
        }
    };
    restClient = createRestClient(credentials);
    initialize(restClient, defaultSubscription, credentials.domain());
}
Also used : Response(okhttp3.Response) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) IOException(java.io.IOException) File(java.io.File) Interceptor(okhttp3.Interceptor) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 78 with Credentials

use of okhttp3.Credentials in project azure-tools-for-java by Microsoft.

the class IntegrationTestBase method setUpStep.

public void setUpStep() throws Exception {
    if (currentTestName == null) {
        currentTestName = name.getMethodName();
    } else {
        throw new Exception("Setting up another test in middle of a test");
    }
    addTextReplacementRule(GLOBAL_ENDPOINT, MOCK_URI + "/");
    ApplicationTokenCredentials credentials = new TestCredentials();
    String defaultSubscription = "";
    if (IS_MOCKED) {
        defaultSubscription = MOCK_SUBSCRIPTION;
        File recordFile = getRecordFile();
        ObjectMapper mapper = new ObjectMapper();
        try {
            testRecord = mapper.readValue(recordFile, TestRecord.class);
        } catch (Exception e) {
            throw new Exception("Fail read test record: " + e.getMessage());
        }
    } else {
        try {
            credentials = ApplicationTokenCredentials.fromFile(new File(azureAuthFile));
        } catch (Exception e) {
            throw new Exception("Fail to open auth file:" + azureAuthFile);
        }
        defaultSubscription = credentials.defaultSubscriptionId();
    }
    interceptor = new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            if (IS_MOCKED) {
                return registerRecordedResponse(chain);
            } else {
                return chain.proceed(chain.request());
            }
        }
    };
    restClient = createRestClient(credentials);
    initialize(restClient, defaultSubscription, credentials.domain());
}
Also used : Response(okhttp3.Response) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) IOException(java.io.IOException) File(java.io.File) Interceptor(okhttp3.Interceptor) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 79 with Credentials

use of okhttp3.Credentials in project java-sdk by watson-developer-cloud.

the class ErrorResponseTest method testUnauthorized.

/**
 * Test HTTP status code 401 (Unauthorized) error response.
 */
@Test
public void testUnauthorized() {
    String message = "The request failed because the moon is full.";
    server.enqueue(new MockResponse().setResponseCode(401).addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON).setBody("{\"error\": \"" + message + "\"}"));
    try {
        GenericModel response = service.testMethod().execute();
    } catch (Exception e) {
        assertTrue(e instanceof UnauthorizedException);
        UnauthorizedException ex = (UnauthorizedException) e;
        assertEquals(401, ex.getStatusCode());
        assertTrue(ex.getMessage().startsWith("Unauthorized: Access is denied due to invalid credentials."));
    }
}
Also used : GenericModel(com.ibm.watson.developer_cloud.service.model.GenericModel) MockResponse(okhttp3.mockwebserver.MockResponse) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) ForbiddenException(com.ibm.watson.developer_cloud.service.exception.ForbiddenException) BadRequestException(com.ibm.watson.developer_cloud.service.exception.BadRequestException) TooManyRequestsException(com.ibm.watson.developer_cloud.service.exception.TooManyRequestsException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) ConflictException(com.ibm.watson.developer_cloud.service.exception.ConflictException) UnsupportedException(com.ibm.watson.developer_cloud.service.exception.UnsupportedException) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) RequestTooLargeException(com.ibm.watson.developer_cloud.service.exception.RequestTooLargeException) InternalServerErrorException(com.ibm.watson.developer_cloud.service.exception.InternalServerErrorException) ServiceUnavailableException(com.ibm.watson.developer_cloud.service.exception.ServiceUnavailableException) Test(org.junit.Test) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest)

Example 80 with Credentials

use of okhttp3.Credentials in project zipkin by openzipkin.

the class ITZipkinServer method shouldAllowAnyOriginByDefault.

@Test
public void shouldAllowAnyOriginByDefault() throws Exception {
    Response response = client.newCall(new Request.Builder().url(url(server, "/api/v2/traces")).header("Origin", "http://foo.example.com").build()).execute();
    assertThat(response.isSuccessful()).isTrue();
    assertThat(response.header("vary")).isNull();
    assertThat(response.header("access-control-allow-credentials")).isNull();
    assertThat(response.header("access-control-allow-origin")).contains("*");
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

Request (okhttp3.Request)39 Response (okhttp3.Response)29 OkHttpClient (okhttp3.OkHttpClient)22 IOException (java.io.IOException)20 Test (org.junit.Test)16 HttpUrl (okhttp3.HttpUrl)10 RequestBody (okhttp3.RequestBody)9 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)9 ResponseBody (okhttp3.ResponseBody)8 NonNull (androidx.annotation.NonNull)7 MockResponse (okhttp3.mockwebserver.MockResponse)7 BasicAuthenticator (com.burgstaller.okhttp.basic.BasicAuthenticator)6 CachingAuthenticator (com.burgstaller.okhttp.digest.CachingAuthenticator)6 Credentials (com.burgstaller.okhttp.digest.Credentials)6 DigestAuthenticator (com.burgstaller.okhttp.digest.DigestAuthenticator)6 Observable (rx.Observable)6 AuthenticationCacheInterceptor (com.burgstaller.okhttp.AuthenticationCacheInterceptor)5 CachingAuthenticatorDecorator (com.burgstaller.okhttp.CachingAuthenticatorDecorator)5 PokemonGo (com.pokegoapi.api.PokemonGo)5 PtcCredentialProvider (com.pokegoapi.auth.PtcCredentialProvider)5