Search in sources :

Example 76 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project autorest-clientruntime-for-java by Azure.

the class RestClientTests method newBuilderClonesProperties.

@Test
public void newBuilderClonesProperties() {
    RestClient restClient = new RestClient.Builder().withBaseUrl("http://localhost").withSerializerAdapter(new JacksonAdapter()).withResponseBuilderFactory(new ServiceResponseBuilder.Factory()).withCredentials(new TokenCredentials("Bearer", "token")).withLogLevel(LogLevel.BASIC.withPrettyJson(true)).withInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            return chain.proceed(chain.request());
        }
    }).withUserAgent("user").withNetworkInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            return chain.proceed(chain.request());
        }
    }).withConnectionTimeout(100, TimeUnit.MINUTES).build();
    RestClient newClient = restClient.newBuilder().withBaseUrl("https://contoso.com").withCredentials(new BasicAuthenticationCredentials("user", "pass")).withLogLevel(LogLevel.BODY_AND_HEADERS).withUserAgent("anotheruser").withConnectionTimeout(200, TimeUnit.SECONDS).withSerializerAdapter(new SerializerAdapter<Object>() {

        @Override
        public Object serializer() {
            return null;
        }

        @Override
        public Converter.Factory converterFactory() {
            return retrofit2.converter.jackson.JacksonConverterFactory.create();
        }

        @Override
        public String serialize(Object object) throws IOException {
            return null;
        }

        @Override
        public String serializeRaw(Object object) {
            return null;
        }

        @Override
        public String serializeList(List<?> list, CollectionFormat format) {
            return null;
        }

        @Override
        public <U> U deserialize(String value, Type type) throws IOException {
            return null;
        }
    }).withResponseBuilderFactory(new ResponseBuilder.Factory() {

        @Override
        public <T, E extends RestException> ResponseBuilder<T, E> newInstance(SerializerAdapter<?> serializerAdapter) {
            return null;
        }
    }).build();
    Assert.assertNotEquals(restClient.retrofit().baseUrl().toString(), newClient.retrofit().baseUrl().toString());
    Assert.assertNotEquals(restClient.logLevel(), newClient.logLevel());
    Assert.assertNotEquals(restClient.logLevel().isPrettyJson(), newClient.logLevel().isPrettyJson());
    Assert.assertNotEquals(restClient.serializerAdapter(), newClient.serializerAdapter());
    Assert.assertNotEquals(restClient.responseBuilderFactory(), newClient.responseBuilderFactory());
    Assert.assertNotEquals(restClient.credentials(), newClient.credentials());
    for (Interceptor interceptor : restClient.httpClient().interceptors()) {
        if (interceptor instanceof UserAgentInterceptor) {
            Assert.assertEquals("user", ((UserAgentInterceptor) interceptor).userAgent());
        }
    }
    for (Interceptor interceptor : newClient.httpClient().interceptors()) {
        if (interceptor instanceof UserAgentInterceptor) {
            Assert.assertEquals("anotheruser", ((UserAgentInterceptor) interceptor).userAgent());
        }
    }
    Assert.assertNotEquals(restClient.httpClient().connectTimeoutMillis(), newClient.httpClient().connectTimeoutMillis());
}
Also used : UserAgentInterceptor(com.microsoft.rest.interceptors.UserAgentInterceptor) BasicAuthenticationCredentials(com.microsoft.rest.credentials.BasicAuthenticationCredentials) SerializerAdapter(com.microsoft.rest.protocol.SerializerAdapter) ResponseBuilder(com.microsoft.rest.protocol.ResponseBuilder) JacksonAdapter(com.microsoft.rest.serializer.JacksonAdapter) IOException(java.io.IOException) Response(okhttp3.Response) Type(java.lang.reflect.Type) Interceptor(okhttp3.Interceptor) UserAgentInterceptor(com.microsoft.rest.interceptors.UserAgentInterceptor) TokenCredentials(com.microsoft.rest.credentials.TokenCredentials) Test(org.junit.Test)

Example 77 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project autorest-clientruntime-for-java by Azure.

the class RestClientTests method newBuilderKeepsConfigs.

@Test
public void newBuilderKeepsConfigs() {
    RestClient restClient = new RestClient.Builder().withBaseUrl("http://localhost").withSerializerAdapter(new JacksonAdapter()).withResponseBuilderFactory(new ServiceResponseBuilder.Factory()).withCredentials(new TokenCredentials("Bearer", "token")).withLogLevel(LogLevel.BASIC).withInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            return chain.proceed(chain.request());
        }
    }).withUserAgent("user").withNetworkInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            return chain.proceed(chain.request());
        }
    }).withConnectionTimeout(100, TimeUnit.MINUTES).withProtocols(Arrays.asList(Protocol.HTTP_1_1)).build();
    RestClient newClient = restClient.newBuilder().build();
    Assert.assertEquals(restClient.retrofit().baseUrl().toString(), newClient.retrofit().baseUrl().toString());
    Assert.assertEquals(restClient.logLevel(), newClient.logLevel());
    Assert.assertEquals(restClient.logLevel().isPrettyJson(), newClient.logLevel().isPrettyJson());
    Assert.assertEquals(restClient.serializerAdapter(), newClient.serializerAdapter());
    Assert.assertEquals(restClient.responseBuilderFactory(), newClient.responseBuilderFactory());
    Assert.assertEquals(restClient.credentials(), newClient.credentials());
    for (Interceptor interceptor : newClient.httpClient().interceptors()) {
        if (interceptor instanceof UserAgentInterceptor) {
            Assert.assertEquals("user", ((UserAgentInterceptor) interceptor).userAgent());
        }
    }
    Assert.assertEquals(restClient.httpClient().interceptors().size(), newClient.httpClient().interceptors().size());
    Assert.assertEquals(restClient.httpClient().networkInterceptors().size(), newClient.httpClient().networkInterceptors().size());
    Assert.assertEquals(TimeUnit.MINUTES.toMillis(100), newClient.httpClient().connectTimeoutMillis());
    Assert.assertEquals(Arrays.asList(Protocol.HTTP_1_1), newClient.httpClient().protocols());
}
Also used : UserAgentInterceptor(com.microsoft.rest.interceptors.UserAgentInterceptor) JacksonAdapter(com.microsoft.rest.serializer.JacksonAdapter) Interceptor(okhttp3.Interceptor) UserAgentInterceptor(com.microsoft.rest.interceptors.UserAgentInterceptor) TokenCredentials(com.microsoft.rest.credentials.TokenCredentials) Test(org.junit.Test)

Example 78 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project autorest-clientruntime-for-java by Azure.

the class UserAgentTests method customUserAgentTests.

@Test
public void customUserAgentTests() throws Exception {
    OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().addInterceptor(new UserAgentInterceptor().withUserAgent("Awesome")).addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            String header = chain.request().header("User-Agent");
            Assert.assertEquals("Awesome", header);
            return new Response.Builder().request(chain.request()).code(200).message("OK").protocol(Protocol.HTTP_1_1).body(ResponseBody.create(MediaType.parse("text/plain"), "azure rocks")).build();
        }
    });
    ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, new Retrofit.Builder()) {
    };
    Response response = serviceClient.httpClient().newCall(new Request.Builder().get().url("http://localhost").build()).execute();
    Assert.assertEquals(200, response.code());
}
Also used : OkHttpClient(okhttp3.OkHttpClient) UserAgentInterceptor(com.microsoft.rest.interceptors.UserAgentInterceptor) Request(okhttp3.Request) IOException(java.io.IOException) Response(okhttp3.Response) Retrofit(retrofit2.Retrofit) Interceptor(okhttp3.Interceptor) UserAgentInterceptor(com.microsoft.rest.interceptors.UserAgentInterceptor) Test(org.junit.Test)

Example 79 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project autorest-clientruntime-for-java by Azure.

the class CredentialsTests method basicCredentialsTest.

@Test
public void basicCredentialsTest() throws Exception {
    BasicAuthenticationCredentials credentials = new BasicAuthenticationCredentials("user", "pass");
    OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
    credentials.applyCredentialsFilter(clientBuilder);
    clientBuilder.addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            String header = chain.request().header("Authorization");
            Assert.assertEquals("Basic dXNlcjpwYXNz", header);
            return new Response.Builder().request(chain.request()).code(200).message("OK").protocol(Protocol.HTTP_1_1).body(ResponseBody.create(MediaType.parse("text/plain"), "azure rocks")).build();
        }
    });
    ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, new Retrofit.Builder()) {
    };
    Response response = serviceClient.httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute();
    Assert.assertEquals(200, response.code());
}
Also used : OkHttpClient(okhttp3.OkHttpClient) BasicAuthenticationCredentials(com.microsoft.rest.credentials.BasicAuthenticationCredentials) IOException(java.io.IOException) Response(okhttp3.Response) Retrofit(retrofit2.Retrofit) Interceptor(okhttp3.Interceptor) Test(org.junit.Test)

Example 80 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project autorest-clientruntime-for-java by Azure.

the class RequestIdHeaderInterceptorTests method newRequestIdForEachCall.

@Test
public void newRequestIdForEachCall() throws Exception {
    RestClient restClient = new RestClient.Builder().withBaseUrl("http://localhost").withSerializerAdapter(new AzureJacksonAdapter()).withResponseBuilderFactory(new AzureResponseBuilder.Factory()).withInterceptor(new RequestIdHeaderInterceptor()).withInterceptor(new Interceptor() {

        private String firstRequestId = null;

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            if (request.header(REQUEST_ID_HEADER) != null) {
                if (firstRequestId == null) {
                    firstRequestId = request.header(REQUEST_ID_HEADER);
                    return new Response.Builder().code(200).request(request).message("OK").protocol(Protocol.HTTP_1_1).body(ResponseBody.create(MediaType.parse("text/plain"), "azure rocks")).build();
                } else if (!request.header(REQUEST_ID_HEADER).equals(firstRequestId)) {
                    return new Response.Builder().code(200).request(request).message("OK").protocol(Protocol.HTTP_1_1).body(ResponseBody.create(MediaType.parse("text/plain"), "azure rocks")).build();
                }
            }
            return new Response.Builder().code(400).request(request).protocol(Protocol.HTTP_1_1).build();
        }
    }).build();
    AzureServiceClient serviceClient = new AzureServiceClient(restClient) {
    };
    Response response = serviceClient.restClient().httpClient().newCall(new Request.Builder().get().url("http://localhost").build()).execute();
    Assert.assertEquals(200, response.code());
    response = serviceClient.restClient().httpClient().newCall(new Request.Builder().get().url("http://localhost").build()).execute();
    Assert.assertEquals(200, response.code());
}
Also used : AzureJacksonAdapter(com.microsoft.azure.serializer.AzureJacksonAdapter) RestClient(com.microsoft.rest.RestClient) Request(okhttp3.Request) RequestIdHeaderInterceptor(com.microsoft.rest.interceptors.RequestIdHeaderInterceptor) Response(okhttp3.Response) Interceptor(okhttp3.Interceptor) RequestIdHeaderInterceptor(com.microsoft.rest.interceptors.RequestIdHeaderInterceptor) Test(org.junit.Test)

Aggregations

Interceptor (okhttp3.Interceptor)138 Request (okhttp3.Request)61 OkHttpClient (okhttp3.OkHttpClient)54 Response (okhttp3.Response)51 IOException (java.io.IOException)45 Test (org.junit.Test)29 Retrofit (retrofit2.Retrofit)27 File (java.io.File)15 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)13 Cache (okhttp3.Cache)9 HttpUrl (okhttp3.HttpUrl)8 Interceptor (com.pushtorefresh.storio3.Interceptor)7 Dispatcher (okhttp3.Dispatcher)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 X509TrustManager (javax.net.ssl.X509TrustManager)6 CachingAuthenticator (com.burgstaller.okhttp.digest.CachingAuthenticator)5 Provides (dagger.Provides)5 Singleton (javax.inject.Singleton)5 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)5 OAuth (com.arm.mbed.cloud.sdk.internal.devicedirectory.auth.OAuth)4