author | mikael |
Fri, 04 Dec 2015 15:08:49 -0800 | |
changeset 35090 | 1f5b6aa795d0 |
parent 7668 | d4a77089c587 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2001, 2010, 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 |
|
30 |
* |
|
31 |
*/ |
|
32 |
||
33 |
import java.net.*; |
|
34 |
import java.util.*; |
|
35 |
import java.io.*; |
|
36 |
||
37 |
public class URLConnectionHeaders { |
|
38 |
||
39 |
static class XServer extends Thread { |
|
40 |
ServerSocket srv; |
|
41 |
Socket s; |
|
42 |
InputStream is; |
|
43 |
OutputStream os; |
|
44 |
||
45 |
XServer (ServerSocket s) { |
|
46 |
srv = s; |
|
47 |
} |
|
48 |
||
49 |
Socket getSocket () { |
|
50 |
return (s); |
|
51 |
} |
|
52 |
||
53 |
public void run() { |
|
54 |
try { |
|
55 |
String x; |
|
56 |
s = srv.accept (); |
|
57 |
is = s.getInputStream (); |
|
58 |
BufferedReader r = new BufferedReader(new InputStreamReader(is)); |
|
59 |
os = s.getOutputStream (); |
|
60 |
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(os)); |
|
61 |
w.write("HTTP/1.1 200 OK\r\n"); |
|
62 |
w.write("Content-Type: text/plain\r\n"); |
|
63 |
while ((x=r.readLine()).length() != 0) { |
|
64 |
System.out.println("request: "+x); |
|
65 |
if (!x.startsWith("GET")) { |
|
66 |
w.write(x); |
|
67 |
w.newLine(); |
|
68 |
} |
|
69 |
} |
|
70 |
w.newLine(); |
|
71 |
w.flush(); |
|
72 |
s.close (); |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
73 |
} catch (IOException e) { e.printStackTrace(); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
74 |
} finally { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
75 |
try { srv.close(); } catch (IOException unused) {} |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
76 |
} |
2 | 77 |
} |
78 |
} |
|
79 |
||
80 |
public static void main(String[] args) { |
|
81 |
try { |
|
82 |
ServerSocket serversocket = new ServerSocket (0); |
|
83 |
int port = serversocket.getLocalPort (); |
|
84 |
XServer server = new XServer (serversocket); |
|
85 |
server.start (); |
|
86 |
Thread.sleep (200); |
|
87 |
URL url = new URL ("http://localhost:"+port+"/index.html"); |
|
88 |
URLConnection uc = url.openConnection (); |
|
89 |
||
90 |
// add request properties |
|
91 |
uc.addRequestProperty("Cookie", "cookie1"); |
|
92 |
uc.addRequestProperty("Cookie", "cookie2"); |
|
93 |
uc.addRequestProperty("Cookie", "cookie3"); |
|
94 |
||
95 |
Map e = uc.getRequestProperties(); |
|
96 |
||
97 |
if (!((List)e.get("Cookie")).toString().equals("[cookie3, cookie2, cookie1]")) { |
|
98 |
throw new RuntimeException("getRequestProperties fails"); |
|
99 |
} |
|
100 |
||
101 |
e = uc.getHeaderFields(); |
|
102 |
||
103 |
if ((e.get("Content-Type") == null) || (e.get(null) == null)) { |
|
104 |
throw new RuntimeException("getHeaderFields fails"); |
|
105 |
} |
|
106 |
||
107 |
} catch (Exception e) { |
|
108 |
e.printStackTrace(); |
|
109 |
} |
|
110 |
} |
|
111 |
} |