jdk/test/java/net/httpclient/APIErrors.java
changeset 36131 379db4b2f95d
child 37720 45cd7cc65382
equal deleted inserted replaced
36130:5423259ef9ea 36131:379db4b2f95d
       
     1 /*
       
     2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /**
       
    25  * @test
       
    26  * @bug 8087112
       
    27  * @library /lib/testlibrary/
       
    28  * @build jdk.testlibrary.SimpleSSLContext ProxyServer
       
    29  * @compile ../../../com/sun/net/httpserver/LogFilter.java
       
    30  * @compile ../../../com/sun/net/httpserver/FileServerHandler.java
       
    31  * @run main/othervm APIErrors
       
    32  */
       
    33 //package javaapplication16;
       
    34 
       
    35 import com.sun.net.httpserver.*;
       
    36 import java.io.IOException;
       
    37 import java.net.*;
       
    38 import java.net.http.*;
       
    39 import java.util.LinkedList;
       
    40 import java.util.List;
       
    41 import java.util.concurrent.*;
       
    42 import java.util.function.Supplier;
       
    43 
       
    44 /**
       
    45  * Does stupid things with API, to check appropriate errors/exceptions thrown
       
    46  */
       
    47 public class APIErrors {
       
    48 
       
    49     static HttpServer s1 = null;
       
    50     static ExecutorService executor = null;
       
    51     static int port;
       
    52     static HttpClient client;
       
    53     static String httproot, fileuri, fileroot;
       
    54     static List<HttpClient> clients = new LinkedList<>();
       
    55 
       
    56     public static void main(String[] args) throws Exception {
       
    57         initServer();
       
    58         fileroot = System.getProperty("test.src") + "/docs";
       
    59 
       
    60         client = HttpClient.create().build();
       
    61 
       
    62         clients.add(HttpClient.getDefault());
       
    63 
       
    64         try {
       
    65             test1();
       
    66             test2();
       
    67             test3();
       
    68         } finally {
       
    69             s1.stop(0);
       
    70             executor.shutdownNow();
       
    71             for (HttpClient client : clients)
       
    72                 client.executorService().shutdownNow();
       
    73         }
       
    74     }
       
    75 
       
    76     static void reject(Runnable r, Class<? extends Exception> extype) {
       
    77         try {
       
    78             r.run();
       
    79             throw new RuntimeException("Expected: " + extype);
       
    80         } catch (Throwable t) {
       
    81             if (!extype.isAssignableFrom(t.getClass())) {
       
    82                 throw new RuntimeException("Wrong exception type: " + extype + " / "
       
    83                     +t.getClass());
       
    84             }
       
    85         }
       
    86     }
       
    87 
       
    88     static void accept(Runnable r) {
       
    89         try {
       
    90             r.run();
       
    91         } catch (Throwable t) {
       
    92             throw new RuntimeException("Unexpected exception: " + t);
       
    93         }
       
    94     }
       
    95 
       
    96     static void checkNonNull(Supplier<?> r) {
       
    97         if (r.get() == null)
       
    98             throw new RuntimeException("Unexpected null return:");
       
    99     }
       
   100 
       
   101     static void assertTrue(Supplier<Boolean> r) {
       
   102         if (r.get() == false)
       
   103             throw new RuntimeException("Assertion failure:");
       
   104     }
       
   105 
       
   106     // HttpClient.Builder
       
   107     static void test1() throws Exception {
       
   108         System.out.println("Test 1");
       
   109         HttpClient.Builder cb = HttpClient.create();
       
   110         InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 5000);
       
   111         reject(() -> { cb.priority(-1);}, IllegalArgumentException.class);
       
   112         reject(() -> { cb.priority(500);}, IllegalArgumentException.class);
       
   113         accept(() -> { cb.priority(1);});
       
   114         accept(() -> { cb.priority(255);});
       
   115 
       
   116         accept(() -> {clients.add(cb.build()); clients.add(cb.build());});
       
   117     }
       
   118 
       
   119     static void test2() throws Exception {
       
   120         System.out.println("Test 2");
       
   121         HttpClient.Builder cb = HttpClient.create();
       
   122         InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 5000);
       
   123         cb.proxy(ProxySelector.of(addr));
       
   124         HttpClient c = cb.build();
       
   125         clients.add(c);
       
   126         checkNonNull(()-> {return c.executorService();});
       
   127         assertTrue(()-> {return c.followRedirects() == HttpClient.Redirect.NEVER;});
       
   128         assertTrue(()-> {return !c.authenticator().isPresent();});
       
   129     }
       
   130 
       
   131     static URI accessibleURI() {
       
   132         return URI.create(fileuri);
       
   133     }
       
   134 
       
   135     static HttpRequest request() {
       
   136         return HttpRequest.create(accessibleURI())
       
   137                 .GET();
       
   138     }
       
   139 
       
   140     static void test3() throws Exception {
       
   141         System.out.println("Test 3");
       
   142         reject(()-> {
       
   143             try {
       
   144                 HttpRequest r1 = request();
       
   145                 HttpResponse resp = r1.response();
       
   146                 HttpResponse resp1 = r1.response();
       
   147             } catch (IOException |InterruptedException e) {
       
   148                 throw new RuntimeException(e);
       
   149             }
       
   150         }, IllegalStateException.class);
       
   151 
       
   152         reject(()-> {
       
   153             try {
       
   154                 HttpRequest r1 = request();
       
   155                 HttpResponse resp = r1.response();
       
   156                 HttpResponse resp1 = r1.responseAsync().get();
       
   157             } catch (IOException |InterruptedException | ExecutionException e) {
       
   158                 throw new RuntimeException(e);
       
   159             }
       
   160         }, IllegalStateException.class);
       
   161         reject(()-> {
       
   162             try {
       
   163                 HttpRequest r1 = request();
       
   164                 HttpResponse resp1 = r1.responseAsync().get();
       
   165                 HttpResponse resp = r1.response();
       
   166             } catch (IOException |InterruptedException | ExecutionException e) {
       
   167                 throw new RuntimeException(e);
       
   168             }
       
   169         }, IllegalStateException.class);
       
   170     }
       
   171 
       
   172     static class Auth extends java.net.Authenticator {
       
   173         int count = 0;
       
   174         @Override
       
   175         protected PasswordAuthentication getPasswordAuthentication() {
       
   176             if (count++ == 0) {
       
   177                 return new PasswordAuthentication("user", "passwd".toCharArray());
       
   178             } else {
       
   179                 return new PasswordAuthentication("user", "goober".toCharArray());
       
   180             }
       
   181         }
       
   182         int count() {
       
   183             return count;
       
   184         }
       
   185     }
       
   186 
       
   187     public static void initServer() throws Exception {
       
   188         String root = System.getProperty ("test.src")+ "/docs";
       
   189         InetSocketAddress addr = new InetSocketAddress (0);
       
   190         s1 = HttpServer.create (addr, 0);
       
   191         if (s1 instanceof HttpsServer) {
       
   192             throw new RuntimeException ("should not be httpsserver");
       
   193         }
       
   194         HttpHandler h = new FileServerHandler(root);
       
   195 
       
   196         HttpContext c1 = s1.createContext("/files", h);
       
   197 
       
   198         executor = Executors.newCachedThreadPool();
       
   199         s1.setExecutor (executor);
       
   200         s1.start();
       
   201 
       
   202         port = s1.getAddress().getPort();
       
   203         System.out.println("HTTP server port = " + port);
       
   204         httproot = "http://127.0.0.1:" + port + "/files/";
       
   205         fileuri = httproot + "foo.txt";
       
   206     }
       
   207 }