test/jdk/java/net/HttpURLConnection/HttpURLConWithProxy.java
author jboes
Fri, 08 Nov 2019 11:15:16 +0000
changeset 59029 3786a0962570
parent 55649 ad8e3b295615
permissions -rw-r--r--
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException Summary: Change implementation to use iterator instead of plain LinkedList Reviewed-by: dfuchs, vtewari
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40737
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
     1
/*
55649
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 47216
diff changeset
     2
 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
40737
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
55649
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 47216
diff changeset
    27
 * @library /test/lib
40737
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    28
 * @summary When proxy is set HttpURLConnection should not use DIRECT connection.
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    29
 * @run main/othervm HttpURLConWithProxy
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    30
 */
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    31
import java.io.IOException;
55649
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 47216
diff changeset
    32
import java.net.InetAddress;
40737
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    33
import java.net.InetSocketAddress;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    34
import java.net.Proxy;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    35
import java.net.ProxySelector;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    36
import java.net.ServerSocket;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    37
import java.net.SocketAddress;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    38
import java.net.URI;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    39
import java.net.URL;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    40
import java.net.URLConnection;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    41
import java.util.ArrayList;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    42
import java.util.List;
55649
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 47216
diff changeset
    43
import jdk.test.lib.net.URIBuilder;
40737
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    44
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    45
public class HttpURLConWithProxy {
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    46
55649
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 47216
diff changeset
    47
    public static void main(String... arg) throws Exception {
40737
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    48
        // Remove the default nonProxyHosts to use localhost for testing
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    49
        System.setProperty("http.nonProxyHosts", "");
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
        System.setProperty("http.proxyHost", "1.1.1.1");
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    52
        System.setProperty("http.proxyPort", "1111");
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    53
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    54
        ServerSocket ss;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    55
        URL url;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    56
        URLConnection con;
55649
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 47216
diff changeset
    57
        InetAddress loopback = InetAddress.getLoopbackAddress();
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 47216
diff changeset
    58
        InetSocketAddress address = new InetSocketAddress(loopback, 0);
40737
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    59
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    60
        // Test1: using Proxy set by System Property:
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    61
        try {
55649
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 47216
diff changeset
    62
            ss = new ServerSocket();
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 47216
diff changeset
    63
            ss.bind(address);
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 47216
diff changeset
    64
            url = URIBuilder.newBuilder()
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 47216
diff changeset
    65
                .scheme("http")
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 47216
diff changeset
    66
                .loopback()
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 47216
diff changeset
    67
                .port(ss.getLocalPort())
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 47216
diff changeset
    68
                .toURL();
40737
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    69
            con = url.openConnection();
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    70
            con.setConnectTimeout(10 * 1000);
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    71
            con.connect();
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    72
            throw new RuntimeException("Shouldn't use DIRECT connection "
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    73
                    + "when proxy is invalid/down");
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    74
        } catch (IOException ie) {
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    75
            System.out.println("Test1 Passed with: " + ie.getMessage());
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    76
        }
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    77
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    78
        // Test2: using custom ProxySelector implementation
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    79
        MyProxySelector myProxySel = new MyProxySelector();
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    80
        ProxySelector.setDefault(myProxySel);
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    81
        try {
55649
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 47216
diff changeset
    82
            ss = new ServerSocket();
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 47216
diff changeset
    83
            ss.bind(address);
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 47216
diff changeset
    84
            url = URIBuilder.newBuilder()
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 47216
diff changeset
    85
                .scheme("http")
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 47216
diff changeset
    86
                .loopback()
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 47216
diff changeset
    87
                .port(ss.getLocalPort())
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 47216
diff changeset
    88
                .toURL();
40737
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    89
            con = url.openConnection();
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    90
            con.setConnectTimeout(10 * 1000);
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    91
            con.connect();
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    92
            throw new RuntimeException("Shouldn't use DIRECT connection "
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    93
                    + "when proxy is invalid/down");
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    94
        } catch (IOException ie) {
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    95
            System.out.println("Test2 Passed with: " + ie.getMessage());
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
    }
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    98
}
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
    99
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
class MyProxySelector extends ProxySelector {
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   102
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   103
    List<Proxy> proxies = new ArrayList<>();
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   104
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   105
    MyProxySelector() {
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   106
        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
   107
        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
   108
        proxies.add(p1);
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   109
        proxies.add(p2);
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   110
    }
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   111
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   112
    @Override
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   113
    public List<Proxy> select(URI uri) {
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   114
        return proxies;
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   115
    }
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   116
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   117
    @Override
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   118
    public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   119
        // System.out.println("MyProxySelector.connectFailed(): "+sa);
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   120
    }
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents:
diff changeset
   121
}