Path: blob/master/test/jdk/sun/net/www/http/HttpClient/IsAvailable.java
41154 views
/*1* Copyright (c) 2013, 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 800965026* @summary HttpClient available() check throws SocketException when connection27* has been closed28* @modules java.base/sun.net29* java.base/sun.net.www.http:+open30* @library /test/lib31*/3233import java.net.InetAddress;34import java.net.InetSocketAddress;35import java.net.URL;36import java.net.ServerSocket;37import sun.net.www.http.HttpClient;38import java.security.*;39import java.lang.reflect.Method;40import jdk.test.lib.net.URIBuilder;4142public class IsAvailable {4344public static void main(String[] args) throws Exception {45int readTimeout = 20;46ServerSocket ss = new ServerSocket();47InetAddress loopback = InetAddress.getLoopbackAddress();48ss.bind(new InetSocketAddress(loopback, 0));4950try (ServerSocket toclose = ss) {5152URL url1 = URIBuilder.newBuilder()53.scheme("http")54.loopback()55.port(ss.getLocalPort())56.toURL();5758HttpClient c1 = HttpClient.New(url1);5960Method available = HttpClient.class.61getDeclaredMethod("available", null);62available.setAccessible(true);6364c1.setReadTimeout(readTimeout);65boolean a = (boolean) available.invoke(c1);66if (!a) {67throw new RuntimeException("connection should be available");68}69if (c1.getReadTimeout() != readTimeout) {70throw new RuntimeException("read timeout has been altered");71}7273c1.closeServer();7475a = (boolean) available.invoke(c1);76if (a) {77throw new RuntimeException("connection shouldn't be available");78}79}80}81}828384