Search in sources :

Example 16 with AllPlanetsQuery

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

the class HttpCacheTest method fileSystemReadFailure.

@Test
public void fileSystemReadFailure() throws IOException, ApolloException {
    FaultyHttpCacheStore faultyCacheStore = new FaultyHttpCacheStore(inMemoryFileSystem);
    cacheStore.delegate = faultyCacheStore;
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    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();
        }
    });
    checkCachedResponse("/HttpCacheTestAllPlanets.json");
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    faultyCacheStore.failStrategy(FaultyHttpCacheStore.FailStrategy.FAIL_HEADER_READ);
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.CACHE_FIRST)).test().assertValue(new Predicate<Response<AllPlanetsQuery.Data>>() {

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    assertThat(server.getRequestCount()).isEqualTo(2);
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    faultyCacheStore.failStrategy(FaultyHttpCacheStore.FailStrategy.FAIL_BODY_READ);
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.CACHE_FIRST)).test().assertError(Exception.class);
    assertThat(server.getRequestCount()).isEqualTo(2);
}
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)

Example 17 with AllPlanetsQuery

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

the class HttpCacheTest method cacheOnlyHit.

@Test
public void cacheOnlyHit() throws Exception {
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    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();
        }
    });
    assertThat(server.takeRequest()).isNotNull();
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.CACHE_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()).isNull();
    assertThat(lastHttResponse.cacheResponse()).isNotNull();
    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)

Example 18 with AllPlanetsQuery

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

the class HttpCacheTest method fromCacheFlag.

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

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
            return !response.hasErrors() && !response.fromCache();
        }
    });
    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() && !response.fromCache();
        }
    });
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.CACHE_ONLY)).test().assertValue(new Predicate<Response<AllPlanetsQuery.Data>>() {

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
            return !response.hasErrors() && response.fromCache();
        }
    });
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.CACHE_FIRST)).test().assertValue(new Predicate<Response<AllPlanetsQuery.Data>>() {

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
            return !response.hasErrors() && response.fromCache();
        }
    });
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.CACHE_FIRST)).test().assertValue(new Predicate<Response<AllPlanetsQuery.Data>>() {

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
            return !response.hasErrors() && response.fromCache();
        }
    });
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.NETWORK_FIRST)).test().assertValue(new Predicate<Response<AllPlanetsQuery.Data>>() {

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
            return !response.hasErrors() && response.fromCache();
        }
    });
}
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)

Example 19 with AllPlanetsQuery

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

the class HttpCacheTest method fileSystemUnavailable.

@Test
public void fileSystemUnavailable() throws IOException, ApolloException {
    cacheStore.delegate = new DiskLruHttpCacheStore(new NoFileSystem(), new File("/cache/"), Integer.MAX_VALUE);
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    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) DiskLruHttpCacheStore(com.apollographql.apollo.cache.http.DiskLruHttpCacheStore) File(java.io.File) 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 20 with AllPlanetsQuery

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

the class HttpCacheTest method expireAfterRead.

@Test
public void expireAfterRead() throws IOException, ApolloException {
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    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();
        }
    });
    checkCachedResponse("/HttpCacheTestAllPlanets.json");
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.CACHE_ONLY.expireAfterRead())).test().assertValue(new Predicate<Response<AllPlanetsQuery.Data>>() {

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    checkNoCachedResponse();
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.CACHE_ONLY)).test().assertError(Exception.class);
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    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();
        }
    });
    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