// Path: test/src/test/java/com/danikula/videocache/support/ProxyCacheTestUtils.java
// public static int getPort(HttpProxyCacheServer server) {
//     String proxyUrl = server.getProxyUrl("test");
//     Pattern pattern = Pattern.compile("http://127.0.0.1:(\\d*)/test");
//     Matcher matcher = pattern.matcher(proxyUrl);
//     assertThat(matcher.find()).isTrue();
//     String portAsString = matcher.group(1);
//     return Integer.parseInt(portAsString);
// }
// 
// Path: test/src/test/java/com/danikula/videocache/support/ProxyCacheTestUtils.java
// public static int getPortWithoutPing(HttpProxyCacheServer server) {
//     return (Integer) ReflectUtil.getField(server, "port");
// }
// 
// Path: test/src/test/java/com/danikula/videocache/support/ProxyCacheTestUtils.java
// public static void installExternalSystemProxy() {
//     // see proxies list at http://proxylist.hidemyass.com/
//     Proxy systemProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("162.8.230.7", 11180));
//     ProxySelector mockedProxySelector = Mockito.mock(ProxySelector.class);
//     when(mockedProxySelector.select(Mockito.<URI>any())).thenReturn(Lists.newArrayList(systemProxy));
//     ProxySelector.setDefault(mockedProxySelector);
// }
// 
// Path: test/src/test/java/com/danikula/videocache/support/ProxyCacheTestUtils.java
// public static void resetSystemProxy() {
//     ProxySelector mockedProxySelector = Mockito.mock(ProxySelector.class);
//     when(mockedProxySelector.select(Mockito.<URI>any())).thenReturn(Lists.newArrayList(Proxy.NO_PROXY));
//     ProxySelector.setDefault(mockedProxySelector);
// }


import org.junit.Before;
import org.junit.Test;
import org.robolectric.RuntimeEnvironment;
import java.io.ByteArrayOutputStream;
import java.net.Socket;
import static com.danikula.videocache.support.ProxyCacheTestUtils.getPort;
import static com.danikula.videocache.support.ProxyCacheTestUtils.getPortWithoutPing;
import static com.danikula.videocache.support.ProxyCacheTestUtils.installExternalSystemProxy;
import static com.danikula.videocache.support.ProxyCacheTestUtils.resetSystemProxy;
import static org.fest.assertions.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

    @Test
    public void testPingFail() throws Exception {
        Pinger pinger = new Pinger("127.0.0.1", 33);
        boolean pinged = pinger.ping(3, 70);
        assertThat(pinged).isFalse();
    }

    @Test
    public void testIsPingRequest() throws Exception {
        Pinger pinger = new Pinger("127.0.0.1", 1);
        assertThat(pinger.isPingRequest("ping")).isTrue();
        assertThat(pinger.isPingRequest("notPing")).isFalse();
    }

    @Test
    public void testResponseToPing() throws Exception {
        Pinger pinger = new Pinger("127.0.0.1", 1);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Socket socket = mock(Socket.class);
        when(socket.getOutputStream()).thenReturn(out);
        pinger.responseToPing(socket);
        assertThat(out.toString()).isEqualTo("HTTP/1.1 200 OK\n\nping ok");
    }

    @Test // https://github.com/danikula/AndroidVideoCache/issues/28
    public void testPingedWithExternalProxy() throws Exception {
        installExternalSystemProxy();

        HttpProxyCacheServer server = new HttpProxyCacheServer(RuntimeEnvironment.application);
// Path: test/src/test/java/com/danikula/videocache/support/ProxyCacheTestUtils.java
// public static int getPort(HttpProxyCacheServer server) {
//     String proxyUrl = server.getProxyUrl("test");
//     Pattern pattern = Pattern.compile("http://127.0.0.1:(\\d*)/test");
//     Matcher matcher = pattern.matcher(proxyUrl);
//     assertThat(matcher.find()).isTrue();
//     String portAsString = matcher.group(1);
//     return Integer.parseInt(portAsString);
// }
// 
// Path: test/src/test/java/com/danikula/videocache/support/ProxyCacheTestUtils.java
// public static int getPortWithoutPing(HttpProxyCacheServer server) {
//     return (Integer) ReflectUtil.getField(server, "port");
// }
// 
// Path: test/src/test/java/com/danikula/videocache/support/ProxyCacheTestUtils.java
// public static void installExternalSystemProxy() {
//     // see proxies list at http://proxylist.hidemyass.com/
//     Proxy systemProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("162.8.230.7", 11180));
//     ProxySelector mockedProxySelector = Mockito.mock(ProxySelector.class);
//     when(mockedProxySelector.select(Mockito.<URI>any())).thenReturn(Lists.newArrayList(systemProxy));
//     ProxySelector.setDefault(mockedProxySelector);
// }
// 
// Path: test/src/test/java/com/danikula/videocache/support/ProxyCacheTestUtils.java
// public static void resetSystemProxy() {
//     ProxySelector mockedProxySelector = Mockito.mock(ProxySelector.class);
//     when(mockedProxySelector.select(Mockito.<URI>any())).thenReturn(Lists.newArrayList(Proxy.NO_PROXY));
//     ProxySelector.setDefault(mockedProxySelector);
// }


// Path: test/src/test/java/com/danikula/videocache/PingerTest.java
import org.junit.Before;
import org.junit.Test;
import org.robolectric.RuntimeEnvironment;
import java.io.ByteArrayOutputStream;
import java.net.Socket;
import static com.danikula.videocache.support.ProxyCacheTestUtils.getPort;
import static com.danikula.videocache.support.ProxyCacheTestUtils.getPortWithoutPing;
import static com.danikula.videocache.support.ProxyCacheTestUtils.installExternalSystemProxy;
import static com.danikula.videocache.support.ProxyCacheTestUtils.resetSystemProxy;
import static org.fest.assertions.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;


    @Test
    public void testPingFail() throws Exception {
        Pinger pinger = new Pinger("127.0.0.1", 33);
        boolean pinged = pinger.ping(3, 70);
        assertThat(pinged).isFalse();
    }

    @Test
    public void testIsPingRequest() throws Exception {
        Pinger pinger = new Pinger("127.0.0.1", 1);
        assertThat(pinger.isPingRequest("ping")).isTrue();
        assertThat(pinger.isPingRequest("notPing")).isFalse();
    }

    @Test
    public void testResponseToPing() throws Exception {
        Pinger pinger = new Pinger("127.0.0.1", 1);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Socket socket = mock(Socket.class);
        when(socket.getOutputStream()).thenReturn(out);
        pinger.responseToPing(socket);
        assertThat(out.toString()).isEqualTo("HTTP/1.1 200 OK\n\nping ok");
    }

    @Test // https://github.com/danikula/AndroidVideoCache/issues/28
    public void testPingedWithExternalProxy() throws Exception {
        installExternalSystemProxy();

        HttpProxyCacheServer server = new HttpProxyCacheServer(RuntimeEnvironment.application);
        Pinger pinger = new Pinger("127.0.0.1", getPortWithoutPing(server));