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