Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/URLConnection/Responses.java
41149 views
1
/*
2
* Copyright (c) 2002, 2019, 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
/*
25
* @test
26
* @bug 4635698
27
* @summary Check that HttpURLConnection.getResponseCode returns -1 for
28
* malformed status-lines in the http response.
29
*/
30
import java.net.*;
31
import java.io.*;
32
import static java.net.Proxy.NO_PROXY;
33
34
public class Responses {
35
36
/*
37
* Test cases :-
38
* "Response from server" expected getResponse() and
39
* getResponseMessage() results
40
*/
41
static Object[][] getTests() {
42
return new Object[][] {
43
{ "HTTP/1.1 200 OK", "200", "OK" },
44
{ "HTTP/1.1 404 ", "404", null },
45
{ "HTTP/1.1 200", "200", null },
46
{ "HTTP/1.1", "-1", null },
47
{ "Invalid", "-1", null },
48
{ null, "-1" , null },
49
};
50
}
51
52
/*
53
* Simple http server used by test
54
*
55
* GET /<n> HTTP/1.x results in http response with the status line
56
* set to geTests()[<n>][0] -- eg: GET /2 results in a response of
57
* "HTTP/1.1 404 "
58
*/
59
static class HttpServer implements Runnable {
60
final ServerSocket ss;
61
volatile boolean shutdown;
62
63
public HttpServer() {
64
try {
65
InetAddress loopback = InetAddress.getLoopbackAddress();
66
ss = new ServerSocket();
67
ss.bind(new InetSocketAddress(loopback, 0));
68
} catch (IOException ioe) {
69
throw new Error("Unable to create ServerSocket: " + ioe);
70
}
71
}
72
73
public int port() {
74
return ss.getLocalPort();
75
}
76
77
public String authority() {
78
InetAddress address = ss.getInetAddress();
79
String hostaddr = address.isAnyLocalAddress()
80
? "localhost" : address.getHostAddress();
81
if (hostaddr.indexOf(':') > -1) {
82
hostaddr = "[" + hostaddr + "]";
83
}
84
return hostaddr + ":" + port();
85
}
86
87
public void shutdown() throws IOException {
88
shutdown = true;
89
ss.close();
90
}
91
92
public void run() {
93
Object[][] tests = getTests();
94
95
try {
96
while(!shutdown) {
97
Socket s = ss.accept();
98
99
BufferedReader in = new BufferedReader(
100
new InputStreamReader(
101
s.getInputStream()));
102
String req = in.readLine();
103
int pos1 = req.indexOf(' ');
104
int pos2 = req.indexOf(' ', pos1+1);
105
106
int i = Integer.parseInt(req.substring(pos1+2, pos2));
107
System.out.println("Server replying to >" + tests[i][0] + "<");
108
109
PrintStream out = new PrintStream(
110
new BufferedOutputStream(
111
s.getOutputStream() ));
112
113
out.print( tests[i][0] );
114
out.print("\r\n");
115
out.print("Content-Length: 0\r\n");
116
out.print("Connection: close\r\n");
117
out.print("\r\n");
118
out.flush();
119
120
s.shutdownOutput();
121
s.close();
122
}
123
} catch (Exception e) {
124
if (!shutdown) {
125
e.printStackTrace();
126
}
127
}
128
}
129
}
130
131
132
public static void main(String args[]) throws Exception {
133
134
/* start the http server */
135
HttpServer svr = new HttpServer();
136
(new Thread(svr)).start();
137
138
String authority = svr.authority();
139
System.out.println("Server listening on: " + authority);
140
141
/*
142
* Iterate through each test case and check that getResponseCode
143
* returns the expected result.
144
*/
145
int failures = 0;
146
Object tests[][] = getTests();
147
for (int i=0; i<tests.length; i++) {
148
149
System.out.println("******************");
150
System.out.println("Test with response: >" + tests[i][0] + "<");
151
152
URL url = new URL("http://" + authority + "/" + i);
153
HttpURLConnection http = (HttpURLConnection)url.openConnection(NO_PROXY);
154
155
try {
156
157
// test getResponseCode
158
//
159
int expectedCode = Integer.parseInt((String)tests[i][1]);
160
int actualCode = http.getResponseCode();
161
if (actualCode != expectedCode) {
162
System.out.println("getResponseCode returned: " + actualCode +
163
", expected: " + expectedCode);
164
failures++;
165
continue;
166
}
167
168
// test getResponseMessage
169
//
170
String expectedPhrase = (String)tests[i][2];
171
String actualPhrase = http.getResponseMessage();
172
if (actualPhrase == null && expectedPhrase == null) {
173
continue;
174
}
175
if (!actualPhrase.equals(expectedPhrase)) {
176
System.out.println("getResponseMessage returned: " +
177
actualPhrase + ", expected: " + expectedPhrase);
178
}
179
} catch (IOException e) {
180
System.err.println("Test failed for >" + tests[i][0] + "<: " + e);
181
e.printStackTrace();
182
failures++;
183
}
184
}
185
186
/* shutdown http server */
187
svr.shutdown();
188
189
if (failures > 0) {
190
throw new Exception(failures + " sub-test(s) failed.");
191
}
192
}
193
}
194
195