use of com.apollographql.apollo.interceptor.ApolloInterceptor 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());
}
use of com.apollographql.apollo.interceptor.ApolloInterceptor 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");
}
}
use of com.apollographql.apollo.interceptor.ApolloInterceptor in project apollo-android by apollographql.
the class ApolloInterceptorChainTest method onProceedAsyncCalled_correctInterceptorResponseIsReceived.
@Test
public void onProceedAsyncCalled_correctInterceptorResponseIsReceived() throws TimeoutException, InterruptedException {
final AtomicInteger counter = new AtomicInteger(1);
EpisodeHeroNameQuery query = createQuery();
final InterceptorResponse expectedResponse = prepareInterceptorResponse(query);
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() {
callBack.onResponse(expectedResponse);
}
});
}
@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) {
assertThat(response).isEqualTo(expectedResponse);
counter.decrementAndGet();
}
@Override
public void onFailure(@Nonnull ApolloException e) {
}
@Override
public void onCompleted() {
}
@Override
public void onFetch(ApolloInterceptor.FetchSourceType sourceType) {
}
});
if (counter.get() != 0) {
Assert.fail("Interceptor's response not received");
}
}
use of com.apollographql.apollo.interceptor.ApolloInterceptor in project apollo-android by apollographql.
the class ApolloInterceptorTest method asyncApplicationInterceptorThrowsRuntimeException.
@Test
public void asyncApplicationInterceptorThrowsRuntimeException() throws TimeoutException, InterruptedException {
final String message = "RuntimeException";
EpisodeHeroNameQuery query = createHeroNameQuery();
ApolloInterceptor interceptor = new ApolloInterceptor() {
@Override
public void interceptAsync(@Nonnull InterceptorRequest request, @Nonnull ApolloInterceptorChain chain, @Nonnull Executor dispatcher, @Nonnull CallBack callBack) {
dispatcher.execute(new Runnable() {
@Override
public void run() {
throw new RuntimeException(message);
}
});
}
@Override
public void dispose() {
}
};
client = createApolloClient(interceptor);
Rx2Apollo.from(client.query(query)).test().assertError(new Predicate<Throwable>() {
@Override
public boolean test(Throwable throwable) throws Exception {
return throwable instanceof RuntimeException && message.equals(throwable.getMessage());
}
});
}
use of com.apollographql.apollo.interceptor.ApolloInterceptor in project apollo-android by apollographql.
the class RealApolloCall method prepareInterceptorChain.
private ApolloInterceptorChain prepareInterceptorChain(Operation operation) {
List<ApolloInterceptor> interceptors = new ArrayList<>();
HttpCachePolicy.Policy httpCachePolicy = operation instanceof Query ? this.httpCachePolicy : null;
ResponseFieldMapper responseFieldMapper = responseFieldMapperFactory.create(operation);
interceptors.addAll(applicationInterceptors);
interceptors.add(responseFetcher.provideInterceptor(logger));
interceptors.add(new ApolloCacheInterceptor(apolloStore, responseFieldMapper, dispatcher, logger));
interceptors.add(new ApolloParseInterceptor(httpCache, apolloStore.networkResponseNormalizer(), responseFieldMapper, scalarTypeAdapters, logger));
interceptors.add(new ApolloServerInterceptor(serverUrl, httpCallFactory, httpCachePolicy, false, scalarTypeAdapters, logger, sendOperationdIdentifiers));
return new RealApolloInterceptorChain(interceptors);
}
Aggregations