test/jdk/java/net/httpclient/ConnectExceptionTest.java
author jboes
Fri, 08 Nov 2019 11:15:16 +0000
changeset 59029 3786a0962570
parent 50681 4254bed3c09d
permissions -rw-r--r--
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException Summary: Change implementation to use iterator instead of plain LinkedList Reviewed-by: dfuchs, vtewari
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     1
/*
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     4
 *
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     8
 *
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    13
 * accompanied this code).
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    14
 *
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    18
 *
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    21
 * questions.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    22
 */
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    23
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    24
/*
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    25
 * @test
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    26
 * @summary Expect ConnectException for all non-security related connect errors
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    27
 * @bug 8204864
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    28
 * @run testng/othervm ConnectExceptionTest
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    29
 * @run testng/othervm/java.security.policy=noPermissions.policy ConnectExceptionTest
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    30
 */
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    31
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    32
import java.io.IOException;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    33
import java.net.ConnectException;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    34
import java.net.InetSocketAddress;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    35
import java.net.Proxy;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    36
import java.net.ProxySelector;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    37
import java.net.SocketAddress;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    38
import java.net.URI;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    39
import java.net.http.HttpClient;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    40
import java.net.http.HttpRequest;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    41
import java.net.http.HttpRequest.BodyPublishers;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    42
import java.net.http.HttpResponse;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    43
import java.net.http.HttpResponse.BodyHandlers;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    44
import java.util.List;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    45
import java.util.concurrent.ExecutionException;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    46
import org.testng.annotations.DataProvider;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    47
import org.testng.annotations.Test;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    48
import static java.lang.System.out;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    49
import static org.testng.Assert.assertTrue;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    50
import static org.testng.Assert.fail;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    51
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    52
public class ConnectExceptionTest {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    53
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    54
    static final ProxySelector INVALID_PROXY = new ProxySelector() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    55
        final List<Proxy> proxy = List.of(new Proxy(Proxy.Type.HTTP,
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    56
                InetSocketAddress.createUnresolved("proxy.invalid", 8080)));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    57
        @Override public List<Proxy> select(URI uri) { return proxy; }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    58
        @Override public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    59
        @Override public String toString() { return "INVALID_PROXY"; }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    60
    };
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    61
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    62
    static final ProxySelector NO_PROXY = new ProxySelector() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    63
        @Override public List<Proxy> select(URI uri) { return List.of(Proxy.NO_PROXY); }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    64
        @Override public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    65
        @Override public String toString() { return "NO_PROXY"; }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    66
    };
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    67
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    68
    @DataProvider(name = "uris")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    69
    public Object[][] uris() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    70
        return new Object[][]{
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    71
            { "http://test.invalid/",  NO_PROXY       },
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    72
            { "https://test.invalid/", NO_PROXY       },
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    73
            { "http://test.invalid/",  INVALID_PROXY  },
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    74
            { "https://test.invalid/", INVALID_PROXY  },
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    75
        };
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    76
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    77
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    78
    @Test(dataProvider = "uris")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    79
    void testSynchronousGET(String uriString, ProxySelector proxy) throws Exception {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    80
        out.printf("%n---%ntestSynchronousGET starting uri:%s, proxy:%s%n", uriString, proxy);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    81
        HttpClient client = HttpClient.newBuilder().proxy(proxy).build();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    82
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    83
        URI uri = URI.create(uriString);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    84
        HttpRequest request = HttpRequest.newBuilder(uri).build();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    85
        try {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    86
            HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    87
            fail("UNEXPECTED response: " + response + ", body:" + response.body());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    88
        } catch (ConnectException ioe) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    89
            out.println("Caught expected: " + ioe);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    90
            //ioe.printStackTrace(out);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    91
        } catch (SecurityException expectedIfSMIsSet) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    92
            out.println("Caught expected: " + expectedIfSMIsSet);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    93
            assertTrue(System.getSecurityManager() != null);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    94
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    95
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    96
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    97
    @Test(dataProvider = "uris")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    98
    void testSynchronousPOST(String uriString, ProxySelector proxy) throws Exception {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    99
        out.printf("%n---%ntestSynchronousPOST starting uri:%s, proxy:%s%n", uriString, proxy);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   100
        HttpClient client = HttpClient.newBuilder().proxy(proxy).build();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   101
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   102
        URI uri = URI.create(uriString);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   103
        HttpRequest request = HttpRequest.newBuilder(uri)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   104
                .POST(BodyPublishers.ofString("Does not matter"))
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   105
                .build();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   106
        try {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   107
            HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   108
            fail("UNEXPECTED response: " + response + ", body:" + response.body());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   109
        } catch (ConnectException ioe) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   110
            out.println("Caught expected: " + ioe);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   111
            //ioe.printStackTrace(out);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   112
        } catch (SecurityException expectedIfSMIsSet) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   113
            out.println("Caught expected: " + expectedIfSMIsSet);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   114
            assertTrue(System.getSecurityManager() != null);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   115
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   116
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   117
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   118
    @Test(dataProvider = "uris")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   119
    void testAsynchronousGET(String uriString, ProxySelector proxy) throws Exception {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   120
        out.printf("%n---%ntestAsynchronousGET starting uri:%s, proxy:%s%n", uriString, proxy);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   121
        HttpClient client = HttpClient.newBuilder().proxy(proxy).build();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   122
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   123
        URI uri = URI.create(uriString);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   124
        HttpRequest request = HttpRequest.newBuilder(uri).build();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   125
        try {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   126
            HttpResponse<String> response = client.sendAsync(request, BodyHandlers.ofString()).get();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   127
            fail("UNEXPECTED response: " + response + ", body:" + response.body());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   128
        } catch (ExecutionException ee) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   129
            Throwable t = ee.getCause();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   130
            if (t instanceof ConnectException) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   131
                out.println("Caught expected: " + t);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   132
            } else if (t instanceof SecurityException) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   133
                out.println("Caught expected: " + t);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   134
                assertTrue(System.getSecurityManager() != null);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   135
            } else {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   136
                t.printStackTrace(out);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   137
                fail("Unexpected exception: " + t);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   138
            }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   139
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   140
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   141
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   142
    @Test(dataProvider = "uris")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   143
    void testAsynchronousPOST(String uriString, ProxySelector proxy) throws Exception {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   144
        out.printf("%n---%ntestAsynchronousPOST starting uri:%s, proxy:%s%n", uriString, proxy);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   145
        HttpClient client = HttpClient.newBuilder().proxy(proxy).build();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   146
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   147
        URI uri = URI.create(uriString);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   148
        HttpRequest request = HttpRequest.newBuilder(uri)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   149
                .POST(BodyPublishers.ofString("Does not matter"))
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   150
                .build();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   151
        try {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   152
            HttpResponse<String> response = client.sendAsync(request, BodyHandlers.ofString()).get();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   153
            fail("UNEXPECTED response: " + response + ", body:" + response.body());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   154
        } catch (ExecutionException ee) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   155
            Throwable t = ee.getCause();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   156
            if (t instanceof ConnectException) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   157
                out.println("Caught expected: " + t);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   158
            } else if (t instanceof SecurityException) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   159
                out.println("Caught expected: " + t);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   160
                assertTrue(System.getSecurityManager() != null);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   161
            } else {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   162
                t.printStackTrace(out);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   163
                fail("Unexpected exception: " + t);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   164
            }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   165
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   166
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   167
}