author | amurillo |
Wed, 15 Aug 2012 16:49:38 -0700 | |
changeset 13465 | d3fc5d192448 |
parent 7668 | d4a77089c587 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2003, 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 |
/* @test |
|
25 |
* @summary Unit test for java.net.CookieHandler |
|
26 |
* @bug 4696506 |
|
5146
14e4cb8ed49a
6933629: java/net/HttpURLConnection/HttpResponseCode.java fails if run in samevm mode
chegar
parents:
2
diff
changeset
|
27 |
* @run main/othervm CookieHandlerTest |
2 | 28 |
* @author Yingxian Wang |
29 |
*/ |
|
30 |
||
5146
14e4cb8ed49a
6933629: java/net/HttpURLConnection/HttpResponseCode.java fails if run in samevm mode
chegar
parents:
2
diff
changeset
|
31 |
// Run in othervm since a default cookier handler is set and this |
14e4cb8ed49a
6933629: java/net/HttpURLConnection/HttpResponseCode.java fails if run in samevm mode
chegar
parents:
2
diff
changeset
|
32 |
// can effect other HTTP related tests. |
14e4cb8ed49a
6933629: java/net/HttpURLConnection/HttpResponseCode.java fails if run in samevm mode
chegar
parents:
2
diff
changeset
|
33 |
|
2 | 34 |
import java.net.*; |
35 |
import java.util.*; |
|
36 |
import java.io.*; |
|
37 |
||
38 |
public class CookieHandlerTest implements Runnable { |
|
39 |
static Map<String,String> cookies; |
|
40 |
ServerSocket ss; |
|
41 |
||
42 |
/* |
|
43 |
* Our "http" server to return a 404 |
|
44 |
*/ |
|
45 |
public void run() { |
|
46 |
try { |
|
47 |
Socket s = ss.accept(); |
|
48 |
||
49 |
// check request contains "Cookie" |
|
50 |
InputStream is = s.getInputStream (); |
|
51 |
BufferedReader r = new BufferedReader(new InputStreamReader(is)); |
|
52 |
boolean flag = false; |
|
53 |
String x; |
|
54 |
while ((x=r.readLine()) != null) { |
|
55 |
if (x.length() ==0) { |
|
56 |
break; |
|
57 |
} |
|
58 |
String header = "Cookie: "; |
|
59 |
if (x.startsWith(header)) { |
|
60 |
if (x.equals("Cookie: "+((String)cookies.get("Cookie")))) { |
|
61 |
flag = true; |
|
62 |
} |
|
63 |
} |
|
64 |
} |
|
65 |
if (!flag) { |
|
66 |
throw new RuntimeException("server should see cookie in request"); |
|
67 |
} |
|
68 |
||
69 |
PrintStream out = new PrintStream( |
|
70 |
new BufferedOutputStream( |
|
71 |
s.getOutputStream() )); |
|
72 |
||
73 |
/* send the header */ |
|
74 |
out.print("HTTP/1.1 200 OK\r\n"); |
|
75 |
out.print("Set-Cookie2: "+((String)cookies.get("Set-Cookie2")+"\r\n")); |
|
76 |
out.print("Content-Type: text/html; charset=iso-8859-1\r\n"); |
|
77 |
out.print("Connection: close\r\n"); |
|
78 |
out.print("\r\n"); |
|
79 |
out.print("<HTML>"); |
|
80 |
out.print("<HEAD><TITLE>Testing cookie</TITLE></HEAD>"); |
|
81 |
out.print("<BODY>OK.</BODY>"); |
|
82 |
out.print("</HTML>"); |
|
83 |
out.flush(); |
|
84 |
||
85 |
s.close(); |
|
86 |
ss.close(); |
|
87 |
} catch (Exception e) { |
|
88 |
e.printStackTrace(); |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
CookieHandlerTest() throws Exception { |
|
93 |
||
94 |
/* start the server */ |
|
95 |
ss = new ServerSocket(0); |
|
96 |
(new Thread(this)).start(); |
|
97 |
||
98 |
/* establish http connection to server */ |
|
99 |
String uri = "http://localhost:" + |
|
100 |
Integer.toString(ss.getLocalPort()); |
|
101 |
URL url = new URL(uri); |
|
102 |
||
103 |
HttpURLConnection http = (HttpURLConnection)url.openConnection(); |
|
104 |
||
105 |
int respCode = http.getResponseCode(); |
|
106 |
http.disconnect(); |
|
107 |
||
108 |
} |
|
109 |
public static void main(String args[]) throws Exception { |
|
110 |
cookies = new HashMap<String, String>(); |
|
111 |
cookies.put("Cookie", "$Version=\"1\"; Customer=\"WILE_E_COYOTE\"; $Path=\"/acme\""); |
|
112 |
cookies.put("Set-Cookie2", "$Version=\"1\"; Part_Number=\"Riding_Rocket_0023\"; $Path=\"/acme/ammo\"; Part_Number=\"Rocket_Launcher_0001\"; $Path=\"/acme\""); |
|
113 |
CookieHandler.setDefault(new MyCookieHandler()); |
|
114 |
new CookieHandlerTest(); |
|
115 |
} |
|
116 |
||
117 |
static class MyCookieHandler extends CookieHandler { |
|
118 |
public Map<String,List<String>> |
|
119 |
get(URI uri, Map<String,List<String>> requestHeaders) |
|
120 |
throws IOException { |
|
121 |
// returns cookies[0] |
|
122 |
// they will be include in request |
|
123 |
Map<String,List<String>> map = new HashMap<String,List<String>>(); |
|
124 |
List<String> l = new ArrayList<String>(); |
|
125 |
l.add(cookies.get("Cookie")); |
|
126 |
map.put("Cookie",l); |
|
127 |
return Collections.unmodifiableMap(map); |
|
128 |
} |
|
129 |
||
130 |
public void |
|
131 |
put(URI uri, Map<String,List<String>> responseHeaders) |
|
132 |
throws IOException { |
|
133 |
// check response has cookies[1] |
|
134 |
List<String> l = responseHeaders.get("Set-Cookie2"); |
|
135 |
String value = l.get(0); |
|
136 |
if (!value.equals(cookies.get("Set-Cookie2"))) { |
|
137 |
throw new RuntimeException("cookie should be available for handle to put into cache"); |
|
138 |
} |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
} |