Path: blob/master/test/jdk/java/net/URLConnection/RequestPropertyValues.java
41149 views
/*1* Copyright (c) 2002, 2013, 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 4644775 623083626* @summary Test URLConnection Request Proterties27*/2829import java.net.MalformedURLException;30import java.net.URL;31import java.net.URLConnection;32import java.util.ArrayList;33import java.util.List;3435/**36* Part1:37* bug 4644775: Unexpected NPE in setRequestProperty(key, null) call38* Part2:39* bug 6230836: A few methods of class URLConnection implemented incorrectly40*/4142public class RequestPropertyValues {4344public static void main(String[] args) throws Exception {45part1();46part2();47}4849public static void part1() throws Exception {50List<URL> urls = new ArrayList<>();51urls.add(new URL("http://localhost:8088"));52urls.add(new URL("file:/etc/passwd"));53urls.add(new URL("jar:http://foo.com/bar.html!/foo/bar"));54if (hasFtp())55urls.add(new URL("ftp://foo:[email protected]/etc/passwd"));5657boolean failed = false;5859for (URL url : urls) {60URLConnection uc = url.openConnection();61try {62uc.setRequestProperty("TestHeader", null);63} catch (NullPointerException npe) {64System.out.println("setRequestProperty is throwing NPE" +65" for url: " + url);66failed = true;67}68try {69uc.addRequestProperty("TestHeader", null);70} catch (NullPointerException npe) {71System.out.println("addRequestProperty is throwing NPE" +72" for url: " + url);73failed = true;74}75}76if (failed) {77throw new Exception("RequestProperty setting/adding is" +78" throwing NPE for null values");79}80}8182public static void part2() throws Exception {83URL url = null;84String[] goodKeys = {"", "$", "key", "Key", " ", " "};85String[] goodValues = {"", "$", "value", "Value", " ", " "};8687URLConnection conn = getConnection(url);8889for (int i = 0; i < goodKeys.length; ++i) {90for (int j = 0; j < goodValues.length; ++j) {91// If a property with the key already exists, overwrite its value with the new value92conn.setRequestProperty(goodKeys[i], goodValues[j]);93String value = conn.getRequestProperty(goodKeys[i]);9495if (!((goodValues[j] == null && value == null) || (value != null && value.equals(goodValues[j]))))96throw new RuntimeException("Method setRequestProperty(String,String) works incorrectly");97}98}99}100101static URLConnection getConnection(URL url) {102return new DummyURLConnection(url);103}104105static class DummyURLConnection extends URLConnection {106107DummyURLConnection(URL url) {108super(url);109}110111public void connect() {112connected = true;113}114}115116private static boolean hasFtp() {117try {118return new java.net.URL("ftp://") != null;119} catch (java.net.MalformedURLException x) {120System.out.println("FTP not supported by this runtime.");121return false;122}123}124}125126127