Search in sources :

Example 1 with InterceptorResponse

use of com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse in project apollo-android by apollographql.

the class ApolloInterceptorTest method asyncApplicationInterceptorCanShortCircuitResponses.

@Test
public void asyncApplicationInterceptorCanShortCircuitResponses() throws Exception {
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
    EpisodeHeroNameQuery query = createHeroNameQuery();
    final InterceptorResponse expectedResponse = prepareInterceptorResponse(query);
    ApolloInterceptor interceptor = createShortcutInterceptor(expectedResponse);
    client = createApolloClient(interceptor);
    Rx2Apollo.from(client.query(query)).test().assertValue(expectedResponse.parsedResponse.get());
}
Also used : EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) InterceptorResponse(com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse) ApolloInterceptor(com.apollographql.apollo.interceptor.ApolloInterceptor) Test(org.junit.Test)

Example 2 with InterceptorResponse

use of com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse in project apollo-android by apollographql.

the class ApolloInterceptorTest method onShortCircuitingResponseSubsequentInterceptorsAreNotCalled.

@Test
public void onShortCircuitingResponseSubsequentInterceptorsAreNotCalled() throws IOException, ApolloException {
    EpisodeHeroNameQuery query = createHeroNameQuery();
    final InterceptorResponse expectedResponse = prepareInterceptorResponse(query);
    ApolloInterceptor firstInterceptor = createShortcutInterceptor(expectedResponse);
    ApolloInterceptor secondInterceptor = createChainInterceptor();
    client = ApolloClient.builder().serverUrl(server.url("/")).okHttpClient(okHttpClient).addApplicationInterceptor(firstInterceptor).addApplicationInterceptor(secondInterceptor).build();
    assertResponse(client.query(query), new Predicate<Response<EpisodeHeroNameQuery.Data>>() {

        @Override
        public boolean test(Response<EpisodeHeroNameQuery.Data> response) throws Exception {
            assertThat(expectedResponse.parsedResponse.get()).isEqualTo(response);
            return true;
        }
    });
}
Also used : Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Utils.assertResponse(com.apollographql.apollo.Utils.assertResponse) Response(com.apollographql.apollo.api.Response) InterceptorResponse(com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse) MockResponse(okhttp3.mockwebserver.MockResponse) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) InterceptorResponse(com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse) ApolloInterceptor(com.apollographql.apollo.interceptor.ApolloInterceptor) ApolloParseException(com.apollographql.apollo.exception.ApolloParseException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Test(org.junit.Test)

Example 3 with InterceptorResponse

use of com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse in project apollo-android by apollographql.

the class ApolloInterceptorTest method asyncApplicationInterceptorRewritesResponsesFromServer.

@Test
public void asyncApplicationInterceptorRewritesResponsesFromServer() throws Exception {
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
    EpisodeHeroNameQuery query = createHeroNameQuery();
    final InterceptorResponse rewrittenResponse = prepareInterceptorResponse(query);
    ApolloInterceptor interceptor = new ApolloInterceptor() {

        @Override
        public void interceptAsync(@Nonnull InterceptorRequest request, @Nonnull ApolloInterceptorChain chain, @Nonnull Executor dispatcher, @Nonnull final CallBack callBack) {
            chain.proceedAsync(request, dispatcher, new CallBack() {

                @Override
                public void onResponse(@Nonnull InterceptorResponse response) {
                    callBack.onResponse(rewrittenResponse);
                }

                @Override
                public void onFailure(@Nonnull ApolloException e) {
                    throw new RuntimeException(e);
                }

                @Override
                public void onCompleted() {
                    callBack.onCompleted();
                }

                @Override
                public void onFetch(FetchSourceType sourceType) {
                    callBack.onFetch(sourceType);
                }
            });
        }

        @Override
        public void dispose() {
        }
    };
    client = createApolloClient(interceptor);
    Rx2Apollo.from(client.query(query)).test().assertValue(rewrittenResponse.parsedResponse.get());
}
Also used : EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) Executor(java.util.concurrent.Executor) InterceptorResponse(com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse) Nonnull(javax.annotation.Nonnull) ApolloException(com.apollographql.apollo.exception.ApolloException) ApolloInterceptorChain(com.apollographql.apollo.interceptor.ApolloInterceptorChain) ApolloInterceptor(com.apollographql.apollo.interceptor.ApolloInterceptor) Test(org.junit.Test)

Example 4 with InterceptorResponse

use of com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse in project apollo-android by apollographql.

the class ApolloInterceptorChainTest method onProceedAsyncCalled_correctExceptionIsCaught.

@Test
public void onProceedAsyncCalled_correctExceptionIsCaught() throws TimeoutException, InterruptedException {
    final AtomicInteger counter = new AtomicInteger(1);
    final String message = "ApolloException";
    EpisodeHeroNameQuery query = createQuery();
    ApolloInterceptor interceptor = new ApolloInterceptor() {

        @Override
        public void interceptAsync(@Nonnull InterceptorRequest request, @Nonnull ApolloInterceptorChain chain, @Nonnull Executor dispatcher, @Nonnull final CallBack callBack) {
            dispatcher.execute(new Runnable() {

                @Override
                public void run() {
                    ApolloException apolloException = new ApolloException(message);
                    callBack.onFailure(apolloException);
                }
            });
        }

        @Override
        public void dispose() {
        }
    };
    List<ApolloInterceptor> interceptors = Collections.singletonList(interceptor);
    RealApolloInterceptorChain chain = new RealApolloInterceptorChain(interceptors);
    chain.proceedAsync(ApolloInterceptor.InterceptorRequest.builder(query).fetchFromCache(false).build(), Utils.immediateExecutor(), new CallBack() {

        @Override
        public void onResponse(@Nonnull InterceptorResponse response) {
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
            assertThat(e.getMessage()).isEqualTo(message);
            counter.decrementAndGet();
        }

        @Override
        public void onCompleted() {
        }

        @Override
        public void onFetch(ApolloInterceptor.FetchSourceType sourceType) {
        }
    });
    if (counter.get() != 0) {
        Assert.fail("Exception thrown by Interceptor not caught");
    }
}
Also used : CallBack(com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack) Nonnull(javax.annotation.Nonnull) RealApolloInterceptorChain(com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain) ApolloInterceptorChain(com.apollographql.apollo.interceptor.ApolloInterceptorChain) RealApolloInterceptorChain(com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) Executor(java.util.concurrent.Executor) InterceptorResponse(com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ApolloException(com.apollographql.apollo.exception.ApolloException) ApolloInterceptor(com.apollographql.apollo.interceptor.ApolloInterceptor) Test(org.junit.Test)

Example 5 with InterceptorResponse

use of com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse in project apollo-android by apollographql.

the class ApolloInterceptorChainTest method prepareInterceptorResponse.

@NonNull
private InterceptorResponse prepareInterceptorResponse(EpisodeHeroNameQuery query) {
    Request request = new Request.Builder().url("https://localhost:8080/").build();
    okhttp3.Response okHttpResponse = new okhttp3.Response.Builder().request(request).protocol(Protocol.HTTP_2).code(200).message("Intercepted").body(ResponseBody.create(MediaType.parse("text/plain; charset=utf-8"), "fakeResponse")).build();
    Response<EpisodeHeroNameQuery.Data> apolloResponse = Response.<EpisodeHeroNameQuery.Data>builder(query).build();
    return new InterceptorResponse(okHttpResponse, apolloResponse, Collections.<Record>emptyList());
}
Also used : EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) InterceptorResponse(com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse) Request(okhttp3.Request) NonNull(android.support.annotation.NonNull)

Aggregations

EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)8 InterceptorResponse (com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse)8 ApolloInterceptor (com.apollographql.apollo.interceptor.ApolloInterceptor)6 Test (org.junit.Test)6 ApolloException (com.apollographql.apollo.exception.ApolloException)5 ApolloInterceptorChain (com.apollographql.apollo.interceptor.ApolloInterceptorChain)4 Executor (java.util.concurrent.Executor)4 Nonnull (javax.annotation.Nonnull)4 CallBack (com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack)3 RealApolloInterceptorChain (com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 NonNull (android.support.annotation.NonNull)2 Request (okhttp3.Request)2 Utils.assertResponse (com.apollographql.apollo.Utils.assertResponse)1 Utils.enqueueAndAssertResponse (com.apollographql.apollo.Utils.enqueueAndAssertResponse)1 Response (com.apollographql.apollo.api.Response)1 ApolloParseException (com.apollographql.apollo.exception.ApolloParseException)1 IOException (java.io.IOException)1 TimeoutException (java.util.concurrent.TimeoutException)1 MockResponse (okhttp3.mockwebserver.MockResponse)1