test/jdk/java/net/ProxySelector/ProxyTest.java
author aeubanks
Tue, 16 Apr 2019 13:06:23 -0700
changeset 54770 62b6e7587b1f
parent 47216 71c04702a3d5
child 54822 f542a3a135bd
permissions -rw-r--r--
8220673: Add test library support for determining platform IP support Reviewed-by: dfuchs, chegar Contributed-by: aeubanks@google.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 13788
diff changeset
     2
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4696512
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary HTTP client: Improve proxy server configuration and selection
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 14342
diff changeset
    28
 * @modules java.base/sun.net.www
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @library ../../../sun/net/www/httptest/
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 5506
diff changeset
    30
 * @build ClosedChannelList TestHttpServer HttpTransaction HttpCallback
4347
ab0a9f495844 6907177: Update jdk tests to remove unncessary -source and -target options
darcy
parents: 2
diff changeset
    31
 * @compile ProxyTest.java
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main/othervm -Dhttp.proxyHost=inexistant -Dhttp.proxyPort=8080 ProxyTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class ProxyTest implements HttpCallback {
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 5506
diff changeset
    40
    static TestHttpServer server;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public ProxyTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public void request (HttpTransaction req) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        req.setResponseEntityBody ("Hello .");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            req.sendResponse (200, "Ok");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static public class MyProxySelector extends ProxySelector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        private ProxySelector def = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        private ArrayList<Proxy> noProxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        public MyProxySelector() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            noProxy = new ArrayList<Proxy>(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            noProxy.add(Proxy.NO_PROXY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        public java.util.List<Proxy> select(URI uri) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            return noProxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        ProxySelector defSelector = ProxySelector.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (defSelector == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throw new RuntimeException("Default ProxySelector is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        ProxySelector.setDefault(new MyProxySelector());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        try {
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 5506
diff changeset
    78
            server = new TestHttpServer (new ProxyTest(), 1, 10, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            URL url = new URL("http://localhost:"+server.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            System.out.println ("client opening connection to: " + url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            InputStream is = urlc.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (server != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
}