test/jdk/java/net/HttpURLConnection/HttpURLConWithProxy.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 40737 jdk/test/java/net/HttpURLConnection/HttpURLConWithProxy.java@9b5eee5d7a26
child 55649 ad8e3b295615
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40737
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
     1
/*
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
     4
 *
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
     8
 *
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    13
 * accompanied this code).
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    14
 *
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    18
 *
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    21
 * questions.
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    22
 */
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    23
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    24
 /*
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    25
 * @test
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    26
 * @bug 8161016
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    27
 * @summary When proxy is set HttpURLConnection should not use DIRECT connection.
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    28
 * @run main/othervm HttpURLConWithProxy
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    29
 */
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    30
import java.io.IOException;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    31
import java.net.InetSocketAddress;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    32
import java.net.Proxy;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    33
import java.net.ProxySelector;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    34
import java.net.ServerSocket;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    35
import java.net.SocketAddress;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    36
import java.net.URI;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    37
import java.net.URL;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    38
import java.net.URLConnection;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    39
import java.util.ArrayList;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    40
import java.util.List;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    41
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    42
public class HttpURLConWithProxy {
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    43
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    44
    public static void main(String... arg) {
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    45
        // Remove the default nonProxyHosts to use localhost for testing
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    46
        System.setProperty("http.nonProxyHosts", "");
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    47
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    48
        System.setProperty("http.proxyHost", "1.1.1.1");
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    49
        System.setProperty("http.proxyPort", "1111");
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    50
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    51
        ServerSocket ss;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    52
        URL url;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    53
        URLConnection con;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    54
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    55
        // Test1: using Proxy set by System Property:
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    56
        try {
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    57
            ss = new ServerSocket(0);
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    58
            url = new URL("http://localhost:" + ss.getLocalPort());
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    59
            con = url.openConnection();
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    60
            con.setConnectTimeout(10 * 1000);
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    61
            con.connect();
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    62
            throw new RuntimeException("Shouldn't use DIRECT connection "
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    63
                    + "when proxy is invalid/down");
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    64
        } catch (IOException ie) {
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    65
            System.out.println("Test1 Passed with: " + ie.getMessage());
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    66
        }
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    67
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    68
        // Test2: using custom ProxySelector implementation
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    69
        MyProxySelector myProxySel = new MyProxySelector();
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    70
        ProxySelector.setDefault(myProxySel);
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    71
        try {
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    72
            ss = new ServerSocket(0);
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    73
            url = new URL("http://localhost:" + ss.getLocalPort());
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    74
            con = url.openConnection();
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    75
            con.setConnectTimeout(10 * 1000);
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    76
            con.connect();
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    77
            throw new RuntimeException("Shouldn't use DIRECT connection "
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    78
                    + "when proxy is invalid/down");
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    79
        } catch (IOException ie) {
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    80
            System.out.println("Test2 Passed with: " + ie.getMessage());
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    81
        }
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    82
    }
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    83
}
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    84
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    85
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    86
class MyProxySelector extends ProxySelector {
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    87
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    88
    List<Proxy> proxies = new ArrayList<>();
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    89
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    90
    MyProxySelector() {
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    91
        Proxy p1 = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("2.2.2.2", 2222));
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    92
        Proxy p2 = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("3.3.3.3", 3333));
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    93
        proxies.add(p1);
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    94
        proxies.add(p2);
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    95
    }
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    96
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    97
    @Override
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    98
    public List<Proxy> select(URI uri) {
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    99
        return proxies;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   100
    }
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   101
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   102
    @Override
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   103
    public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   104
        // System.out.println("MyProxySelector.connectFailed(): "+sa);
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   105
    }
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   106
}