Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/URLConnection/URLConnectionHeaders.java
38821 views
/*1* Copyright (c) 2001, 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/*24* @test25* @bug 4143541 4147035 424436226* @summary URLConnection cannot enumerate request properties,27* and URLConnection can neither get nor set multiple28* request properties w/ same key29*30*/3132import java.net.*;33import java.util.*;34import java.io.*;3536public class URLConnectionHeaders {3738static class XServer extends Thread {39ServerSocket srv;40Socket s;41InputStream is;42OutputStream os;4344XServer (ServerSocket s) {45srv = s;46}4748Socket getSocket () {49return (s);50}5152public void run() {53try {54String x;55s = srv.accept ();56is = s.getInputStream ();57BufferedReader r = new BufferedReader(new InputStreamReader(is));58os = s.getOutputStream ();59BufferedWriter w = new BufferedWriter(new OutputStreamWriter(os));60w.write("HTTP/1.1 200 OK\r\n");61w.write("Content-Type: text/plain\r\n");62while ((x=r.readLine()).length() != 0) {63System.out.println("request: "+x);64if (!x.startsWith("GET")) {65w.write(x);66w.newLine();67}68}69w.newLine();70w.flush();71s.close ();72} catch (IOException e) { e.printStackTrace();73} finally {74try { srv.close(); } catch (IOException unused) {}75}76}77}7879public static void main(String[] args) {80try {81ServerSocket serversocket = new ServerSocket (0);82int port = serversocket.getLocalPort ();83XServer server = new XServer (serversocket);84server.start ();85Thread.sleep (200);86URL url = new URL ("http://localhost:"+port+"/index.html");87URLConnection uc = url.openConnection ();8889// add request properties90uc.addRequestProperty("Cookie", "cookie1");91uc.addRequestProperty("Cookie", "cookie2");92uc.addRequestProperty("Cookie", "cookie3");9394Map e = uc.getRequestProperties();9596if (!((List)e.get("Cookie")).toString().equals("[cookie3, cookie2, cookie1]")) {97throw new RuntimeException("getRequestProperties fails");98}99100e = uc.getHeaderFields();101102if ((e.get("Content-Type") == null) || (e.get(null) == null)) {103throw new RuntimeException("getHeaderFields fails");104}105106} catch (Exception e) {107e.printStackTrace();108}109}110}111112113