Path: blob/master/test/jdk/java/net/CookieHandler/CookieHandlerTest.java
41152 views
/*1* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @summary Unit test for java.net.CookieHandler25* @bug 469650626* @library /test/lib27* @run main/othervm CookieHandlerTest28* @run main/othervm -Djava.net.preferIPv6Addresses=true CookieHandlerTest29* @author Yingxian Wang30*/3132// Run in othervm since a default cookier handler is set and this33// can effect other HTTP related tests.3435import java.net.*;36import java.util.*;37import java.io.*;38import jdk.test.lib.net.URIBuilder;3940public class CookieHandlerTest implements Runnable {41static Map<String,String> cookies;42ServerSocket ss;4344/*45* Our "http" server to return a 40446*/47public void run() {48try {49Socket s = ss.accept();5051// check request contains "Cookie"52InputStream is = s.getInputStream ();53BufferedReader r = new BufferedReader(new InputStreamReader(is));54boolean flag = false;55String x;56while ((x=r.readLine()) != null) {57if (x.length() ==0) {58break;59}60String header = "Cookie: ";61if (x.startsWith(header)) {62if (x.equals("Cookie: "+((String)cookies.get("Cookie")))) {63flag = true;64}65}66}67if (!flag) {68throw new RuntimeException("server should see cookie in request");69}7071PrintStream out = new PrintStream(72new BufferedOutputStream(73s.getOutputStream() ));7475/* send the header */76out.print("HTTP/1.1 200 OK\r\n");77out.print("Set-Cookie2: "+((String)cookies.get("Set-Cookie2")+"\r\n"));78out.print("Content-Type: text/html; charset=iso-8859-1\r\n");79out.print("Connection: close\r\n");80out.print("\r\n");81out.print("<HTML>");82out.print("<HEAD><TITLE>Testing cookie</TITLE></HEAD>");83out.print("<BODY>OK.</BODY>");84out.print("</HTML>");85out.flush();8687s.close();88ss.close();89} catch (Exception e) {90e.printStackTrace();91}92}9394CookieHandlerTest() throws Exception {9596/* start the server */97InetAddress loopback = InetAddress.getLoopbackAddress();98ss = new ServerSocket();99ss.bind(new InetSocketAddress(loopback, 0));100(new Thread(this)).start();101102/* establish http connection to server */103URL url = URIBuilder.newBuilder()104.scheme("http")105.loopback()106.port(ss.getLocalPort())107.toURL();108109HttpURLConnection http = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);110111int respCode = http.getResponseCode();112http.disconnect();113114}115public static void main(String args[]) throws Exception {116cookies = new HashMap<String, String>();117cookies.put("Cookie", "$Version=\"1\"; Customer=\"WILE_E_COYOTE\"; $Path=\"/acme\"");118cookies.put("Set-Cookie2", "$Version=\"1\"; Part_Number=\"Riding_Rocket_0023\"; $Path=\"/acme/ammo\"; Part_Number=\"Rocket_Launcher_0001\"; $Path=\"/acme\"");119CookieHandler.setDefault(new MyCookieHandler());120new CookieHandlerTest();121}122123static class MyCookieHandler extends CookieHandler {124public Map<String,List<String>>125get(URI uri, Map<String,List<String>> requestHeaders)126throws IOException {127// returns cookies[0]128// they will be include in request129Map<String,List<String>> map = new HashMap<String,List<String>>();130List<String> l = new ArrayList<String>();131l.add(cookies.get("Cookie"));132map.put("Cookie",l);133return Collections.unmodifiableMap(map);134}135136public void137put(URI uri, Map<String,List<String>> responseHeaders)138throws IOException {139// check response has cookies[1]140List<String> l = responseHeaders.get("Set-Cookie2");141String value = l.get(0);142if (!value.equals(cookies.get("Set-Cookie2"))) {143throw new RuntimeException("cookie should be available for handle to put into cache");144}145}146}147148}149150151