Path: blob/master/test/jdk/java/net/URLConnection/URLConnectionHeaders.java
41149 views
/*1* Copyright (c) 2001, 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/*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* @library /test/lib30*31*/3233import java.net.*;34import java.util.*;35import java.io.*;36import jdk.test.lib.net.URIBuilder;37import static java.net.Proxy.NO_PROXY;3839public class URLConnectionHeaders {4041static class XServer extends Thread {42ServerSocket srv;43Socket s;44InputStream is;45OutputStream os;4647XServer (ServerSocket s) {48srv = s;49}5051Socket getSocket () {52return (s);53}5455public void run() {56try {57String x;58s = srv.accept ();59is = s.getInputStream ();60BufferedReader r = new BufferedReader(new InputStreamReader(is));61os = s.getOutputStream ();62BufferedWriter w = new BufferedWriter(new OutputStreamWriter(os));63w.write("HTTP/1.1 200 OK\r\n");64w.write("Content-Type: text/plain\r\n");65while ((x=r.readLine()).length() != 0) {66System.out.println("request: "+x);67if (!x.startsWith("GET")) {68w.write(x);69w.newLine();70}71}72w.newLine();73w.flush();74s.close ();75} catch (IOException e) { e.printStackTrace();76} finally {77try { srv.close(); } catch (IOException unused) {}78}79}80}8182public static void main(String[] args) throws Exception {83try {84InetAddress loopback = InetAddress.getLoopbackAddress();85ServerSocket serversocket = new ServerSocket();86serversocket.bind(new InetSocketAddress(loopback, 0));87int port = serversocket.getLocalPort();88XServer server = new XServer(serversocket);89server.start();90Thread.sleep(200);91URL url = URIBuilder.newBuilder()92.scheme("http")93.loopback()94.port(port)95.path("/index.html")96.toURL();97URLConnection uc = url.openConnection(NO_PROXY);9899// add request properties100uc.addRequestProperty("Cookie", "cookie1");101uc.addRequestProperty("Cookie", "cookie2");102uc.addRequestProperty("Cookie", "cookie3");103104Map e = uc.getRequestProperties();105106if (!((List)e.get("Cookie")).toString().equals("[cookie3, cookie2, cookie1]")) {107throw new RuntimeException("getRequestProperties fails");108}109110e = uc.getHeaderFields();111112if ((e.get("Content-Type") == null) || (e.get(null) == null)) {113throw new RuntimeException("getHeaderFields fails");114}115116} catch (Exception e) {117e.printStackTrace();118throw e;119}120}121}122123124