author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58365 | 73950479184b |
permissions | -rw-r--r-- |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1 |
/* |
55522
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
52387
diff
changeset
|
2 |
* Copyright (c) 2016, 2019, 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 | 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; |
58365
73950479184b
8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents:
57793
diff
changeset
|
43 |
import static java.net.Proxy.NO_PROXY; |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
44 |
|
43497
1a2262d4395c
8173475: java/net/HttpURLConnection/SetAuthenticator tests have undeclared dependency on java.logging module
skovalev
parents:
42351
diff
changeset
|
45 |
/* |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
46 |
* @test |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
47 |
* @bug 8169415 |
52121
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
47216
diff
changeset
|
48 |
* @library /test/lib |
43497
1a2262d4395c
8173475: java/net/HttpURLConnection/SetAuthenticator tests have undeclared dependency on java.logging module
skovalev
parents:
42351
diff
changeset
|
49 |
* @modules java.logging |
1a2262d4395c
8173475: java/net/HttpURLConnection/SetAuthenticator tests have undeclared dependency on java.logging module
skovalev
parents:
42351
diff
changeset
|
50 |
* java.base/sun.net.www |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
51 |
* jdk.httpserver/sun.net.httpserver |
52121
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
47216
diff
changeset
|
52 |
* @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
|
53 |
* @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
|
54 |
* 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
|
55 |
* 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
|
56 |
* 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
|
57 |
* 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
|
58 |
* The test supports several execution modes: |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
59 |
* SERVER: The server performs Digest Server authentication; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
60 |
* 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
|
61 |
* Digest Proxy authentication; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
62 |
* 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
|
63 |
* server that perform Digest authentication; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
64 |
* PROXY305: The server attempts to redirect |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
65 |
* the client to a proxy using 305 code; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
66 |
* @run main/othervm HTTPTest SERVER |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
67 |
* @run main/othervm HTTPTest PROXY |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
68 |
* @run main/othervm HTTPTest SERVER307 |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
69 |
* @run main/othervm HTTPTest PROXY305 |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
70 |
* |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
71 |
* @author danielfuchs |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
72 |
*/ |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
73 |
public class HTTPTest { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
74 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
75 |
public static final boolean DEBUG = |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
76 |
Boolean.parseBoolean(System.getProperty("test.debug", "false")); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
77 |
public static enum HttpAuthType { SERVER, PROXY, SERVER307, PROXY305 }; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
78 |
public static enum HttpProtocolType { HTTP, HTTPS }; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
79 |
public static enum HttpSchemeType { NONE, BASICSERVER, BASIC, DIGEST }; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
80 |
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
|
81 |
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
|
82 |
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
|
83 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
84 |
public static class HttpTestAuthenticator extends Authenticator { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
85 |
private final String realm; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
86 |
private final String username; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
87 |
// 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
|
88 |
// authenticator from the server side. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
89 |
private final ThreadLocal<Boolean> skipCount = new ThreadLocal<>(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
90 |
// count will be incremented every time getPasswordAuthentication() |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
91 |
// is called from the client side. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
92 |
final AtomicInteger count = new AtomicInteger(); |
57793
d372747e8f08
8191169: java/net/Authenticator/B4769350.java failed intermittently
dfuchs
parents:
55522
diff
changeset
|
93 |
private final String name; |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
94 |
|
57793
d372747e8f08
8191169: java/net/Authenticator/B4769350.java failed intermittently
dfuchs
parents:
55522
diff
changeset
|
95 |
public HttpTestAuthenticator(String name, String realm, String username) { |
d372747e8f08
8191169: java/net/Authenticator/B4769350.java failed intermittently
dfuchs
parents:
55522
diff
changeset
|
96 |
this.name = name; |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
97 |
this.realm = realm; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
98 |
this.username = username; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
99 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
100 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
101 |
@Override |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
102 |
protected PasswordAuthentication getPasswordAuthentication() { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
103 |
if (skipCount.get() == null || skipCount.get().booleanValue() == false) { |
57793
d372747e8f08
8191169: java/net/Authenticator/B4769350.java failed intermittently
dfuchs
parents:
55522
diff
changeset
|
104 |
System.out.println("Authenticator " + name + " called: " + count.incrementAndGet()); |
42351
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 |
return new PasswordAuthentication(getUserName(), |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
107 |
new char[] {'b','a','r'}); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
108 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
109 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
110 |
// 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
|
111 |
// being authentified. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
112 |
public final char[] getPassword(String user) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
113 |
if (user.equals(username)) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
114 |
skipCount.set(Boolean.TRUE); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
115 |
try { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
116 |
return getPasswordAuthentication().getPassword(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
117 |
} finally { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
118 |
skipCount.set(Boolean.FALSE); |
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 |
throw new SecurityException("User unknown: " + user); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
122 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
123 |
|
57793
d372747e8f08
8191169: java/net/Authenticator/B4769350.java failed intermittently
dfuchs
parents:
55522
diff
changeset
|
124 |
@Override |
d372747e8f08
8191169: java/net/Authenticator/B4769350.java failed intermittently
dfuchs
parents:
55522
diff
changeset
|
125 |
public String toString() { |
d372747e8f08
8191169: java/net/Authenticator/B4769350.java failed intermittently
dfuchs
parents:
55522
diff
changeset
|
126 |
return super.toString() + "[name=\"" + name + "\"]"; |
d372747e8f08
8191169: java/net/Authenticator/B4769350.java failed intermittently
dfuchs
parents:
55522
diff
changeset
|
127 |
} |
d372747e8f08
8191169: java/net/Authenticator/B4769350.java failed intermittently
dfuchs
parents:
55522
diff
changeset
|
128 |
|
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
129 |
public final String getUserName() { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
130 |
return username; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
131 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
132 |
public final String getRealm() { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
133 |
return realm; |
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 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
136 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
137 |
public static final HttpTestAuthenticator AUTHENTICATOR; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
138 |
static { |
57793
d372747e8f08
8191169: java/net/Authenticator/B4769350.java failed intermittently
dfuchs
parents:
55522
diff
changeset
|
139 |
AUTHENTICATOR = new HttpTestAuthenticator("AUTHENTICATOR","dublin", "foox"); |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
140 |
Authenticator.setDefault(AUTHENTICATOR); |
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 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
143 |
static { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
144 |
try { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
145 |
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
146 |
public boolean verify(String hostname, SSLSession session) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
147 |
return true; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
148 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
149 |
}); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
150 |
SSLContext.setDefault(new SimpleSSLContext().get()); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
151 |
} catch (IOException ex) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
152 |
throw new ExceptionInInitializerError(ex); |
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 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
156 |
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
|
157 |
static { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
158 |
if (DEBUG) logger.setLevel(Level.ALL); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
159 |
Stream.of(Logger.getLogger("").getHandlers()) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
160 |
.forEach(h -> h.setLevel(Level.ALL)); |
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 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
163 |
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
|
164 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
165 |
public static void main(String[] args) throws Exception { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
166 |
// new HTTPTest().execute(HttpAuthType.SERVER.name()); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
167 |
new HTTPTest().execute(args); |
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 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
170 |
public void execute(String... args) throws Exception { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
171 |
Stream<HttpAuthType> modes; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
172 |
if (args == null || args.length == 0) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
173 |
modes = Stream.of(HttpAuthType.values()); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
174 |
} else { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
175 |
modes = Stream.of(args).map(HttpAuthType::valueOf); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
176 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
177 |
modes.forEach(this::test); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
178 |
System.out.println("Test PASSED - Authenticator called: " |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
179 |
+ expected(AUTHENTICATOR.count.get())); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
180 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
181 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
182 |
public void test(HttpAuthType mode) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
183 |
for (HttpProtocolType type: HttpProtocolType.values()) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
184 |
test(type, mode); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
185 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
186 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
187 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
188 |
public HttpSchemeType getHttpSchemeType() { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
189 |
return DEFAULT_SCHEME_TYPE; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
190 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
191 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
192 |
public void test(HttpProtocolType protocol, HttpAuthType mode) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
193 |
if (mode == HttpAuthType.PROXY305 && protocol == HttpProtocolType.HTTPS ) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
194 |
// silently skip unsupported test combination |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
195 |
return; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
196 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
197 |
System.out.println("\n**** Testing " + protocol + " " |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
198 |
+ mode + " mode ****\n"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
199 |
int authCount = AUTHENTICATOR.count.get(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
200 |
int expectedIncrement = 0; |
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 |
// 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
|
203 |
// request body. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
204 |
HTTPTestServer server = |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
205 |
HTTPTestServer.create(protocol, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
206 |
mode, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
207 |
AUTHENTICATOR, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
208 |
getHttpSchemeType()); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
209 |
try { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
210 |
expectedIncrement += run(server, protocol, mode); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
211 |
} finally { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
212 |
server.stop(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
213 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
214 |
} catch (IOException ex) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
215 |
ex.printStackTrace(System.err); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
216 |
throw new UncheckedIOException(ex); |
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 |
int count = AUTHENTICATOR.count.get(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
219 |
if (count != authCount + expectedIncrement) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
220 |
throw new AssertionError("Authenticator called " + count(count) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
221 |
+ " expected it to be called " |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
222 |
+ expected(authCount + expectedIncrement)); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
223 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
224 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
225 |
|
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 |
* Runs the test with the given parameters. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
228 |
* @param server The server |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
229 |
* @param protocol The protocol (HTTP/HTTPS) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
230 |
* @param mode The mode (PROXY, SERVER, SERVER307...) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
231 |
* @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
|
232 |
* called. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
233 |
* @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
|
234 |
*/ |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
235 |
public int run(HTTPTestServer server, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
236 |
HttpProtocolType protocol, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
237 |
HttpAuthType mode) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
238 |
throws IOException |
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 |
// 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
|
241 |
// POST that contains "Hello World!" |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
242 |
HTTPTestClient.connect(protocol, server, mode, null); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
243 |
// 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
|
244 |
// to have been called. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
245 |
return EXPECTED_AUTH_CALLS_PER_TEST; |
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 |
public static String count(int count) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
249 |
switch(count) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
250 |
case 0: return "not even once"; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
251 |
case 1: return "once"; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
252 |
case 2: return "twice"; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
253 |
default: return String.valueOf(count) + " times"; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
254 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
255 |
} |
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 |
public static String expected(int count) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
258 |
switch(count) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
259 |
default: return count(count); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
260 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
261 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
262 |
public static String protocol(HttpProtocolType type) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
263 |
return type.name().toLowerCase(Locale.US); |
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 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
266 |
public static URL url(HttpProtocolType protocol, InetSocketAddress address, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
267 |
String path) throws MalformedURLException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
268 |
return new URL(protocol(protocol), |
55522
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
52387
diff
changeset
|
269 |
address.getAddress().getHostAddress(), |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
270 |
address.getPort(), path); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
271 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
272 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
273 |
public static Proxy proxy(HTTPTestServer server, HttpAuthType authType) { |
55522
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
52387
diff
changeset
|
274 |
if (authType != HttpAuthType.PROXY) return null; |
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
52387
diff
changeset
|
275 |
|
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
52387
diff
changeset
|
276 |
InetSocketAddress proxyAddress = server.getProxyAddress(); |
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
52387
diff
changeset
|
277 |
if (!proxyAddress.isUnresolved()) { |
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
52387
diff
changeset
|
278 |
// Forces the proxy to use an unresolved address created |
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
52387
diff
changeset
|
279 |
// from the actual IP address to avoid using the proxy |
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
52387
diff
changeset
|
280 |
// address hostname which would result in resolving to |
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
52387
diff
changeset
|
281 |
// a posibly different address. For instance we want to |
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
52387
diff
changeset
|
282 |
// avoid cases such as: |
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
52387
diff
changeset
|
283 |
// ::1 => "localhost" => 127.0.0.1 |
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
52387
diff
changeset
|
284 |
proxyAddress = InetSocketAddress. |
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
52387
diff
changeset
|
285 |
createUnresolved(proxyAddress.getAddress().getHostAddress(), |
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
52387
diff
changeset
|
286 |
proxyAddress.getPort()); |
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
52387
diff
changeset
|
287 |
} |
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
52387
diff
changeset
|
288 |
|
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
52387
diff
changeset
|
289 |
return new Proxy(Proxy.Type.HTTP, proxyAddress); |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
290 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
291 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
292 |
public static HttpURLConnection openConnection(URL url, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
293 |
HttpAuthType authType, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
294 |
Proxy proxy) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
295 |
throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
296 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
297 |
HttpURLConnection conn = (HttpURLConnection) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
298 |
(authType == HttpAuthType.PROXY |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
299 |
? url.openConnection(proxy) |
58365
73950479184b
8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents:
57793
diff
changeset
|
300 |
: url.openConnection(NO_PROXY)); |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
301 |
return conn; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
302 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
303 |
} |