author | dfuchs |
Tue, 07 May 2019 18:10:59 +0100 | |
changeset 54746 | 61049e91eae5 |
parent 47216 | 71c04702a3d5 |
child 58009 | 0daf32316b47 |
child 58678 | 9cf78a70fa4f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54746
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 { |
|
54746
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
63 |
InetAddress loopback = InetAddress.getLoopbackAddress(); |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
64 |
ss = new ServerSocket(); |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
65 |
ss.bind(new InetSocketAddress(loopback, 0)); |
2 | 66 |
} catch (IOException ioe) { |
67 |
throw new Error("Unable to create ServerSocket: " + ioe); |
|
68 |
} |
|
69 |
} |
|
70 |
||
71 |
public int port() { |
|
72 |
return ss.getLocalPort(); |
|
73 |
} |
|
74 |
||
54746
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
75 |
public String authority() { |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
76 |
InetAddress address = ss.getInetAddress(); |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
77 |
String hostaddr = address.isAnyLocalAddress() |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
78 |
? "localhost" : address.getHostAddress(); |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
79 |
if (hostaddr.indexOf(':') > -1) { |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
80 |
hostaddr = "[" + hostaddr + "]"; |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
81 |
} |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
82 |
return hostaddr + ":" + port(); |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
83 |
} |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
84 |
|
2 | 85 |
public void shutdown() throws IOException { |
86 |
ss.close(); |
|
87 |
} |
|
88 |
||
89 |
public void run() { |
|
90 |
Object[][] tests = getTests(); |
|
91 |
||
92 |
try { |
|
93 |
for (;;) { |
|
94 |
Socket s = ss.accept(); |
|
95 |
||
96 |
BufferedReader in = new BufferedReader( |
|
97 |
new InputStreamReader( |
|
98 |
s.getInputStream())); |
|
99 |
String req = in.readLine(); |
|
100 |
int pos1 = req.indexOf(' '); |
|
101 |
int pos2 = req.indexOf(' ', pos1+1); |
|
102 |
||
103 |
int i = Integer.parseInt(req.substring(pos1+2, pos2)); |
|
104 |
||
105 |
PrintStream out = new PrintStream( |
|
106 |
new BufferedOutputStream( |
|
107 |
s.getOutputStream() )); |
|
108 |
||
109 |
out.print( tests[i][0] ); |
|
110 |
out.print("\r\n"); |
|
111 |
out.print("Content-Length: 0\r\n"); |
|
112 |
out.print("Connection: close\r\n"); |
|
113 |
out.print("\r\n"); |
|
114 |
out.flush(); |
|
115 |
||
116 |
s.shutdownOutput(); |
|
117 |
s.close(); |
|
118 |
} |
|
119 |
} catch (Exception e) { |
|
120 |
} |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
||
125 |
public static void main(String args[]) throws Exception { |
|
126 |
||
127 |
/* start the http server */ |
|
128 |
HttpServer svr = new HttpServer(); |
|
129 |
(new Thread(svr)).start(); |
|
130 |
||
54746
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
131 |
String authority = svr.authority(); |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
132 |
System.out.println("Server listening on: " + authority); |
2 | 133 |
|
134 |
/* |
|
135 |
* Iterate through each test case and check that getResponseCode |
|
136 |
* returns the expected result. |
|
137 |
*/ |
|
138 |
int failures = 0; |
|
139 |
Object tests[][] = getTests(); |
|
140 |
for (int i=0; i<tests.length; i++) { |
|
141 |
||
142 |
System.out.println("******************"); |
|
143 |
System.out.println("Test with response: >" + tests[i][0] + "<"); |
|
144 |
||
54746
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
145 |
URL url = new URL("http://" + authority + "/" + i); |
2 | 146 |
HttpURLConnection http = (HttpURLConnection)url.openConnection(); |
147 |
||
148 |
try { |
|
149 |
||
150 |
// test getResponseCode |
|
151 |
// |
|
152 |
int expectedCode = Integer.parseInt((String)tests[i][1]); |
|
153 |
int actualCode = http.getResponseCode(); |
|
154 |
if (actualCode != expectedCode) { |
|
155 |
System.out.println("getResponseCode returned: " + actualCode + |
|
156 |
", expected: " + expectedCode); |
|
157 |
failures++; |
|
158 |
continue; |
|
159 |
} |
|
160 |
||
161 |
// test getResponseMessage |
|
162 |
// |
|
163 |
String expectedPhrase = (String)tests[i][2]; |
|
164 |
String actualPhrase = http.getResponseMessage(); |
|
165 |
if (actualPhrase == null && expectedPhrase == null) { |
|
166 |
continue; |
|
167 |
} |
|
168 |
if (!actualPhrase.equals(expectedPhrase)) { |
|
169 |
System.out.println("getResponseMessage returned: " + |
|
170 |
actualPhrase + ", expected: " + expectedPhrase); |
|
171 |
} |
|
172 |
} catch (IOException e) { |
|
173 |
e.printStackTrace(); |
|
174 |
failures++; |
|
175 |
} |
|
176 |
} |
|
177 |
||
178 |
/* shutdown http server */ |
|
179 |
svr.shutdown(); |
|
180 |
||
181 |
if (failures > 0) { |
|
182 |
throw new Exception(failures + " sub-test(s) failed."); |
|
183 |
} |
|
184 |
} |
|
185 |
} |