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