Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/HttpClient/B7025238.java
38868 views
1
/*
2
* Copyright (c) 2013, 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
import com.sun.net.httpserver.*;
25
import java.io.IOException;
26
import java.io.OutputStream;
27
import java.net.*;
28
import java.util.concurrent.Executors;
29
30
/*
31
* @test
32
* @bug 7025238
33
* @summary HttpURLConnection does not handle URLs with an empty path component
34
*/
35
public class B7025238 {
36
37
public static void main(String[] args) throws Exception {
38
new B7025238().runTest();
39
}
40
41
public void runTest() throws Exception {
42
Server s = null;
43
try {
44
s = new Server();
45
s.startServer();
46
URL url = new URL("http://localhost:" + s.getPort() + "?q=test");
47
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
48
urlConnection.setRequestMethod("GET");
49
urlConnection.connect();
50
int code = urlConnection.getResponseCode();
51
52
if (code != 200) {
53
throw new RuntimeException("Test failed!");
54
}
55
} finally {
56
s.stopServer();
57
}
58
}
59
60
class Server {
61
HttpServer server;
62
63
public void startServer() {
64
InetSocketAddress addr = new InetSocketAddress(0);
65
try {
66
server = HttpServer.create(addr, 0);
67
} catch (IOException ioe) {
68
throw new RuntimeException("Server could not be created");
69
}
70
71
server.createContext("/", new EmptyPathHandler());
72
server.start();
73
}
74
75
public int getPort() {
76
return server.getAddress().getPort();
77
}
78
79
public void stopServer() {
80
server.stop(0);
81
}
82
}
83
84
class EmptyPathHandler implements HttpHandler {
85
86
@Override
87
public void handle(HttpExchange exchange) throws IOException {
88
String requestMethod = exchange.getRequestMethod();
89
90
if (requestMethod.equalsIgnoreCase("GET")) {
91
Headers responseHeaders = exchange.getResponseHeaders();
92
responseHeaders.set("Content-Type", "text/plain");
93
exchange.sendResponseHeaders(200, 0);
94
OutputStream os = exchange.getResponseBody();
95
String str = "Hello from server!";
96
os.write(str.getBytes());
97
os.flush();
98
os.close();
99
}
100
}
101
}
102
}
103
104
105