use of java.net.Proxy in project robovm by robovm.
the class OldProxyTest method test_hashCode.
public void test_hashCode() {
SocketAddress address1 = new InetSocketAddress("127.0.0.1", 1234);
Proxy proxy1 = new Proxy(Proxy.Type.HTTP, address1);
Proxy proxy2 = new Proxy(Proxy.Type.HTTP, address1);
assertTrue(proxy1.hashCode() == proxy2.hashCode());
new Proxy(Proxy.Type.SOCKS, address1);
Proxy proxy4 = new Proxy(Proxy.Type.SOCKS, address1);
assertTrue(proxy1.hashCode() == proxy2.hashCode());
assertTrue(proxy1.hashCode() != proxy4.hashCode());
SocketAddress address2 = new InetSocketAddress("127.0.0.1", 1235);
Proxy proxy5 = new Proxy(Proxy.Type.SOCKS, address1);
Proxy proxy6 = new Proxy(Proxy.Type.SOCKS, address2);
assertTrue(proxy5.hashCode() != proxy6.hashCode());
}
use of java.net.Proxy in project robovm by robovm.
the class HttpURLConnectionTest method testUsingProxy.
/**
* Test checks if the proxy specified in openConnection
* method will be used for connection to the server
*/
public void testUsingProxy() throws Exception {
// Regression for HARMONY-570
MockServer server = new MockServer("server");
MockServer proxy = new MockServer("proxy");
URL url = new URL("http://localhost:" + server.port());
HttpURLConnection connection = (HttpURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", proxy.port())));
connection.setConnectTimeout(2000);
connection.setReadTimeout(2000);
server.start();
synchronized (bound) {
if (!server.started)
bound.wait(5000);
}
proxy.start();
synchronized (bound) {
if (!proxy.started)
bound.wait(5000);
}
connection.connect();
// wait while server and proxy run
server.join();
proxy.join();
assertTrue("Connection does not use proxy", connection.usingProxy());
assertTrue("Proxy server was not used", proxy.accepted);
HttpURLConnection huc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
assertFalse(huc.usingProxy());
}
use of java.net.Proxy in project robovm by robovm.
the class HttpsURLConnectionTest method testProxyAuthConnectionFailed.
/**
* Tests HTTPS connection process made through the proxy server.
* Proxy server needs authentication but client fails to authenticate
* (Authenticator was not set up in the system).
*/
public void testProxyAuthConnectionFailed() throws Throwable {
// setting up the properties pointing to the key/trust stores
setUpStoreProperties();
// create the SSLServerSocket which will be used by server side
ServerSocket ss = new ServerSocket(0);
// create the HostnameVerifier to check that Hostname verification
// is done
TestHostnameVerifier hnv = new TestHostnameVerifier();
HttpsURLConnection.setDefaultHostnameVerifier(hnv);
// create HttpsURLConnection to be tested
URL url = new URL("https://requested.host:55555/requested.data");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", ss.getLocalPort())));
connection.setSSLSocketFactory(getContext().getSocketFactory());
// perform the interaction between the peers and check the results
try {
doInteraction(connection, ss, AUTHENTICATION_REQUIRED_CODE, true);
} catch (IOException e) {
// SSL Tunnelling failed
if (DO_LOG) {
System.out.println("Got expected IOException: " + e.getMessage());
}
}
}
use of java.net.Proxy in project robovm by robovm.
the class HttpsURLConnectionTest method testProxyConnection_Not_Found_Response.
/**
* Tests the behaviour of HTTPS connection in case of unavailability
* of requested resource.
*/
public void testProxyConnection_Not_Found_Response() throws Throwable {
// setting up the properties pointing to the key/trust stores
setUpStoreProperties();
// create the SSLServerSocket which will be used by server side
ServerSocket ss = new ServerSocket(0);
// create the HostnameVerifier to check that Hostname verification
// is done
TestHostnameVerifier hnv = new TestHostnameVerifier();
HttpsURLConnection.setDefaultHostnameVerifier(hnv);
// create HttpsURLConnection to be tested
URL url = new URL("https://localhost:" + ss.getLocalPort());
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", ss.getLocalPort())));
connection.setSSLSocketFactory(getContext().getSocketFactory());
try {
// NOT FOUND
doInteraction(connection, ss, NOT_FOUND_CODE);
fail("Expected exception was not thrown.");
} catch (FileNotFoundException e) {
if (DO_LOG) {
System.out.println("Expected exception was thrown: " + e.getMessage());
}
}
}
use of java.net.Proxy in project okhttp by square.
the class JavaNetAuthenticator method authenticate.
@Override
public Request authenticate(Route route, Response response) throws IOException {
List<Challenge> challenges = response.challenges();
Request request = response.request();
HttpUrl url = request.url();
boolean proxyAuthorization = response.code() == 407;
Proxy proxy = route.proxy();
for (int i = 0, size = challenges.size(); i < size; i++) {
Challenge challenge = challenges.get(i);
if (!"Basic".equalsIgnoreCase(challenge.scheme()))
continue;
PasswordAuthentication auth;
if (proxyAuthorization) {
InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
auth = java.net.Authenticator.requestPasswordAuthentication(proxyAddress.getHostName(), getConnectToInetAddress(proxy, url), proxyAddress.getPort(), url.scheme(), challenge.realm(), challenge.scheme(), url.url(), RequestorType.PROXY);
} else {
auth = java.net.Authenticator.requestPasswordAuthentication(url.host(), getConnectToInetAddress(proxy, url), url.port(), url.scheme(), challenge.realm(), challenge.scheme(), url.url(), RequestorType.SERVER);
}
if (auth != null) {
String credential = Credentials.basic(auth.getUserName(), new String(auth.getPassword()));
return request.newBuilder().header(proxyAuthorization ? "Proxy-Authorization" : "Authorization", credential).build();
}
}
// No challenges were satisfied!
return null;
}
Aggregations