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());
}
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;
}
});
}
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());
}
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");
}
}
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());
}
Aggregations