test/jdk/java/net/HttpURLConnection/NoProxyTest.java
author aeubanks
Wed, 27 Mar 2019 09:06:43 -0700
changeset 54314 46cf212cdcca
parent 47216 71c04702a3d5
permissions -rw-r--r--
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder Reviewed-by: dfuchs, chegar Contributed-by: aeubanks@google.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39128
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
     1
/*
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
     4
 *
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
     8
 *
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    13
 * accompanied this code).
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    14
 *
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    18
 *
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    21
 * questions.
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    22
 */
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    23
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    24
 /*
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    25
 * @test
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    26
 * @bug 8144008
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    27
 * @library /test/lib
39128
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    28
 * @summary Setting NO_PROXY on HTTP URL connections does not stop proxying
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    29
 * @run main/othervm NoProxyTest
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    30
 */
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    31
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    32
import java.io.IOException;
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    33
import java.net.InetAddress;
39128
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    34
import java.net.MalformedURLException;
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    35
import java.net.Proxy;
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    36
import java.net.ProxySelector;
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    37
import java.net.SocketAddress;
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    38
import java.net.URI;
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    39
import java.net.URL;
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    40
import java.net.URLConnection;
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    41
import java.util.List;
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    42
import jdk.test.lib.net.URIBuilder;
39128
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    43
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    44
public class NoProxyTest {
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    45
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    46
    static class NoProxyTestSelector extends ProxySelector {
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    47
        @Override
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    48
        public List<Proxy> select(URI uri) {
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    49
            throw new RuntimeException("Should not reach here as proxy==Proxy.NO_PROXY");
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    50
        }
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    51
        @Override
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    52
        public void connectFailed(URI u, SocketAddress s, IOException e) { }
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    53
    }
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    54
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    55
    public static void main(String args[]) throws MalformedURLException {
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    56
        ProxySelector.setDefault(new NoProxyTestSelector());
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    57
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    58
        URL url = URIBuilder.newBuilder()
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    59
            .scheme("http")
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    60
            .loopback()
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    61
            .path("/")
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    62
            .toURLUnchecked();
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    63
        System.out.println("URL: " + url);
39128
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    64
        URLConnection connection;
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    65
        try {
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    66
            connection = url.openConnection(Proxy.NO_PROXY);
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    67
            connection.connect();
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    68
        } catch (IOException ignore) {
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    69
            //ignore
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    70
        }
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    71
    }
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents:
diff changeset
    72
}