Search in sources :

Example 16 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project crnk-framework by crnk-project.

the class QuerySpecClientTest method testUpdate.

public void testUpdate(boolean pushAlways) {
    final List<String> methods = new ArrayList<>();
    final List<String> paths = new ArrayList<>();
    final Interceptor interceptor = new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            methods.add(request.method());
            paths.add(request.url().encodedPath());
            return chain.proceed(request);
        }
    };
    HttpAdapter httpAdapter = client.getHttpAdapter();
    if (httpAdapter instanceof OkHttpAdapter) {
        ((OkHttpAdapter) httpAdapter).addListener(new OkHttpAdapterListener() {

            @Override
            public void onBuild(Builder builder) {
                builder.addInterceptor(interceptor);
            }
        });
    }
    Task task = new Task();
    task.setId(1L);
    task.setName("test");
    taskRepo.create(task);
    Task savedTask = taskRepo.findOne(1L, new QuerySpec(Task.class));
    Assert.assertNotNull(savedTask);
    // perform update
    task.setName("updatedName");
    taskRepo.save(task);
    // check updated
    savedTask = taskRepo.findOne(1L, new QuerySpec(Task.class));
    Assert.assertNotNull(savedTask);
    Assert.assertEquals("updatedName", task.getName());
    if (httpAdapter instanceof OkHttpAdapter) {
        // check HTTP handling
        Assert.assertEquals(4, methods.size());
        Assert.assertEquals(4, paths.size());
        Assert.assertEquals("POST", methods.get(0));
        Assert.assertEquals("GET", methods.get(1));
        if (pushAlways) {
            Assert.assertEquals("POST", methods.get(2));
            Assert.assertEquals("/tasks", paths.get(2));
        } else {
            Assert.assertEquals("PATCH", methods.get(2));
            Assert.assertEquals("/tasks/1", paths.get(2));
        }
        Assert.assertEquals("GET", methods.get(3));
        Assert.assertEquals("/tasks", paths.get(0));
        Assert.assertEquals("/tasks/1", paths.get(1));
        Assert.assertEquals("/tasks/1", paths.get(3));
    }
}
Also used : Task(io.crnk.test.mock.models.Task) HttpAdapter(io.crnk.client.http.HttpAdapter) OkHttpAdapter(io.crnk.client.http.okhttp.OkHttpAdapter) Builder(okhttp3.OkHttpClient.Builder) ArrayList(java.util.ArrayList) Request(okhttp3.Request) OkHttpAdapterListener(io.crnk.client.http.okhttp.OkHttpAdapterListener) OkHttpAdapter(io.crnk.client.http.okhttp.OkHttpAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec) Interceptor(okhttp3.Interceptor)

Example 17 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project crnk-framework by crnk-project.

the class CharsetTest method testUTF8isDefault.

public void testUTF8isDefault(boolean okHttp) throws InstantiationException, IllegalAccessException {
    requestContentType = null;
    responseContentType = null;
    if (okHttp) {
        OkHttpAdapter adapter = OkHttpAdapter.newInstance();
        adapter.addListener(new OkHttpAdapterListener() {

            @Override
            public void onBuild(OkHttpClient.Builder builder) {
                builder.addInterceptor(new Interceptor() {

                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        requestContentType = chain.request().header(HttpHeaders.HTTP_CONTENT_TYPE);
                        Response response = chain.proceed(chain.request());
                        responseContentType = response.header(HttpHeaders.HTTP_CONTENT_TYPE);
                        return response;
                    }
                });
            }
        });
        client.setHttpAdapter(adapter);
    } else {
        HttpClientAdapter adapter = HttpClientAdapter.newInstance();
        adapter.addListener(new HttpClientAdapterListener() {

            @Override
            public void onBuild(HttpClientBuilder builder) {
                builder.addInterceptorFirst(new HttpRequestInterceptor() {

                    @Override
                    public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException {
                        Header header = httpRequest.getFirstHeader(HttpHeaders.HTTP_CONTENT_TYPE);
                        requestContentType = header != null ? header.getValue() : null;
                    }
                });
                builder.addInterceptorFirst(new HttpResponseInterceptor() {

                    @Override
                    public void process(HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {
                        Header header = httpResponse.getFirstHeader(HttpHeaders.HTTP_CONTENT_TYPE);
                        responseContentType = header != null ? header.getValue() : null;
                    }
                });
            }
        });
        client.setHttpAdapter(adapter);
    }
    ResourceRepositoryV2<Task, Long> testRepo = client.getRepositoryForType(Task.class);
    Task entity = new Task();
    entity.setId(1L);
    entity.setName("äöüé@¢€");
    testRepo.create(entity);
    Assert.assertEquals(HttpHeaders.JSONAPI_CONTENT_TYPE_AND_CHARSET, requestContentType);
    Assert.assertEquals(HttpHeaders.JSONAPI_CONTENT_TYPE_AND_CHARSET, responseContentType);
    Task savedEntity = testRepo.findOne(1L, new QuerySpec(Task.class));
    Assert.assertEquals(entity.getName(), savedEntity.getName());
    Assert.assertNull(requestContentType);
    Assert.assertEquals(HttpHeaders.JSONAPI_CONTENT_TYPE_AND_CHARSET, responseContentType);
}
Also used : HttpRequest(org.apache.http.HttpRequest) Task(io.crnk.test.mock.models.Task) OkHttpClient(okhttp3.OkHttpClient) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) HttpClientAdapterListener(io.crnk.client.http.apache.HttpClientAdapterListener) Response(okhttp3.Response) HttpResponse(org.apache.http.HttpResponse) HttpClientAdapter(io.crnk.client.http.apache.HttpClientAdapter) Header(org.apache.http.Header) OkHttpAdapterListener(io.crnk.client.http.okhttp.OkHttpAdapterListener) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) OkHttpAdapter(io.crnk.client.http.okhttp.OkHttpAdapter) HttpResponseInterceptor(org.apache.http.HttpResponseInterceptor) QuerySpec(io.crnk.core.queryspec.QuerySpec) Interceptor(okhttp3.Interceptor) HttpResponseInterceptor(org.apache.http.HttpResponseInterceptor) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor)

Example 18 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project crnk-framework by crnk-project.

the class RestTemplateClientTest method testUpdate.

@Test
public void testUpdate() {
    final List<String> methods = new ArrayList<>();
    final List<String> paths = new ArrayList<>();
    final Interceptor interceptor = new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            methods.add(request.method());
            paths.add(request.url().encodedPath());
            return chain.proceed(request);
        }
    };
    Task task = new Task();
    task.setId(1L);
    task.setName("test");
    taskRepo.create(task);
    Task savedTask = taskRepo.findOne(1L, new QuerySpec(Task.class));
    Assert.assertNotNull(savedTask);
    // perform update
    task.setName("updatedName");
    taskRepo.save(task);
    // check updated
    savedTask = taskRepo.findOne(1L, new QuerySpec(Task.class));
    Assert.assertNotNull(savedTask);
    Assert.assertEquals("updatedName", task.getName());
}
Also used : Task(io.crnk.test.mock.models.Task) ArrayList(java.util.ArrayList) Request(okhttp3.Request) QuerySpec(io.crnk.core.queryspec.QuerySpec) Interceptor(okhttp3.Interceptor) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 19 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project mbed-cloud-sdk-java by ARMmbed.

the class GsonCustomConverterFactory method setApiKey.

/**
 * Helper method to configure the first api key found
 * @param apiKey API key
 * @return ApiClient
 */
public ApiClient setApiKey(String apiKey) {
    for (Interceptor apiAuthorization : apiAuthorizations.values()) {
        if (apiAuthorization instanceof ApiKeyAuth) {
            ApiKeyAuth keyAuth = (ApiKeyAuth) apiAuthorization;
            keyAuth.setApiKey(apiKey);
            return this;
        }
    }
    return this;
}
Also used : ApiKeyAuth(com.arm.mbed.cloud.sdk.internal.connectorca.auth.ApiKeyAuth) Interceptor(okhttp3.Interceptor)

Example 20 with Interceptor

use of com.pushtorefresh.storio3.Interceptor in project mbed-cloud-sdk-java by ARMmbed.

the class GsonCustomConverterFactory method setAccessToken.

/**
 * Helper method to pre-set the oauth access token of the first oauth found in the apiAuthorizations (there should be only one)
 * @param accessToken Access token
 * @return ApiClient
 */
public ApiClient setAccessToken(String accessToken) {
    for (Interceptor apiAuthorization : apiAuthorizations.values()) {
        if (apiAuthorization instanceof OAuth) {
            OAuth oauth = (OAuth) apiAuthorization;
            oauth.setAccessToken(accessToken);
            return this;
        }
    }
    return this;
}
Also used : Interceptor(okhttp3.Interceptor) OAuth(com.arm.mbed.cloud.sdk.internal.connectorca.auth.OAuth)

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