@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2019 , Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2020 , Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
/*
* @test
* @bug 8161016
* @bug 8161016 8183369
* @library /test/lib
* @summary When proxy is set HttpURLConnection should not use DIRECT connection.
* @run main/othervm HttpURLConWithProxy
@@ -37,10 +37,14 @@
import java .net .SocketAddress ;
import java .net .URI ;
import java .net .URL ;
import java .net .URLConnection ;
import java .net .HttpURLConnection ;
import java .util .ArrayList ;
import java .util .List ;
import jdk .test .lib .net .URIBuilder ;
import java .util .logging .Handler ;
import java .util .logging .Level ;
import java .util .logging .Logger ;
import java .util .logging .LogRecord ;
public class HttpURLConWithProxy {
@@ -50,10 +54,15 @@ public static void main(String... arg) throws Exception {
System .setProperty ("http.proxyHost" , "1.1.1.1" );
System .setProperty ("http.proxyPort" , "1111" );
String HTTPLOG = "sun.net.www.protocol.http.HttpURLConnection" ;
Logger .getLogger (HTTPLOG ).setLevel (Level .ALL );
Handler h = new ProxyHandler ();
h .setLevel (Level .ALL );
Logger .getLogger (HTTPLOG ).addHandler (h );
ServerSocket ss ;
URL url ;
URLConnection con ;
HttpURLConnection con ;
InetAddress loopback = InetAddress .getLoopbackAddress ();
InetSocketAddress address = new InetSocketAddress (loopback , 0 );
@@ -66,16 +75,24 @@ public static void main(String... arg) throws Exception {
.loopback ()
.port (ss .getLocalPort ())
.toURL ();
con = url .openConnection ();
con = ( HttpURLConnection ) url .openConnection ();
con .setConnectTimeout (10 * 1000 );
con .connect ();
throw new RuntimeException ("Shouldn't use DIRECT connection "
+ "when proxy is invalid/down" );
if (con .usingProxy ()){
System .out .println ("Test1 Passed with: Connection succeeded with proxy" );
} else {
throw new RuntimeException ("Shouldn't use DIRECT connection "
+ "when proxy is invalid/down" );
}
} catch (IOException ie ) {
if (!ProxyHandler .proxyRetried ) {
throw new RuntimeException ("Connection not retried with proxy" );
}
System .out .println ("Test1 Passed with: " + ie .getMessage ());
}
// Test2: using custom ProxySelector implementation
ProxyHandler .proxyRetried = false ;
MyProxySelector myProxySel = new MyProxySelector ();
ProxySelector .setDefault (myProxySel );
try {
@@ -86,12 +103,19 @@ public static void main(String... arg) throws Exception {
.loopback ()
.port (ss .getLocalPort ())
.toURL ();
con = url .openConnection ();
con = ( HttpURLConnection ) url .openConnection ();
con .setConnectTimeout (10 * 1000 );
con .connect ();
throw new RuntimeException ("Shouldn't use DIRECT connection "
+ "when proxy is invalid/down" );
if (con .usingProxy ()){
System .out .println ("Test2 Passed with: Connection succeeded with proxy" );
} else {
throw new RuntimeException ("Shouldn't use DIRECT connection "
+ "when proxy is invalid/down" );
}
} catch (IOException ie ) {
if (!ProxyHandler .proxyRetried ) {
throw new RuntimeException ("Connection not retried with proxy" );
}
System .out .println ("Test2 Passed with: " + ie .getMessage ());
}
}
@@ -119,3 +143,22 @@ public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
// System.out.println("MyProxySelector.connectFailed(): "+sa);
}
}
class ProxyHandler extends Handler {
public static boolean proxyRetried = false ;
@ Override
public void publish (LogRecord record ) {
if (record .getMessage ().contains ("Retrying with proxy" )) {
proxyRetried = true ;
}
}
@ Override
public void flush () {
}
@ Override
public void close () throws SecurityException {
}
}