99import com .danikula .videocache .support .ProxyCacheTestUtils ;
1010import com .danikula .videocache .support .Response ;
1111
12+ import junit .framework .Assert ;
13+
1214import org .junit .Before ;
1315import org .junit .Test ;
1416import org .robolectric .RuntimeEnvironment ;
3133import static com .danikula .videocache .support .ProxyCacheTestUtils .HTTP_DATA_URL_ONE_REDIRECT ;
3234import static com .danikula .videocache .support .ProxyCacheTestUtils .getFileContent ;
3335import static com .danikula .videocache .support .ProxyCacheTestUtils .getPort ;
36+ import static com .danikula .videocache .support .ProxyCacheTestUtils .getPortWithoutPing ;
37+ import static com .danikula .videocache .support .ProxyCacheTestUtils .installExternalSystemProxy ;
3438import static com .danikula .videocache .support .ProxyCacheTestUtils .loadAssetFile ;
3539import static com .danikula .videocache .support .ProxyCacheTestUtils .readProxyResponse ;
40+ import static com .danikula .videocache .support .ProxyCacheTestUtils .resetSystemProxy ;
3641import static org .fest .assertions .api .Assertions .assertThat ;
3742
3843/**
@@ -47,6 +52,7 @@ public void setup() throws Exception {
4752 cacheFolder = ProxyCacheTestUtils .newCacheFile ();
4853 createDirectory (cacheFolder );
4954 cleanDirectory (cacheFolder );
55+ resetSystemProxy ();
5056 }
5157
5258 @ Test
@@ -338,6 +344,25 @@ public void testTrimFileCacheForTotalSizeLru() throws Exception {
338344 assertThat (new File (cacheFolder , fileNameGenerator .generate (HTTP_DATA_URL ))).doesNotExist ();
339345 }
340346
347+ @ Test // https://github.com/danikula/AndroidVideoCache/issues/28
348+ public void testWorkWithExternalProxy () throws Exception {
349+ installExternalSystemProxy ();
350+
351+ Pair <File , Response > response = readProxyData (HTTP_DATA_URL , 0 );
352+ assertThat (response .second .data ).isEqualTo (loadAssetFile (ASSETS_DATA_NAME ));
353+ }
354+
355+ @ Test (expected = IOException .class ) // https://github.com/danikula/AndroidVideoCache/issues/28
356+ public void testDoesNotWorkWithoutCustomProxySelector () throws Exception {
357+ HttpProxyCacheServer httpProxyCacheServer = new HttpProxyCacheServer (RuntimeEnvironment .application );
358+ // IgnoreHostProxySelector is set in HttpProxyCacheServer constructor. So let reset it by custom.
359+ installExternalSystemProxy ();
360+
361+ String proxiedUrl = "http://127.0.0.1:" + getPortWithoutPing (httpProxyCacheServer ) + "/" + HTTP_DATA_URL ;
362+ readProxyResponse (httpProxyCacheServer , proxiedUrl );
363+ Assert .fail (); // should throw IOException on the previous line
364+ }
365+
341366 private Pair <File , Response > readProxyData (String url , int offset ) throws IOException {
342367 File file = file (cacheFolder , url );
343368 HttpProxyCacheServer proxy = newProxy (cacheFolder );
0 commit comments