Path: blob/master/test/jdk/java/net/httpclient/ConnectExceptionTest.java
41152 views
/*1* Copyright (c) 2018, 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* @summary Expect ConnectException for all non-security related connect errors26* @bug 820486427* @run testng/othervm ConnectExceptionTest28* @run testng/othervm/java.security.policy=noPermissions.policy ConnectExceptionTest29*/3031import java.io.IOException;32import java.net.ConnectException;33import java.net.InetSocketAddress;34import java.net.Proxy;35import java.net.ProxySelector;36import java.net.SocketAddress;37import java.net.URI;38import java.net.http.HttpClient;39import java.net.http.HttpRequest;40import java.net.http.HttpRequest.BodyPublishers;41import java.net.http.HttpResponse;42import java.net.http.HttpResponse.BodyHandlers;43import java.util.List;44import java.util.concurrent.ExecutionException;45import org.testng.annotations.DataProvider;46import org.testng.annotations.Test;47import static java.lang.System.out;48import static org.testng.Assert.assertTrue;49import static org.testng.Assert.fail;5051public class ConnectExceptionTest {5253static final ProxySelector INVALID_PROXY = new ProxySelector() {54final List<Proxy> proxy = List.of(new Proxy(Proxy.Type.HTTP,55InetSocketAddress.createUnresolved("proxy.invalid", 8080)));56@Override public List<Proxy> select(URI uri) { return proxy; }57@Override public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { }58@Override public String toString() { return "INVALID_PROXY"; }59};6061static final ProxySelector NO_PROXY = new ProxySelector() {62@Override public List<Proxy> select(URI uri) { return List.of(Proxy.NO_PROXY); }63@Override public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { }64@Override public String toString() { return "NO_PROXY"; }65};6667@DataProvider(name = "uris")68public Object[][] uris() {69return new Object[][]{70{ "http://test.invalid/", NO_PROXY },71{ "https://test.invalid/", NO_PROXY },72{ "http://test.invalid/", INVALID_PROXY },73{ "https://test.invalid/", INVALID_PROXY },74};75}7677@Test(dataProvider = "uris")78void testSynchronousGET(String uriString, ProxySelector proxy) throws Exception {79out.printf("%n---%ntestSynchronousGET starting uri:%s, proxy:%s%n", uriString, proxy);80HttpClient client = HttpClient.newBuilder().proxy(proxy).build();8182URI uri = URI.create(uriString);83HttpRequest request = HttpRequest.newBuilder(uri).build();84try {85HttpResponse<String> response = client.send(request, BodyHandlers.ofString());86fail("UNEXPECTED response: " + response + ", body:" + response.body());87} catch (ConnectException ioe) {88out.println("Caught expected: " + ioe);89//ioe.printStackTrace(out);90} catch (SecurityException expectedIfSMIsSet) {91out.println("Caught expected: " + expectedIfSMIsSet);92assertTrue(System.getSecurityManager() != null);93}94}9596@Test(dataProvider = "uris")97void testSynchronousPOST(String uriString, ProxySelector proxy) throws Exception {98out.printf("%n---%ntestSynchronousPOST starting uri:%s, proxy:%s%n", uriString, proxy);99HttpClient client = HttpClient.newBuilder().proxy(proxy).build();100101URI uri = URI.create(uriString);102HttpRequest request = HttpRequest.newBuilder(uri)103.POST(BodyPublishers.ofString("Does not matter"))104.build();105try {106HttpResponse<String> response = client.send(request, BodyHandlers.ofString());107fail("UNEXPECTED response: " + response + ", body:" + response.body());108} catch (ConnectException ioe) {109out.println("Caught expected: " + ioe);110//ioe.printStackTrace(out);111} catch (SecurityException expectedIfSMIsSet) {112out.println("Caught expected: " + expectedIfSMIsSet);113assertTrue(System.getSecurityManager() != null);114}115}116117@Test(dataProvider = "uris")118void testAsynchronousGET(String uriString, ProxySelector proxy) throws Exception {119out.printf("%n---%ntestAsynchronousGET starting uri:%s, proxy:%s%n", uriString, proxy);120HttpClient client = HttpClient.newBuilder().proxy(proxy).build();121122URI uri = URI.create(uriString);123HttpRequest request = HttpRequest.newBuilder(uri).build();124try {125HttpResponse<String> response = client.sendAsync(request, BodyHandlers.ofString()).get();126fail("UNEXPECTED response: " + response + ", body:" + response.body());127} catch (ExecutionException ee) {128Throwable t = ee.getCause();129if (t instanceof ConnectException) {130out.println("Caught expected: " + t);131} else if (t instanceof SecurityException) {132out.println("Caught expected: " + t);133assertTrue(System.getSecurityManager() != null);134} else {135t.printStackTrace(out);136fail("Unexpected exception: " + t);137}138}139}140141@Test(dataProvider = "uris")142void testAsynchronousPOST(String uriString, ProxySelector proxy) throws Exception {143out.printf("%n---%ntestAsynchronousPOST starting uri:%s, proxy:%s%n", uriString, proxy);144HttpClient client = HttpClient.newBuilder().proxy(proxy).build();145146URI uri = URI.create(uriString);147HttpRequest request = HttpRequest.newBuilder(uri)148.POST(BodyPublishers.ofString("Does not matter"))149.build();150try {151HttpResponse<String> response = client.sendAsync(request, BodyHandlers.ofString()).get();152fail("UNEXPECTED response: " + response + ", body:" + response.body());153} catch (ExecutionException ee) {154Throwable t = ee.getCause();155if (t instanceof ConnectException) {156out.println("Caught expected: " + t);157} else if (t instanceof SecurityException) {158out.println("Caught expected: " + t);159assertTrue(System.getSecurityManager() != null);160} else {161t.printStackTrace(out);162fail("Unexpected exception: " + t);163}164}165}166}167168169