Search in sources :

Example 1 with InternalCache

use of okhttp3.internal.cache.InternalCache in project okhttp by square.

the class URLEncodingTest method backdoorUrlToUri.

private URI backdoorUrlToUri(URL url) throws Exception {
    final AtomicReference<URI> uriReference = new AtomicReference<>();
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    Internal.instance.setCache(builder, new InternalCache() {

        @Override
        public Response get(Request request) throws IOException {
            uriReference.set(request.url().uri());
            throw new UnsupportedOperationException();
        }

        @Override
        public CacheRequest put(Response response) throws IOException {
            return null;
        }

        @Override
        public void remove(Request request) throws IOException {
        }

        @Override
        public void update(Response cached, Response network) {
        }

        @Override
        public void trackConditionalCacheHit() {
        }

        @Override
        public void trackResponse(CacheStrategy cacheStrategy) {
        }
    });
    try {
        HttpURLConnection connection = new OkUrlFactory(builder.build()).open(url);
        connection.getResponseCode();
    } catch (Exception expected) {
        if (expected.getCause() instanceof URISyntaxException) {
            expected.printStackTrace();
        }
    }
    return uriReference.get();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) CacheRequest(okhttp3.internal.cache.CacheRequest) InternalCache(okhttp3.internal.cache.InternalCache) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Response(okhttp3.Response) OkUrlFactory(okhttp3.OkUrlFactory) HttpURLConnection(java.net.HttpURLConnection) CacheRequest(okhttp3.internal.cache.CacheRequest) CacheStrategy(okhttp3.internal.cache.CacheStrategy)

Aggregations

IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 OkHttpClient (okhttp3.OkHttpClient)1 OkUrlFactory (okhttp3.OkUrlFactory)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 CacheRequest (okhttp3.internal.cache.CacheRequest)1 CacheStrategy (okhttp3.internal.cache.CacheStrategy)1 InternalCache (okhttp3.internal.cache.InternalCache)1