Skip to content

Instantly share code, notes, and snippets.

@swankjesse
Created April 26, 2016 02:16
Show Gist options
  • Save swankjesse/240132f0d750a05d0380f32f7f822531 to your computer and use it in GitHub Desktop.
Save swankjesse/240132f0d750a05d0380f32f7f822531 to your computer and use it in GitHub Desktop.
package okhttp3;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.Rule;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class UrlConnectionProxyTest {
@Rule public final MockWebServer server = new MockWebServer();
private final Proxy BAD_PROXY = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.5.5", 1234));
/**
* This connection will work. Because it tries the bad proxy, that fails,
* then it bypasses the proxy and tries a direct connection.
*
* the .proxySelector setting here is to simulate Android's default proxy selector with a single proxy in it.
*
* @throws Exception
*/
@Test
public void testBypassProxy() throws Exception {
server.enqueue(new MockResponse()
.addHeader("Content-Type: text/plain")
.setBody("abc"));
boolean connectionFailed = false;
ProxySelector defaultProxySelector = ProxySelector.getDefault();
ProxySelector.setDefault(new BadProxySelector());
try {
HttpURLConnection urlConnection
= (HttpURLConnection) server.url("/path").url().openConnection();
urlConnection.setConnectTimeout(1000);
int responseCode = urlConnection.getResponseCode();
System.out.println(responseCode);
} catch (Exception e) {
System.out.println("call.execute: " + e);
connectionFailed = true;
} finally {
ProxySelector.setDefault(defaultProxySelector);
}
assertTrue(connectionFailed);
}
/**
* This connection will properly fail, because a proxy is set, and OkHttp will always use that
*
* @throws Exception
*/
@Test
public void testForceProxy() throws Exception {
server.enqueue(new MockResponse()
.addHeader("Content-Type: text/plain")
.setBody("abc"));
boolean connectionFailed = false;
try {
HttpURLConnection urlConnection
= (HttpURLConnection) server.url("/path").url().openConnection(BAD_PROXY);
urlConnection.setConnectTimeout(1000);
int responseCode = urlConnection.getResponseCode();
System.out.println(responseCode);
} catch (Exception e) {
System.out.println("call.execute: " + e);
connectionFailed = true;
}
assertTrue(connectionFailed);
}
class BadProxySelector extends ProxySelector {
@Override
public List<Proxy> select(final URI uri) {
return Arrays.asList(BAD_PROXY, Proxy.NO_PROXY);
}
@Override
public void connectFailed(final URI uri, final SocketAddress address, final IOException failure) {
System.out.println("Proxy connection failed. This should happen once.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment