author | dfuchs |
Mon, 20 May 2019 12:37:40 +0100 | |
changeset 54938 | 8c977741c3c8 |
parent 47216 | 71c04702a3d5 |
child 58365 | 73950479184b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54938
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2001, 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 4143541 4147035 4244362 |
|
27 |
* @summary URLConnection cannot enumerate request properties, |
|
28 |
* and URLConnection can neither get nor set multiple |
|
29 |
* request properties w/ same key |
|
54938
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
30 |
* @library /test/lib |
2 | 31 |
* |
32 |
*/ |
|
33 |
||
34 |
import java.net.*; |
|
35 |
import java.util.*; |
|
36 |
import java.io.*; |
|
54938
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
37 |
import jdk.test.lib.net.URIBuilder; |
2 | 38 |
|
39 |
public class URLConnectionHeaders { |
|
40 |
||
41 |
static class XServer extends Thread { |
|
42 |
ServerSocket srv; |
|
43 |
Socket s; |
|
44 |
InputStream is; |
|
45 |
OutputStream os; |
|
46 |
||
47 |
XServer (ServerSocket s) { |
|
48 |
srv = s; |
|
49 |
} |
|
50 |
||
51 |
Socket getSocket () { |
|
52 |
return (s); |
|
53 |
} |
|
54 |
||
55 |
public void run() { |
|
56 |
try { |
|
57 |
String x; |
|
58 |
s = srv.accept (); |
|
59 |
is = s.getInputStream (); |
|
60 |
BufferedReader r = new BufferedReader(new InputStreamReader(is)); |
|
61 |
os = s.getOutputStream (); |
|
62 |
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(os)); |
|
63 |
w.write("HTTP/1.1 200 OK\r\n"); |
|
64 |
w.write("Content-Type: text/plain\r\n"); |
|
65 |
while ((x=r.readLine()).length() != 0) { |
|
66 |
System.out.println("request: "+x); |
|
67 |
if (!x.startsWith("GET")) { |
|
68 |
w.write(x); |
|
69 |
w.newLine(); |
|
70 |
} |
|
71 |
} |
|
72 |
w.newLine(); |
|
73 |
w.flush(); |
|
74 |
s.close (); |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
75 |
} catch (IOException e) { e.printStackTrace(); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
76 |
} finally { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
77 |
try { srv.close(); } catch (IOException unused) {} |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
78 |
} |
2 | 79 |
} |
80 |
} |
|
81 |
||
54938
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
82 |
public static void main(String[] args) throws Exception { |
2 | 83 |
try { |
54938
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
84 |
InetAddress loopback = InetAddress.getLoopbackAddress(); |
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
85 |
ServerSocket serversocket = new ServerSocket(); |
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
86 |
serversocket.bind(new InetSocketAddress(loopback, 0)); |
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
87 |
int port = serversocket.getLocalPort(); |
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
88 |
XServer server = new XServer(serversocket); |
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
89 |
server.start(); |
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
90 |
Thread.sleep(200); |
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
91 |
URL url = URIBuilder.newBuilder() |
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
92 |
.scheme("http") |
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
93 |
.loopback() |
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
94 |
.port(port) |
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
95 |
.path("/index.html") |
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
96 |
.toURL(); |
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
97 |
URLConnection uc = url.openConnection(); |
2 | 98 |
|
99 |
// add request properties |
|
100 |
uc.addRequestProperty("Cookie", "cookie1"); |
|
101 |
uc.addRequestProperty("Cookie", "cookie2"); |
|
102 |
uc.addRequestProperty("Cookie", "cookie3"); |
|
103 |
||
104 |
Map e = uc.getRequestProperties(); |
|
105 |
||
106 |
if (!((List)e.get("Cookie")).toString().equals("[cookie3, cookie2, cookie1]")) { |
|
107 |
throw new RuntimeException("getRequestProperties fails"); |
|
108 |
} |
|
109 |
||
110 |
e = uc.getHeaderFields(); |
|
111 |
||
112 |
if ((e.get("Content-Type") == null) || (e.get(null) == null)) { |
|
113 |
throw new RuntimeException("getHeaderFields fails"); |
|
114 |
} |
|
115 |
||
116 |
} catch (Exception e) { |
|
117 |
e.printStackTrace(); |
|
54938
8c977741c3c8
8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents:
47216
diff
changeset
|
118 |
throw e; |
2 | 119 |
} |
120 |
} |
|
121 |
} |