Path: blob/master/test/jdk/sun/net/www/protocol/file/GetHeaderFields.java
41159 views
/*1* Copyright (c) 2020, 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 824138926* @summary URLConnection::getHeaderFields returns result inconsistent with getHeaderField/Key for FileURLConnection, FtpURLConnection27*/2829import java.net.*;30import java.io.*;31import java.util.*;3233public class GetHeaderFields {3435public static void main(String[] args) {36HashMap<String,List<String>> map1 = new HashMap<>();37Map<String,List<String>> headers = null;3839try {40File f = File.createTempFile("test", null);41f.deleteOnExit();42String s = f.getAbsolutePath();43s = s.startsWith("/") ? s : "/" + s;44URL url = new URL("file://localhost"+s);45URLConnection urlc = url.openConnection();46InputStream in = urlc.getInputStream();47headers = urlc.getHeaderFields();48Set<String> keys = headers.keySet();4950int i = 0;51while (true) {52String key = urlc.getHeaderFieldKey(i);53if (key == null)54break;55String val = urlc.getHeaderField(i);56putValue(map1,key,val);57i++;58}59System.out.println("headers = " + headers.toString());60System.out.println("map1 = " + map1.toString());61in.close();62} catch (java.io.FileNotFoundException fnfe) {63throw new RuntimeException("failed to recognize localhost");64} catch (Exception ex) {65throw new RuntimeException("Unexpected exception: " + ex);66}67if (!map1.equals(headers)) {68throw new RuntimeException("headers != map1");69}7071// check connection to non-existent resource returns72// an empty map7374URLConnection urlc = null;7576try {77URL url = new URL("file:///dev/non/existing/ABCD/123/foobar/sqisjjlk");78urlc = url.openConnection();79urlc.getInputStream();80} catch (IOException e) {}8182headers = urlc.getHeaderFields();83if (headers == null) {84throw new RuntimeException("expected empty map");85}86if (!headers.isEmpty()) {87throw new RuntimeException("expected empty map");88}8990}9192static void putValue(HashMap<String,List<String>> map, String key, String val) {93List<String> l = map.get(key);94if (l == null) {95l = new LinkedList<String>();96map.put(key, l);97}98l.add(val);99}100}101102103