|
| 1 | +package com.koushikdutta.async.test; |
| 2 | + |
| 3 | +import android.os.Environment; |
| 4 | +import com.koushikdutta.async.AsyncServer; |
| 5 | +import com.koushikdutta.async.http.AsyncHttpClient; |
| 6 | +import com.koushikdutta.async.http.ResponseCacheMiddleware; |
| 7 | +import com.koushikdutta.async.http.libcore.HttpDate; |
| 8 | +import com.koushikdutta.async.http.server.AsyncHttpServer; |
| 9 | +import com.koushikdutta.async.http.server.AsyncHttpServerRequest; |
| 10 | +import com.koushikdutta.async.http.server.AsyncHttpServerResponse; |
| 11 | +import com.koushikdutta.async.http.server.HttpServerRequestCallback; |
| 12 | +import junit.framework.TestCase; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.util.Date; |
| 16 | + |
| 17 | +/** |
| 18 | + * Created by koush on 6/13/13. |
| 19 | + */ |
| 20 | +public class CacheTests extends TestCase { |
| 21 | + public void testMaxAgePrivate() throws Exception { |
| 22 | + AsyncHttpClient client = new AsyncHttpClient(AsyncServer.getDefault()); |
| 23 | + ResponseCacheMiddleware cache = ResponseCacheMiddleware.addCache(client, new File(Environment.getExternalStorageDirectory(), "AndroidAsyncTest"), 1024 * 1024 * 10); |
| 24 | + AsyncHttpServer httpServer = new AsyncHttpServer(); |
| 25 | + try { |
| 26 | + httpServer.get("/uname/(.*)", new HttpServerRequestCallback() { |
| 27 | + @Override |
| 28 | + public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) { |
| 29 | + response.getHeaders().getHeaders().set("Date", HttpDate.format(new Date())); |
| 30 | + response.getHeaders().getHeaders().set("Cache-Control", "private, max-age=10000"); |
| 31 | + response.send(request.getMatcher().group(1)); |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + httpServer.listen(AsyncServer.getDefault(), 5555); |
| 36 | + // clear the old cache |
| 37 | + cache.clear(); |
| 38 | + |
| 39 | + client.getString("http://localhost:5555/uname/43434").get(); |
| 40 | + |
| 41 | + client.getString("http://localhost:5555/uname/43434").get(); |
| 42 | + |
| 43 | + |
| 44 | + assertEquals(cache.getCacheHitCount(), 1); |
| 45 | + assertEquals(cache.getNetworkCount(), 1); |
| 46 | + } |
| 47 | + finally { |
| 48 | + AsyncServer.getDefault().stop(); |
| 49 | + client.getMiddleware().remove(cache); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments