Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/CookieHandler/CookieHandlerTest.java
38812 views
/*1* Copyright (c) 2003, 2010, 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* @run main/othervm CookieHandlerTest27* @author Yingxian Wang28*/2930// Run in othervm since a default cookier handler is set and this31// can effect other HTTP related tests.3233import java.net.*;34import java.util.*;35import java.io.*;3637public class CookieHandlerTest implements Runnable {38static Map<String,String> cookies;39ServerSocket ss;4041/*42* Our "http" server to return a 40443*/44public void run() {45try {46Socket s = ss.accept();4748// check request contains "Cookie"49InputStream is = s.getInputStream ();50BufferedReader r = new BufferedReader(new InputStreamReader(is));51boolean flag = false;52String x;53while ((x=r.readLine()) != null) {54if (x.length() ==0) {55break;56}57String header = "Cookie: ";58if (x.startsWith(header)) {59if (x.equals("Cookie: "+((String)cookies.get("Cookie")))) {60flag = true;61}62}63}64if (!flag) {65throw new RuntimeException("server should see cookie in request");66}6768PrintStream out = new PrintStream(69new BufferedOutputStream(70s.getOutputStream() ));7172/* send the header */73out.print("HTTP/1.1 200 OK\r\n");74out.print("Set-Cookie2: "+((String)cookies.get("Set-Cookie2")+"\r\n"));75out.print("Content-Type: text/html; charset=iso-8859-1\r\n");76out.print("Connection: close\r\n");77out.print("\r\n");78out.print("<HTML>");79out.print("<HEAD><TITLE>Testing cookie</TITLE></HEAD>");80out.print("<BODY>OK.</BODY>");81out.print("</HTML>");82out.flush();8384s.close();85ss.close();86} catch (Exception e) {87e.printStackTrace();88}89}9091CookieHandlerTest() throws Exception {9293/* start the server */94ss = new ServerSocket(0);95(new Thread(this)).start();9697/* establish http connection to server */98String uri = "http://localhost:" +99Integer.toString(ss.getLocalPort());100URL url = new URL(uri);101102HttpURLConnection http = (HttpURLConnection)url.openConnection();103104int respCode = http.getResponseCode();105http.disconnect();106107}108public static void main(String args[]) throws Exception {109cookies = new HashMap<String, String>();110cookies.put("Cookie", "$Version=\"1\"; Customer=\"WILE_E_COYOTE\"; $Path=\"/acme\"");111cookies.put("Set-Cookie2", "$Version=\"1\"; Part_Number=\"Riding_Rocket_0023\"; $Path=\"/acme/ammo\"; Part_Number=\"Rocket_Launcher_0001\"; $Path=\"/acme\"");112CookieHandler.setDefault(new MyCookieHandler());113new CookieHandlerTest();114}115116static class MyCookieHandler extends CookieHandler {117public Map<String,List<String>>118get(URI uri, Map<String,List<String>> requestHeaders)119throws IOException {120// returns cookies[0]121// they will be include in request122Map<String,List<String>> map = new HashMap<String,List<String>>();123List<String> l = new ArrayList<String>();124l.add(cookies.get("Cookie"));125map.put("Cookie",l);126return Collections.unmodifiableMap(map);127}128129public void130put(URI uri, Map<String,List<String>> responseHeaders)131throws IOException {132// check response has cookies[1]133List<String> l = responseHeaders.get("Set-Cookie2");134String value = l.get(0);135if (!value.equals(cookies.get("Set-Cookie2"))) {136throw new RuntimeException("cookie should be available for handle to put into cache");137}138}139}140141}142143144