test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPTest.java
author chegar
Fri, 02 Nov 2018 16:11:29 +0000
changeset 52387 8c0b1894d524
parent 52121 934969c63223
child 55522 b2cbe2f674f0
child 58678 9cf78a70fa4f
permissions -rw-r--r--
8213296: Fix legal headers in test/jdk/java/net Reviewed-by: alanb, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
     1
/*
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 47216
diff changeset
     2
 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
     4
 *
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
52387
8c0b1894d524 8213296: Fix legal headers in test/jdk/java/net
chegar
parents: 52121
diff changeset
     7
 * published by the Free Software Foundation.
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
     8
 *
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    14
 *
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    18
 *
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    21
 * questions.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    22
 */
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    23
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    24
import java.io.IOException;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    25
import java.io.UncheckedIOException;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    26
import java.net.Authenticator;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    27
import java.net.HttpURLConnection;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    28
import java.net.InetSocketAddress;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    29
import java.net.MalformedURLException;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    30
import java.net.PasswordAuthentication;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    31
import java.net.Proxy;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    32
import java.net.URL;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    33
import java.util.Locale;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    34
import java.util.concurrent.atomic.AtomicInteger;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    35
import java.util.logging.Level;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    36
import java.util.logging.Logger;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    37
import java.util.stream.Stream;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    38
import javax.net.ssl.HostnameVerifier;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    39
import javax.net.ssl.HttpsURLConnection;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    40
import javax.net.ssl.SSLContext;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    41
import javax.net.ssl.SSLSession;
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 47216
diff changeset
    42
import jdk.test.lib.net.SimpleSSLContext;
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    43
43497
1a2262d4395c 8173475: java/net/HttpURLConnection/SetAuthenticator tests have undeclared dependency on java.logging module
skovalev
parents: 42351
diff changeset
    44
/*
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    45
 * @test
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    46
 * @bug 8169415
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 47216
diff changeset
    47
 * @library /test/lib
43497
1a2262d4395c 8173475: java/net/HttpURLConnection/SetAuthenticator tests have undeclared dependency on java.logging module
skovalev
parents: 42351
diff changeset
    48
 * @modules java.logging
1a2262d4395c 8173475: java/net/HttpURLConnection/SetAuthenticator tests have undeclared dependency on java.logging module
skovalev
parents: 42351
diff changeset
    49
 *          java.base/sun.net.www
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    50
 *          jdk.httpserver/sun.net.httpserver
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 47216
diff changeset
    51
 * @build jdk.test.lib.net.SimpleSSLContext HTTPTest HTTPTestServer HTTPTestClient
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    52
 * @summary A simple HTTP test that starts an echo server supporting Digest
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    53
 *          authentication, then starts a regular HTTP client to invoke it.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    54
 *          The client first does a GET request on "/", then follows on
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    55
 *          with a POST request that sends "Hello World!" to the server.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    56
 *          The client expects to receive "Hello World!" in return.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    57
 *          The test supports several execution modes:
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    58
 *            SERVER: The server performs Digest Server authentication;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    59
 *            PROXY:  The server pretends to be a proxy and performs
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    60
 *                    Digest Proxy authentication;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    61
 *            SERVER307: The server redirects the client (307) to another
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    62
 *                    server that perform Digest authentication;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    63
 *            PROXY305: The server attempts to redirect
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    64
 *                    the client to a proxy using 305 code;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    65
 * @run main/othervm HTTPTest SERVER
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    66
 * @run main/othervm HTTPTest PROXY
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    67
 * @run main/othervm HTTPTest SERVER307
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    68
 * @run main/othervm HTTPTest PROXY305
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    69
 *
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    70
 * @author danielfuchs
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    71
 */
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    72
public class HTTPTest {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    73
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    74
    public static final boolean DEBUG =
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    75
         Boolean.parseBoolean(System.getProperty("test.debug", "false"));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    76
    public static enum HttpAuthType { SERVER, PROXY, SERVER307, PROXY305 };
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    77
    public static enum HttpProtocolType { HTTP, HTTPS };
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    78
    public static enum HttpSchemeType { NONE, BASICSERVER, BASIC, DIGEST };
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    79
    public static final HttpAuthType DEFAULT_HTTP_AUTH_TYPE = HttpAuthType.SERVER;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    80
    public static final HttpProtocolType DEFAULT_PROTOCOL_TYPE = HttpProtocolType.HTTP;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    81
    public static final HttpSchemeType DEFAULT_SCHEME_TYPE = HttpSchemeType.DIGEST;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    82
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    83
    public static class HttpTestAuthenticator extends Authenticator {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    84
        private final String realm;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    85
        private final String username;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    86
        // Used to prevent incrementation of 'count' when calling the
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    87
        // authenticator from the server side.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    88
        private final ThreadLocal<Boolean> skipCount = new ThreadLocal<>();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    89
        // count will be incremented every time getPasswordAuthentication()
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    90
        // is called from the client side.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    91
        final AtomicInteger count = new AtomicInteger();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    92
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    93
        public HttpTestAuthenticator(String realm, String username) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    94
            this.realm = realm;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    95
            this.username = username;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    96
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    97
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    98
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    99
        protected PasswordAuthentication getPasswordAuthentication() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   100
            if (skipCount.get() == null || skipCount.get().booleanValue() == false) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   101
                System.out.println("Authenticator called: " + count.incrementAndGet());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   102
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   103
            return new PasswordAuthentication(getUserName(),
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   104
                    new char[] {'b','a','r'});
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   105
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   106
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   107
        // Called by the server side to get the password of the user
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   108
        // being authentified.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   109
        public final char[] getPassword(String user) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   110
            if (user.equals(username)) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   111
                skipCount.set(Boolean.TRUE);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   112
                try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   113
                    return getPasswordAuthentication().getPassword();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   114
                } finally {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   115
                    skipCount.set(Boolean.FALSE);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   116
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   117
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   118
            throw new SecurityException("User unknown: " + user);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   119
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   120
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   121
        public final String getUserName() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   122
            return username;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   123
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   124
        public final String getRealm() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   125
            return realm;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   126
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   127
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   128
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   129
    public static final HttpTestAuthenticator AUTHENTICATOR;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   130
    static {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   131
        AUTHENTICATOR = new HttpTestAuthenticator("dublin", "foox");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   132
        Authenticator.setDefault(AUTHENTICATOR);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   133
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   134
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   135
    static {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   136
        try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   137
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   138
                public boolean verify(String hostname, SSLSession session) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   139
                    return true;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   140
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   141
            });
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   142
            SSLContext.setDefault(new SimpleSSLContext().get());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   143
        } catch (IOException ex) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   144
            throw new ExceptionInInitializerError(ex);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   145
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   146
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   147
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   148
    static final Logger logger = Logger.getLogger ("com.sun.net.httpserver");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   149
    static {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   150
        if (DEBUG) logger.setLevel(Level.ALL);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   151
        Stream.of(Logger.getLogger("").getHandlers())
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   152
              .forEach(h -> h.setLevel(Level.ALL));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   153
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   154
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   155
    static final int EXPECTED_AUTH_CALLS_PER_TEST = 1;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   156
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   157
    public static void main(String[] args) throws Exception {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   158
        // new HTTPTest().execute(HttpAuthType.SERVER.name());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   159
        new HTTPTest().execute(args);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   160
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   161
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   162
    public void execute(String... args) throws Exception {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   163
        Stream<HttpAuthType> modes;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   164
        if (args == null || args.length == 0) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   165
            modes = Stream.of(HttpAuthType.values());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   166
        } else {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   167
            modes = Stream.of(args).map(HttpAuthType::valueOf);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   168
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   169
        modes.forEach(this::test);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   170
        System.out.println("Test PASSED - Authenticator called: "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   171
                 + expected(AUTHENTICATOR.count.get()));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   172
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   173
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   174
    public void test(HttpAuthType mode) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   175
        for (HttpProtocolType type: HttpProtocolType.values()) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   176
            test(type, mode);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   177
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   178
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   179
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   180
    public HttpSchemeType getHttpSchemeType() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   181
        return DEFAULT_SCHEME_TYPE;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   182
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   183
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   184
    public void test(HttpProtocolType protocol, HttpAuthType mode) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   185
        if (mode == HttpAuthType.PROXY305 && protocol == HttpProtocolType.HTTPS ) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   186
            // silently skip unsupported test combination
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   187
            return;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   188
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   189
        System.out.println("\n**** Testing " + protocol + " "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   190
                           + mode + " mode ****\n");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   191
        int authCount = AUTHENTICATOR.count.get();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   192
        int expectedIncrement = 0;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   193
        try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   194
            // Creates an HTTP server that echoes back whatever is in the
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   195
            // request body.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   196
            HTTPTestServer server =
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   197
                    HTTPTestServer.create(protocol,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   198
                                          mode,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   199
                                          AUTHENTICATOR,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   200
                                          getHttpSchemeType());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   201
            try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   202
                expectedIncrement += run(server, protocol, mode);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   203
            } finally {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   204
                server.stop();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   205
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   206
        }  catch (IOException ex) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   207
            ex.printStackTrace(System.err);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   208
            throw new UncheckedIOException(ex);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   209
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   210
        int count = AUTHENTICATOR.count.get();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   211
        if (count != authCount + expectedIncrement) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   212
            throw new AssertionError("Authenticator called " + count(count)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   213
                        + " expected it to be called "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   214
                        + expected(authCount + expectedIncrement));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   215
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   216
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   217
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   218
    /**
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   219
     * Runs the test with the given parameters.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   220
     * @param server    The server
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   221
     * @param protocol  The protocol (HTTP/HTTPS)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   222
     * @param mode      The mode (PROXY, SERVER, SERVER307...)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   223
     * @return The number of times the default authenticator should have been
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   224
     *         called.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   225
     * @throws IOException in case of connection or protocol issues
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   226
     */
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   227
    public int run(HTTPTestServer server,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   228
                   HttpProtocolType protocol,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   229
                   HttpAuthType mode)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   230
            throws IOException
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   231
    {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   232
        // Connect to the server with a GET request, then with a
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   233
        // POST that contains "Hello World!"
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   234
        HTTPTestClient.connect(protocol, server, mode, null);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   235
        // return the number of times the default authenticator is supposed
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   236
        // to have been called.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   237
        return EXPECTED_AUTH_CALLS_PER_TEST;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   238
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   239
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   240
    public static String count(int count) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   241
        switch(count) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   242
            case 0: return "not even once";
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   243
            case 1: return "once";
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   244
            case 2: return "twice";
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   245
            default: return String.valueOf(count) + " times";
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   246
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   247
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   248
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   249
    public static String expected(int count) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   250
        switch(count) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   251
            default: return count(count);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   252
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   253
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   254
    public static String protocol(HttpProtocolType type) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   255
        return type.name().toLowerCase(Locale.US);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   256
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   257
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   258
    public static URL url(HttpProtocolType protocol, InetSocketAddress address,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   259
                          String path) throws MalformedURLException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   260
        return new URL(protocol(protocol),
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   261
                       address.getHostString(),
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   262
                       address.getPort(), path);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   263
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   264
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   265
    public static Proxy proxy(HTTPTestServer server, HttpAuthType authType) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   266
        return (authType == HttpAuthType.PROXY)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   267
               ? new Proxy(Proxy.Type.HTTP, server.getAddress())
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   268
               : null;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   269
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   270
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   271
    public static HttpURLConnection openConnection(URL url,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   272
                                                   HttpAuthType authType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   273
                                                   Proxy proxy)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   274
                                    throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   275
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   276
        HttpURLConnection conn = (HttpURLConnection)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   277
                (authType == HttpAuthType.PROXY
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   278
                    ? url.openConnection(proxy)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   279
                    : url.openConnection());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   280
        return conn;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   281
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   282
}