duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2001-2002 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 4143541 4147035 4244362 |
| 27 | * @summary URLConnection cannot enumerate request properties, |
| 28 | * and URLConnection can neither get nor set multiple |
| 29 | * request properties w/ same key |
| 30 | * |
| 31 | */ |
| 32 | |
| 33 | import java.net.*; |
| 34 | import java.util.*; |
| 35 | import java.io.*; |
| 36 | |
| 37 | public class URLConnectionHeaders { |
| 38 | |
| 39 | static class XServer extends Thread { |
| 40 | ServerSocket srv; |
| 41 | Socket s; |
| 42 | InputStream is; |
| 43 | OutputStream os; |
| 44 | |
| 45 | XServer (ServerSocket s) { |
| 46 | srv = s; |
| 47 | } |
| 48 | |
| 49 | Socket getSocket () { |
| 50 | return (s); |
| 51 | } |
| 52 | |
| 53 | public void run() { |
| 54 | try { |
| 55 | String x; |
| 56 | s = srv.accept (); |
| 57 | is = s.getInputStream (); |
| 58 | BufferedReader r = new BufferedReader(new InputStreamReader(is)); |
| 59 | os = s.getOutputStream (); |
| 60 | BufferedWriter w = new BufferedWriter(new OutputStreamWriter(os)); |
| 61 | w.write("HTTP/1.1 200 OK\r\n"); |
| 62 | w.write("Content-Type: text/plain\r\n"); |
| 63 | while ((x=r.readLine()).length() != 0) { |
| 64 | System.out.println("request: "+x); |
| 65 | if (!x.startsWith("GET")) { |
| 66 | w.write(x); |
| 67 | w.newLine(); |
| 68 | } |
| 69 | } |
| 70 | w.newLine(); |
| 71 | w.flush(); |
| 72 | s.close (); |
| 73 | srv.close (); // or else the HTTPURLConnection will retry |
| 74 | } catch (IOException e) { e.printStackTrace();} |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | public static void main(String[] args) { |
| 79 | try { |
| 80 | ServerSocket serversocket = new ServerSocket (0); |
| 81 | int port = serversocket.getLocalPort (); |
| 82 | XServer server = new XServer (serversocket); |
| 83 | server.start (); |
| 84 | Thread.sleep (200); |
| 85 | URL url = new URL ("http://localhost:"+port+"/index.html"); |
| 86 | URLConnection uc = url.openConnection (); |
| 87 | |
| 88 | // add request properties |
| 89 | uc.addRequestProperty("Cookie", "cookie1"); |
| 90 | uc.addRequestProperty("Cookie", "cookie2"); |
| 91 | uc.addRequestProperty("Cookie", "cookie3"); |
| 92 | |
| 93 | Map e = uc.getRequestProperties(); |
| 94 | |
| 95 | if (!((List)e.get("Cookie")).toString().equals("[cookie3, cookie2, cookie1]")) { |
| 96 | throw new RuntimeException("getRequestProperties fails"); |
| 97 | } |
| 98 | |
| 99 | e = uc.getHeaderFields(); |
| 100 | |
| 101 | if ((e.get("Content-Type") == null) || (e.get(null) == null)) { |
| 102 | throw new RuntimeException("getHeaderFields fails"); |
| 103 | } |
| 104 | |
| 105 | } catch (Exception e) { |
| 106 | e.printStackTrace(); |
| 107 | } |
| 108 | } |
| 109 | } |