Search in sources :

Example 1 with ResponseCacheMiddleware

use of com.koushikdutta.async.http.cache.ResponseCacheMiddleware in project AndroidAsync by koush.

the class HttpClientTests method testCache.

public void testCache() throws Exception {
    ResponseCacheMiddleware cache = ResponseCacheMiddleware.addCache(client, new File(getContext().getFilesDir(), "AndroidAsyncTest"), 1024 * 1024 * 10);
    try {
        // clear the old cache
        cache.clear();
        // populate the cache
        testGithubRandomData();
        // this should result in a conditional cache hit
        testGithubRandomData();
        assertEquals(cache.getCacheHitCount(), 1);
    } finally {
        client.getMiddleware().remove(cache);
    }
}
Also used : ResponseCacheMiddleware(com.koushikdutta.async.http.cache.ResponseCacheMiddleware) File(java.io.File)

Example 2 with ResponseCacheMiddleware

use of com.koushikdutta.async.http.cache.ResponseCacheMiddleware in project AndroidAsync by koush.

the class CacheTests method testMaxAgePrivate.

public void testMaxAgePrivate() throws Exception {
    AsyncHttpClient client = new AsyncHttpClient(AsyncServer.getDefault());
    ResponseCacheMiddleware cache = ResponseCacheMiddleware.addCache(client, new File(getContext().getFilesDir(), "AndroidAsyncTest"), 1024 * 1024 * 10);
    AsyncHttpServer httpServer = new AsyncHttpServer();
    try {
        httpServer.get("/uname/(.*)", new HttpServerRequestCallback() {

            @Override
            public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
                response.getHeaders().set("Date", HttpDate.format(new Date()));
                response.getHeaders().set("Cache-Control", "private, max-age=10000");
                response.send(request.getMatcher().group(1));
            }
        });
        AsyncServerSocket socket = httpServer.listen(AsyncServer.getDefault(), 0);
        int port = socket.getLocalPort();
        // clear the old cache
        cache.clear();
        client.executeString(new AsyncHttpGet("http://localhost:" + port + "/uname/43434"), null).get();
        client.executeString(new AsyncHttpGet("http://localhost:" + port + "/uname/43434"), null).get();
        assertEquals(cache.getCacheHitCount(), 1);
        assertEquals(cache.getNetworkCount(), 1);
    } finally {
        AsyncServer.getDefault().stop();
        client.getMiddleware().remove(cache);
    }
}
Also used : AsyncServerSocket(com.koushikdutta.async.AsyncServerSocket) AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) ResponseCacheMiddleware(com.koushikdutta.async.http.cache.ResponseCacheMiddleware) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) File(java.io.File) Date(java.util.Date) HttpDate(com.koushikdutta.async.http.HttpDate) AsyncHttpClient(com.koushikdutta.async.http.AsyncHttpClient)

Aggregations

ResponseCacheMiddleware (com.koushikdutta.async.http.cache.ResponseCacheMiddleware)2 File (java.io.File)2 AsyncServerSocket (com.koushikdutta.async.AsyncServerSocket)1 AsyncHttpClient (com.koushikdutta.async.http.AsyncHttpClient)1 AsyncHttpGet (com.koushikdutta.async.http.AsyncHttpGet)1 HttpDate (com.koushikdutta.async.http.HttpDate)1 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)1 AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)1 AsyncHttpServerResponse (com.koushikdutta.async.http.server.AsyncHttpServerResponse)1 HttpServerRequestCallback (com.koushikdutta.async.http.server.HttpServerRequestCallback)1 Date (java.util.Date)1