Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/sun/net/www/http/RequestMethodCheck/RequestMethodEquality.java
66646 views
1
/*
2
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/* @test
25
* @summary This test checks that a broken HttpClient is not returned from the KeepAliveCache
26
* when the intial HttpURLConnection.setRequest method is passed a 'new String("POST")'
27
* rather than the "POST" String literal
28
* @bug 8274779
29
* @library /test/lib
30
* @modules java.base/sun.net.www.http
31
* java.base/sun.net.www.protocol.http
32
* @build java.base/sun.net.www.http.HttpClientAccess
33
* @run testng/othervm RequestMethodEquality
34
*/
35
36
import com.sun.net.httpserver.HttpExchange;
37
import com.sun.net.httpserver.HttpHandler;
38
import com.sun.net.httpserver.HttpServer;
39
import jdk.test.lib.net.URIBuilder;
40
import org.testng.Assert;
41
import org.testng.annotations.AfterTest;
42
import org.testng.annotations.BeforeTest;
43
import org.testng.annotations.Test;
44
import sun.net.www.http.HttpClient;
45
import sun.net.www.http.HttpClientAccess;
46
import sun.net.www.http.KeepAliveCache;
47
import sun.net.www.protocol.http.HttpURLConnection;
48
49
import java.io.IOException;
50
import java.net.InetAddress;
51
import java.net.InetSocketAddress;
52
import java.net.Proxy;
53
import java.net.URL;
54
55
public class RequestMethodEquality {
56
private static final String TEST_CONTEXT = "/reqmethodtest";
57
private HttpServer server;
58
private CustomHandler handler;
59
private HttpClientAccess httpClientAccess;
60
61
@BeforeTest
62
public void setup() throws Exception {
63
handler = new CustomHandler();
64
server = createServer(handler);
65
httpClientAccess = new HttpClientAccess();
66
}
67
68
@AfterTest
69
public void tearDown() throws Exception {
70
if (server != null) {
71
server.stop(0);
72
}
73
}
74
75
@Test
76
public void testHttpClient() throws Exception {
77
HttpURLConnection conn = null;
78
try {
79
URL url = URIBuilder.newBuilder()
80
.scheme("http")
81
.host(server.getAddress().getAddress())
82
.port(server.getAddress().getPort())
83
.path(TEST_CONTEXT)
84
.toURL();
85
86
conn = (HttpURLConnection) url.openConnection();
87
conn.setChunkedStreamingMode(8); // ensures the call to HttpURLConnection.streaming() passes
88
89
int firstConnectTimeout = 1234;
90
HttpClient freshClient = HttpClient.New(url, Proxy.NO_PROXY, firstConnectTimeout, true, conn);
91
freshClient.closeServer(); // ensures that the call to HttpClient.available() fails
92
93
httpClientAccess.setInCache(freshClient, true); // allows the assertion in HttpClient.New to pass
94
95
// Injecting a mock KeepAliveCache that the HttpClient can use
96
KeepAliveCache kac = httpClientAccess.getKeepAliveCache();
97
kac.put(url, null, freshClient);
98
99
// The 'new' keyword is important here as the original code
100
// used '==' rather than String.equals to compare request methods
101
conn.setRequestMethod(new String("POST"));
102
103
// Before the fix, the value returned to 'cachedClient' would have been the (broken) cached
104
// 'freshClient' as HttpClient.available() could never be checked
105
int secondConnectTimeout = 4321;
106
HttpClient cachedClient = HttpClient.New(url, Proxy.NO_PROXY, secondConnectTimeout, true, conn);
107
cachedClient.closeServer();
108
109
int originalConnectTimeout = freshClient.getConnectTimeout();
110
int cachedConnectTimeout = cachedClient.getConnectTimeout();
111
112
// If both connectTimeout values are equal, it means the test retrieved the same broken
113
// HttpClient from the cache and is trying to re-use it.
114
Assert.assertNotEquals(originalConnectTimeout, cachedConnectTimeout, "Both connectTimeout values are equal.\nThis means the test is reusing a broken HttpClient rather than creating a new one.");
115
} finally {
116
if (conn != null) {
117
conn.disconnect();
118
}
119
}
120
}
121
122
private static HttpServer createServer(final HttpHandler handler) throws IOException {
123
final InetSocketAddress serverAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
124
final int backlog = -1;
125
final HttpServer server = HttpServer.create(serverAddress, backlog);
126
server.createContext(TEST_CONTEXT, handler);
127
server.start();
128
System.out.println("Server started on " + server.getAddress());
129
return server;
130
}
131
132
private static class CustomHandler implements HttpHandler {
133
@Override
134
public void handle(HttpExchange exchange) throws IOException {
135
// We'll always send 200 OK - We don't care about the server logic
136
exchange.sendResponseHeaders(200, 1);
137
exchange.close();
138
}
139
}
140
}
141
142