Search in sources :

Example 1 with AllPlanetsQuery

use of com.apollographql.apollo.integration.httpcache.AllPlanetsQuery in project apollo-android by apollographql.

the class ApolloPrefetchTest method prefetchDefault.

@Test
public void prefetchDefault() throws IOException, ApolloException {
    server.enqueue(mockResponse("HttpCacheTestAllPlanets.json"));
    prefetch(apolloClient.prefetch(new AllPlanetsQuery()));
    checkCachedResponse("HttpCacheTestAllPlanets.json");
    assertResponse(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.CACHE_ONLY.expireAfter(2, TimeUnit.SECONDS)), new Predicate<Response<AllPlanetsQuery.Data>>() {

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> dataResponse) throws Exception {
            return !dataResponse.hasErrors();
        }
    });
}
Also used : Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Utils.assertResponse(com.apollographql.apollo.Utils.assertResponse) Response(com.apollographql.apollo.api.Response) AllPlanetsQuery(com.apollographql.apollo.integration.httpcache.AllPlanetsQuery) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Test(org.junit.Test)

Example 2 with AllPlanetsQuery

use of com.apollographql.apollo.integration.httpcache.AllPlanetsQuery in project apollo-android by apollographql.

the class HttpCacheTest method noCacheStore.

@Test
public void noCacheStore() throws Exception {
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    ApolloClient apolloClient = ApolloClient.builder().serverUrl(server.url("/")).okHttpClient(new OkHttpClient.Builder().addInterceptor(new TrackingInterceptor()).dispatcher(new Dispatcher(Utils.immediateExecutorService())).build()).dispatcher(Utils.immediateExecutor()).build();
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery())).test().assertValue(new Predicate<Response<AllPlanetsQuery.Data>>() {

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    checkNoCachedResponse();
}
Also used : Response(com.apollographql.apollo.api.Response) MockResponse(okhttp3.mockwebserver.MockResponse) AllPlanetsQuery(com.apollographql.apollo.integration.httpcache.AllPlanetsQuery) Dispatcher(okhttp3.Dispatcher) ApolloHttpException(com.apollographql.apollo.exception.ApolloHttpException) ParseException(java.text.ParseException) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Test(org.junit.Test)

Example 3 with AllPlanetsQuery

use of com.apollographql.apollo.integration.httpcache.AllPlanetsQuery in project apollo-android by apollographql.

the class HttpCacheTest method cacheStaleBeforeNetwork.

@Test
@SuppressWarnings("CheckReturnValue")
public void cacheStaleBeforeNetwork() throws Exception {
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery())).test();
    assertThat(server.getRequestCount()).isEqualTo(1);
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.NETWORK_FIRST)).test();
    assertThat(server.getRequestCount()).isEqualTo(2);
    assertThat(lastHttResponse.networkResponse()).isNotNull();
    assertThat(lastHttResponse.cacheResponse()).isNull();
    checkCachedResponse("/HttpCacheTestAllPlanets.json");
}
Also used : AllPlanetsQuery(com.apollographql.apollo.integration.httpcache.AllPlanetsQuery) Test(org.junit.Test)

Example 4 with AllPlanetsQuery

use of com.apollographql.apollo.integration.httpcache.AllPlanetsQuery in project apollo-android by apollographql.

the class HttpCacheTest method prematureDisconnect.

@Test
public void prematureDisconnect() throws Exception {
    MockResponse mockResponse = mockResponse("/HttpCacheTestAllPlanets.json");
    Buffer truncatedBody = new Buffer();
    truncatedBody.write(mockResponse.getBody(), 16);
    mockResponse.setBody(truncatedBody);
    server.enqueue(mockResponse);
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.NETWORK_ONLY)).test().assertError(ApolloException.class);
    checkNoCachedResponse();
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) AllPlanetsQuery(com.apollographql.apollo.integration.httpcache.AllPlanetsQuery) Test(org.junit.Test)

Example 5 with AllPlanetsQuery

use of com.apollographql.apollo.integration.httpcache.AllPlanetsQuery in project apollo-android by apollographql.

the class HttpCacheTest method networkOnly.

@Test
public void networkOnly() throws Exception {
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.NETWORK_ONLY)).test().assertValue(new Predicate<Response<AllPlanetsQuery.Data>>() {

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    assertThat(server.getRequestCount()).isEqualTo(1);
    assertThat(lastHttResponse.networkResponse()).isNotNull();
    assertThat(lastHttResponse.cacheResponse()).isNull();
    checkCachedResponse("/HttpCacheTestAllPlanets.json");
}
Also used : Response(com.apollographql.apollo.api.Response) MockResponse(okhttp3.mockwebserver.MockResponse) AllPlanetsQuery(com.apollographql.apollo.integration.httpcache.AllPlanetsQuery) ApolloHttpException(com.apollographql.apollo.exception.ApolloHttpException) ParseException(java.text.ParseException) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Test(org.junit.Test)

Aggregations

AllPlanetsQuery (com.apollographql.apollo.integration.httpcache.AllPlanetsQuery)21 Test (org.junit.Test)21 Response (com.apollographql.apollo.api.Response)12 MockResponse (okhttp3.mockwebserver.MockResponse)12 ApolloException (com.apollographql.apollo.exception.ApolloException)11 IOException (java.io.IOException)11 ApolloHttpException (com.apollographql.apollo.exception.ApolloHttpException)10 ParseException (java.text.ParseException)10 Utils.assertResponse (com.apollographql.apollo.Utils.assertResponse)2 Utils.enqueueAndAssertResponse (com.apollographql.apollo.Utils.enqueueAndAssertResponse)2 Utils.mockResponse (com.apollographql.apollo.Utils.mockResponse)2 Buffer (okio.Buffer)2 Utils.cacheAndAssertCachedResponse (com.apollographql.apollo.Utils.cacheAndAssertCachedResponse)1 DiskLruHttpCacheStore (com.apollographql.apollo.cache.http.DiskLruHttpCacheStore)1 Record (com.apollographql.apollo.cache.normalized.Record)1 JsonWriter (com.apollographql.apollo.internal.json.JsonWriter)1 OperationJsonWriter (com.apollographql.apollo.response.OperationJsonWriter)1 ScalarTypeAdapters (com.apollographql.apollo.response.ScalarTypeAdapters)1 File (java.io.File)1 Dispatcher (okhttp3.Dispatcher)1