1
1
/*
2
- * Copyright (c) 2018, 2023 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2018, 2025 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
23
23
24
24
/*
25
25
* @test
26
- * @bug 8203882
26
+ * @bug 8203882 8352623
27
27
* @summary (httpclient) Check that HttpClient throws IOException when
28
28
* receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate
29
29
* header only in the case where an authenticator is configured
36
36
* UnauthorizedTest
37
37
*/
38
38
39
- import com .sun .net .httpserver .HttpServer ;
40
- import com .sun .net .httpserver .HttpsConfigurator ;
41
- import com .sun .net .httpserver .HttpsServer ;
42
39
import jdk .test .lib .net .SimpleSSLContext ;
43
40
import org .testng .annotations .AfterTest ;
44
41
import org .testng .annotations .BeforeTest ;
49
46
import java .io .IOException ;
50
47
import java .io .InputStream ;
51
48
import java .io .OutputStream ;
49
+ import java .lang .ref .WeakReference ;
52
50
import java .net .Authenticator ;
53
- import java .net .InetAddress ;
54
- import java .net .InetSocketAddress ;
55
51
import java .net .URI ;
56
52
import java .net .http .HttpClient ;
57
53
import java .net .http .HttpRequest ;
60
56
import java .util .concurrent .ExecutionException ;
61
57
import java .util .concurrent .atomic .AtomicLong ;
62
58
import jdk .httpclient .test .lib .common .HttpServerAdapters ;
63
- import jdk .httpclient .test .lib .http2 .Http2TestServer ;
64
59
65
60
import static java .lang .System .out ;
66
61
import static java .net .http .HttpClient .Version .HTTP_1_1 ;
@@ -95,41 +90,45 @@ public class UnauthorizedTest implements HttpServerAdapters {
95
90
static final int HTTP_OK = 200 ;
96
91
static final String MESSAGE = "Unauthorized" ;
97
92
93
+ static WeakReference <HttpClient > ref (HttpClient client ) {
94
+ return new WeakReference <>(client );
95
+ }
96
+
98
97
@ DataProvider (name = "all" )
99
98
public Object [][] positive () {
100
99
return new Object [][] {
101
- { httpURI + "/server" , UNAUTHORIZED , true , authClient },
102
- { httpsURI + "/server" , UNAUTHORIZED , true , authClient },
103
- { http2URI + "/server" , UNAUTHORIZED , true , authClient },
104
- { https2URI + "/server" , UNAUTHORIZED , true , authClient },
105
- { httpURI + "/proxy" , PROXY_UNAUTHORIZED , true , authClient },
106
- { httpsURI + "/proxy" , PROXY_UNAUTHORIZED , true , authClient },
107
- { http2URI + "/proxy" , PROXY_UNAUTHORIZED , true , authClient },
108
- { https2URI + "/proxy" , PROXY_UNAUTHORIZED , true , authClient },
109
- { httpURI + "/server" , UNAUTHORIZED , false , authClient },
110
- { httpsURI + "/server" , UNAUTHORIZED , false , authClient },
111
- { http2URI + "/server" , UNAUTHORIZED , false , authClient },
112
- { https2URI + "/server" , UNAUTHORIZED , false , authClient },
113
- { httpURI + "/proxy" , PROXY_UNAUTHORIZED , false , authClient },
114
- { httpsURI + "/proxy" , PROXY_UNAUTHORIZED , false , authClient },
115
- { http2URI + "/proxy" , PROXY_UNAUTHORIZED , false , authClient },
116
- { https2URI + "/proxy" , PROXY_UNAUTHORIZED , false , authClient },
117
- { httpURI + "/server" , UNAUTHORIZED , true , noAuthClient },
118
- { httpsURI + "/server" , UNAUTHORIZED , true , noAuthClient },
119
- { http2URI + "/server" , UNAUTHORIZED , true , noAuthClient },
120
- { https2URI + "/server" , UNAUTHORIZED , true , noAuthClient },
121
- { httpURI + "/proxy" , PROXY_UNAUTHORIZED , true , noAuthClient },
122
- { httpsURI + "/proxy" , PROXY_UNAUTHORIZED , true , noAuthClient },
123
- { http2URI + "/proxy" , PROXY_UNAUTHORIZED , true , noAuthClient },
124
- { https2URI + "/proxy" , PROXY_UNAUTHORIZED , true , noAuthClient },
125
- { httpURI + "/server" , UNAUTHORIZED , false , noAuthClient },
126
- { httpsURI + "/server" , UNAUTHORIZED , false , noAuthClient },
127
- { http2URI + "/server" , UNAUTHORIZED , false , noAuthClient },
128
- { https2URI + "/server" , UNAUTHORIZED , false , noAuthClient },
129
- { httpURI + "/proxy" , PROXY_UNAUTHORIZED , false , noAuthClient },
130
- { httpsURI + "/proxy" , PROXY_UNAUTHORIZED , false , noAuthClient },
131
- { http2URI + "/proxy" , PROXY_UNAUTHORIZED , false , noAuthClient },
132
- { https2URI + "/proxy" , PROXY_UNAUTHORIZED , false , noAuthClient },
100
+ { httpURI + "/server" , UNAUTHORIZED , true , ref ( authClient ) },
101
+ { httpsURI + "/server" , UNAUTHORIZED , true , ref ( authClient ) },
102
+ { http2URI + "/server" , UNAUTHORIZED , true , ref ( authClient ) },
103
+ { https2URI + "/server" , UNAUTHORIZED , true , ref ( authClient ) },
104
+ { httpURI + "/proxy" , PROXY_UNAUTHORIZED , true , ref ( authClient ) },
105
+ { httpsURI + "/proxy" , PROXY_UNAUTHORIZED , true , ref ( authClient ) },
106
+ { http2URI + "/proxy" , PROXY_UNAUTHORIZED , true , ref ( authClient ) },
107
+ { https2URI + "/proxy" , PROXY_UNAUTHORIZED , true , ref ( authClient ) },
108
+ { httpURI + "/server" , UNAUTHORIZED , false , ref ( authClient ) },
109
+ { httpsURI + "/server" , UNAUTHORIZED , false , ref ( authClient ) },
110
+ { http2URI + "/server" , UNAUTHORIZED , false , ref ( authClient ) },
111
+ { https2URI + "/server" , UNAUTHORIZED , false , ref ( authClient ) },
112
+ { httpURI + "/proxy" , PROXY_UNAUTHORIZED , false , ref ( authClient ) },
113
+ { httpsURI + "/proxy" , PROXY_UNAUTHORIZED , false , ref ( authClient ) },
114
+ { http2URI + "/proxy" , PROXY_UNAUTHORIZED , false , ref ( authClient ) },
115
+ { https2URI + "/proxy" , PROXY_UNAUTHORIZED , false , ref ( authClient ) },
116
+ { httpURI + "/server" , UNAUTHORIZED , true , ref ( noAuthClient ) },
117
+ { httpsURI + "/server" , UNAUTHORIZED , true , ref ( noAuthClient ) },
118
+ { http2URI + "/server" , UNAUTHORIZED , true , ref ( noAuthClient ) },
119
+ { https2URI + "/server" , UNAUTHORIZED , true , ref ( noAuthClient ) },
120
+ { httpURI + "/proxy" , PROXY_UNAUTHORIZED , true , ref ( noAuthClient ) },
121
+ { httpsURI + "/proxy" , PROXY_UNAUTHORIZED , true , ref ( noAuthClient ) },
122
+ { http2URI + "/proxy" , PROXY_UNAUTHORIZED , true , ref ( noAuthClient ) },
123
+ { https2URI + "/proxy" , PROXY_UNAUTHORIZED , true , ref ( noAuthClient ) },
124
+ { httpURI + "/server" , UNAUTHORIZED , false , ref ( noAuthClient ) },
125
+ { httpsURI + "/server" , UNAUTHORIZED , false , ref ( noAuthClient ) },
126
+ { http2URI + "/server" , UNAUTHORIZED , false , ref ( noAuthClient ) },
127
+ { https2URI + "/server" , UNAUTHORIZED , false , ref ( noAuthClient ) },
128
+ { httpURI + "/proxy" , PROXY_UNAUTHORIZED , false , ref ( noAuthClient ) },
129
+ { httpsURI + "/proxy" , PROXY_UNAUTHORIZED , false , ref ( noAuthClient ) },
130
+ { http2URI + "/proxy" , PROXY_UNAUTHORIZED , false , ref ( noAuthClient ) },
131
+ { https2URI + "/proxy" , PROXY_UNAUTHORIZED , false , ref ( noAuthClient ) },
133
132
};
134
133
}
135
134
@@ -139,7 +138,8 @@ public Object[][] positive() {
139
138
};
140
139
141
140
@ Test (dataProvider = "all" )
142
- void test (String uriString , int code , boolean async , HttpClient client ) throws Throwable {
141
+ void test (String uriString , int code , boolean async , WeakReference <HttpClient > clientRef ) throws Throwable {
142
+ HttpClient client = clientRef .get ();
143
143
out .printf ("%n---- starting (%s, %d, %s, %s) ----%n" ,
144
144
uriString , code , async ? "async" : "sync" ,
145
145
client .authenticator ().isPresent () ? "authClient" : "noAuthClient" );
@@ -223,10 +223,20 @@ public void setup() throws Exception {
223
223
224
224
@ AfterTest
225
225
public void teardown () throws Exception {
226
+ // authClient.close();
227
+ // noAuthClient.close();
228
+ var TRACKER = ReferenceTracker .INSTANCE ;
229
+ TRACKER .track (authClient );
230
+ TRACKER .track (noAuthClient );
231
+ authClient = noAuthClient = null ;
232
+ System .gc ();
233
+ var error = TRACKER .check (1000 );
234
+
226
235
httpTestServer .stop ();
227
236
httpsTestServer .stop ();
228
237
http2TestServer .stop ();
229
238
https2TestServer .stop ();
239
+ if (error != null ) throw error ;
230
240
}
231
241
232
242
static class UnauthorizedHandler implements HttpTestHandler {
0 commit comments