blob: 221be09978375130516513b47e271ca0c990e889 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
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
chegara5feee62010-03-11 17:37:33 +000027 * @run main/othervm CookieHandlerTest
duke6e45e102007-12-01 00:00:00 +000028 * @author Yingxian Wang
29 */
30
chegara5feee62010-03-11 17:37:33 +000031// Run in othervm since a default cookier handler is set and this
32// can effect other HTTP related tests.
33
duke6e45e102007-12-01 00:00:00 +000034import java.net.*;
35import java.util.*;
36import java.io.*;
37
38public 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}