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