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/HttpURLConnection/PostOnDelete.java
38867 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.InputStream;
27
import java.io.OutputStream;
28
import java.net.*;
29
30
/*
31
* @test
32
* @bug 7157360
33
* @summary HttpURLConnection: HTTP method DELETE doesn't support output
34
*/
35
public class PostOnDelete {
36
37
/* string to send */
38
private static String msg = "Hello Server";
39
/* length of the string to verify */
40
private int len = msg.length();
41
42
public static void main(String[] args) throws Exception {
43
new PostOnDelete().runTest();
44
}
45
46
public void runTest() throws Exception {
47
Server s = null;
48
try {
49
s = new Server();
50
s.startServer();
51
URL url = new URL("http://localhost:" + s.getPort());
52
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
53
urlConnection.setRequestMethod("DELETE");
54
urlConnection.setDoOutput(true);
55
OutputStream os = urlConnection.getOutputStream();
56
os.write(msg.getBytes());
57
os.close();
58
int code = urlConnection.getResponseCode();
59
60
if (code != 200) {
61
throw new RuntimeException("Request entity for DELETE failed!");
62
}
63
} finally {
64
s.stopServer();
65
}
66
}
67
68
class Server {
69
HttpServer server;
70
71
public void startServer() {
72
InetSocketAddress addr = new InetSocketAddress(0);
73
try {
74
server = HttpServer.create(addr, 0);
75
} catch (IOException ioe) {
76
throw new RuntimeException("Server could not be created");
77
}
78
79
server.createContext("/", new EmptyPathHandler());
80
server.start();
81
}
82
83
public int getPort() {
84
return server.getAddress().getPort();
85
}
86
87
public void stopServer() {
88
server.stop(0);
89
}
90
}
91
92
class EmptyPathHandler implements HttpHandler {
93
94
@Override
95
public void handle(HttpExchange exchange) throws IOException {
96
String requestMethod = exchange.getRequestMethod();
97
98
if (requestMethod.equalsIgnoreCase("DELETE")) {
99
InputStream is = exchange.getRequestBody();
100
101
int count = 0;
102
while (is.read() != -1) {
103
count++;
104
}
105
is.close();
106
107
Headers responseHeaders = exchange.getResponseHeaders();
108
responseHeaders.set("Content-Type", "text/plain");
109
exchange.sendResponseHeaders((count == len) ? 200 : 400, 0);
110
OutputStream os = exchange.getResponseBody();
111
String str = "Hello from server!";
112
os.write(str.getBytes());
113
os.flush();
114
os.close();
115
}
116
}
117
}
118
}
119
120