test/jdk/java/net/httpclient/HttpRequestBuilderTest.java
branchhttp-client-branch
changeset 55763 634d8e14c172
parent 47216 71c04702a3d5
child 55833 3e1f7cdef8a5
equal deleted inserted replaced
55762:e947a3a50a95 55763:634d8e14c172
     1 /*
     1 /*
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    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
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 import jdk.incubator.http.HttpRequest;
       
    25 import java.net.URI;
    24 import java.net.URI;
    26 import jdk.incubator.http.HttpClient;
    25 import jdk.incubator.http.HttpClient;
    27 import java.time.Duration;
    26 import java.time.Duration;
       
    27 import java.util.Arrays;
    28 import java.util.function.BiFunction;
    28 import java.util.function.BiFunction;
    29 import java.util.function.Function;
    29 import java.util.function.Function;
       
    30 import java.util.function.Supplier;
    30 import java.util.stream.Collectors;
    31 import java.util.stream.Collectors;
    31 import java.util.stream.Stream;
    32 import java.util.stream.Stream;
       
    33 import jdk.incubator.http.HttpRequest;
       
    34 import static jdk.incubator.http.HttpRequest.BodyPublisher.fromString;
    32 
    35 
    33 /**
    36 /**
    34  * @test
    37  * @test
    35  * @bug 8170064
    38  * @bug 8170064
    36  * @summary  HttpRequest API documentation says:" Unless otherwise stated,
    39  * @summary  HttpRequest[.Builder] API and behaviour checks
    37  * {@code null} parameter values will cause methods
       
    38  * of this class to throw {@code NullPointerException}".
       
    39  */
    40  */
    40 public class HttpRequestBuilderTest {
    41 public class HttpRequestBuilderTest {
    41 
    42 
    42     static final URI TEST_URI = URI.create("http://www.foo.com/");
    43     static final URI TEST_URI = URI.create("http://www.foo.com/");
    43 
    44 
    44 
    45 
    45     public static void main(String[] args) throws Exception {
    46     public static void main(String[] args) throws Exception {
    46 
    47 
       
    48         test0("newBuilder().build()",
       
    49               () -> HttpRequest.newBuilder().build(),
       
    50               IllegalStateException.class);
       
    51 
       
    52         test0("newBuilder(null)",
       
    53               () -> HttpRequest.newBuilder(null),
       
    54               NullPointerException.class);
       
    55 
       
    56         test0("newBuilder(URI.create(\"badScheme://www.foo.com/\")",
       
    57               () -> HttpRequest.newBuilder(URI.create("badScheme://www.foo.com/")),
       
    58               IllegalArgumentException.class);
       
    59 
       
    60         test0("newBuilder(URI.create(\"http://www.foo.com:-1/\")",
       
    61                 () -> HttpRequest.newBuilder(URI.create("http://www.foo.com:-1/")),
       
    62                 IllegalArgumentException.class);
       
    63 
       
    64         test0("newBuilder(URI.create(\"https://www.foo.com:-1/\")",
       
    65                 () -> HttpRequest.newBuilder(URI.create("https://www.foo.com:-1/")),
       
    66                 IllegalArgumentException.class);
       
    67 
       
    68         test0("newBuilder(" + TEST_URI + ").uri(null)",
       
    69               () -> HttpRequest.newBuilder(TEST_URI).uri(null),
       
    70               NullPointerException.class);
       
    71 
       
    72         test0("newBuilder(uri).build()",
       
    73               () -> HttpRequest.newBuilder(TEST_URI).build()
       
    74               /* no expected exceptions */ );
       
    75 
    47         HttpRequest.Builder builder = HttpRequest.newBuilder();
    76         HttpRequest.Builder builder = HttpRequest.newBuilder();
       
    77 
    48         builder = test1("uri", builder, builder::uri, (URI)null,
    78         builder = test1("uri", builder, builder::uri, (URI)null,
    49                         NullPointerException.class);
    79                         NullPointerException.class);
       
    80 
       
    81         builder = test1("uri", builder, builder::uri, URI.create("http://www.foo.com:-1/"),
       
    82                         IllegalArgumentException.class);
       
    83 
       
    84         builder = test1("uri", builder, builder::uri, URI.create("https://www.foo.com:-1/"),
       
    85                         IllegalArgumentException.class);
       
    86 
    50         builder = test2("header", builder, builder::header, (String) null, "bar",
    87         builder = test2("header", builder, builder::header, (String) null, "bar",
    51                         NullPointerException.class);
    88                         NullPointerException.class);
       
    89 
    52         builder = test2("header", builder, builder::header, "foo", (String) null,
    90         builder = test2("header", builder, builder::header, "foo", (String) null,
    53                         NullPointerException.class);
    91                         NullPointerException.class);
       
    92 
    54         builder = test2("header", builder, builder::header, (String)null,
    93         builder = test2("header", builder, builder::header, (String)null,
    55                         (String) null, NullPointerException.class);
    94                         (String) null, NullPointerException.class);
       
    95 
       
    96         builder = test2("header", builder, builder::header, "", "bar",
       
    97                         IllegalArgumentException.class);
       
    98 
       
    99         builder = test2("header", builder, builder::header, "foo", "\r",
       
   100                         IllegalArgumentException.class);
       
   101 
    56         builder = test1("headers", builder, builder::headers, (String[]) null,
   102         builder = test1("headers", builder, builder::headers, (String[]) null,
    57                         NullPointerException.class);
   103                         NullPointerException.class);
       
   104 
       
   105         builder = test1("headers", builder, builder::headers, new String[0],
       
   106                         IllegalArgumentException.class);
       
   107 
    58         builder = test1("headers", builder, builder::headers,
   108         builder = test1("headers", builder, builder::headers,
    59                         (String[]) new String[] {null, "bar"},
   109                         (String[]) new String[] {null, "bar"},
    60                         NullPointerException.class);
   110                         NullPointerException.class);
       
   111 
    61         builder = test1("headers", builder, builder::headers,
   112         builder = test1("headers", builder, builder::headers,
    62                         (String[]) new String[] {"foo", null},
   113                         (String[]) new String[] {"foo", null},
    63                         NullPointerException.class);
   114                         NullPointerException.class);
       
   115 
    64         builder = test1("headers", builder, builder::headers,
   116         builder = test1("headers", builder, builder::headers,
    65                         (String[]) new String[] {null, null},
   117                         (String[]) new String[] {null, null},
    66                         NullPointerException.class);
   118                         NullPointerException.class);
    67         builder = test1("headers", builder, builder::headers,
   119 
    68                        (String[]) new String[] {"foo", "bar", null},
   120         builder = test1("headers", builder, builder::headers,
    69                        NullPointerException.class,
   121                         (String[]) new String[] {"foo", "bar", null},
    70                        IllegalArgumentException.class);
   122                         NullPointerException.class,
    71         builder = test1("headers", builder, builder::headers,
   123                         IllegalArgumentException.class);
    72                        (String[]) new String[] {"foo", "bar", null, null},
   124 
    73                        NullPointerException.class);
   125         builder = test1("headers", builder, builder::headers,
    74         builder = test1("headers", builder, builder::headers,
   126                         (String[]) new String[] {"foo", "bar", null, null},
    75                        (String[]) new String[] {"foo", "bar", "baz", null},
   127                         NullPointerException.class);
    76                        NullPointerException.class);
   128 
    77         builder = test1("headers", builder, builder::headers,
   129         builder = test1("headers", builder, builder::headers,
    78                        (String[]) new String[] {"foo", "bar", null, "baz"},
   130                         (String[]) new String[] {"foo", "bar", "baz", null},
    79                        NullPointerException.class);
   131                         NullPointerException.class);
    80         builder = test1("headers", builder, builder::headers,
   132 
    81                        (String[]) new String[] {"foo", "bar", "baz"},
   133         builder = test1("headers", builder, builder::headers,
    82                        IllegalArgumentException.class);
   134                         (String[]) new String[] {"foo", "bar", "\r", "baz"},
    83         builder = test1("headers", builder, builder::headers,
   135                         IllegalArgumentException.class);
    84                        (String[]) new String[] {"foo"},
   136 
    85                        IllegalArgumentException.class);
   137         builder = test1("headers", builder, builder::headers,
       
   138                         (String[]) new String[] {"foo", "bar", "baz", "\n"},
       
   139                         IllegalArgumentException.class);
       
   140 
       
   141         builder = test1("headers", builder, builder::headers,
       
   142                         (String[]) new String[] {"foo", "bar", "", "baz"},
       
   143                         IllegalArgumentException.class);
       
   144 
       
   145         builder = test1("headers", builder, builder::headers,
       
   146                         (String[]) new String[] {"foo", "bar", null, "baz"},
       
   147                         NullPointerException.class);
       
   148 
       
   149         builder = test1("headers", builder, builder::headers,
       
   150                         (String[]) new String[] {"foo", "bar", "baz"},
       
   151                         IllegalArgumentException.class);
       
   152 
       
   153         builder = test1("headers", builder, builder::headers,
       
   154                         (String[]) new String[] {"foo"},
       
   155                         IllegalArgumentException.class);
       
   156 
    86         builder = test1("DELETE", builder, builder::DELETE,
   157         builder = test1("DELETE", builder, builder::DELETE,
    87                         (HttpRequest.BodyProcessor)null, null);
   158                         HttpRequest.noBody(), null);
       
   159 
    88         builder = test1("POST", builder, builder::POST,
   160         builder = test1("POST", builder, builder::POST,
    89                         (HttpRequest.BodyProcessor)null, null);
   161                         HttpRequest.noBody(), null);
       
   162 
    90         builder = test1("PUT", builder, builder::PUT,
   163         builder = test1("PUT", builder, builder::PUT,
    91                         (HttpRequest.BodyProcessor)null, null);
   164                         HttpRequest.noBody(), null);
       
   165 
    92         builder = test2("method", builder, builder::method, "GET",
   166         builder = test2("method", builder, builder::method, "GET",
    93                         (HttpRequest.BodyProcessor) null, null);
   167                         HttpRequest.noBody(), null);
       
   168 
       
   169         builder = test1("DELETE", builder, builder::DELETE,
       
   170                         (HttpRequest.BodyPublisher)null,
       
   171                         NullPointerException.class);
       
   172 
       
   173         builder = test1("POST", builder, builder::POST,
       
   174                         (HttpRequest.BodyPublisher)null,
       
   175                         NullPointerException.class);
       
   176 
       
   177         builder = test1("PUT", builder, builder::PUT,
       
   178                         (HttpRequest.BodyPublisher)null,
       
   179                         NullPointerException.class);
       
   180 
       
   181         builder = test2("method", builder, builder::method, "GET",
       
   182                         (HttpRequest.BodyPublisher) null,
       
   183                         NullPointerException.class);
       
   184 
    94         builder = test2("setHeader", builder, builder::setHeader,
   185         builder = test2("setHeader", builder, builder::setHeader,
    95                         (String) null, "bar",
   186                         (String) null, "bar",
    96                         NullPointerException.class);
   187                         NullPointerException.class);
       
   188 
    97         builder = test2("setHeader", builder, builder::setHeader,
   189         builder = test2("setHeader", builder, builder::setHeader,
    98                         "foo", (String) null,
   190                         "foo", (String) null,
    99                         NullPointerException.class);
   191                         NullPointerException.class);
       
   192 
   100         builder = test2("setHeader", builder, builder::setHeader,
   193         builder = test2("setHeader", builder, builder::setHeader,
   101                         (String)null, (String) null,
   194                         (String)null, (String) null,
   102                         NullPointerException.class);
   195                         NullPointerException.class);
       
   196 
   103         builder = test1("timeout", builder, builder::timeout,
   197         builder = test1("timeout", builder, builder::timeout,
   104                         (Duration)null, NullPointerException.class);
   198                         (Duration)null,
       
   199                         NullPointerException.class);
       
   200 
   105         builder = test1("version", builder, builder::version,
   201         builder = test1("version", builder, builder::version,
   106                         (HttpClient.Version)null,
   202                         (HttpClient.Version)null,
   107                         NullPointerException.class);
   203                         NullPointerException.class);
       
   204 
   108         builder = test2("method", builder, builder::method, null,
   205         builder = test2("method", builder, builder::method, null,
   109                        HttpRequest.BodyProcessor.fromString("foo"),
   206                         HttpRequest.BodyPublisher.fromString("foo"),
   110                        NullPointerException.class);
   207                         NullPointerException.class);
   111 // see JDK-8170093
   208 // see JDK-8170093
   112 //
   209 //
   113 //        builder = test2("method", builder, builder::method, "foo",
   210 //        builder = test2("method", builder, builder::method, "foo",
   114 //                       HttpRequest.BodyProcessor.fromString("foo"),
   211 //                       HttpRequest.BodyProcessor.fromString("foo"),
   115 //                       IllegalArgumentException.class);
   212 //                       IllegalArgumentException.class);
   116 //
   213 //
   117 //        builder.build();
   214 //        builder.build();
   118 
   215 
       
   216 
       
   217         method("newBuilder(TEST_URI).build().method() == GET",
       
   218                () -> HttpRequest.newBuilder(TEST_URI),
       
   219                "GET");
       
   220 
       
   221         method("newBuilder(TEST_URI).GET().build().method() == GET",
       
   222                () -> HttpRequest.newBuilder(TEST_URI).GET(),
       
   223                "GET");
       
   224 
       
   225         method("newBuilder(TEST_URI).POST(fromString(\"\")).GET().build().method() == GET",
       
   226                () -> HttpRequest.newBuilder(TEST_URI).POST(fromString("")).GET(),
       
   227                "GET");
       
   228 
       
   229         method("newBuilder(TEST_URI).PUT(fromString(\"\")).GET().build().method() == GET",
       
   230                () -> HttpRequest.newBuilder(TEST_URI).PUT(fromString("")).GET(),
       
   231                "GET");
       
   232 
       
   233         method("newBuilder(TEST_URI).DELETE(fromString(\"\")).GET().build().method() == GET",
       
   234                () -> HttpRequest.newBuilder(TEST_URI).DELETE(fromString("")).GET(),
       
   235                "GET");
       
   236 
       
   237         method("newBuilder(TEST_URI).POST(fromString(\"\")).build().method() == POST",
       
   238                () -> HttpRequest.newBuilder(TEST_URI).POST(fromString("")),
       
   239                "POST");
       
   240 
       
   241         method("newBuilder(TEST_URI).PUT(fromString(\"\")).build().method() == PUT",
       
   242                () -> HttpRequest.newBuilder(TEST_URI).PUT(fromString("")),
       
   243                "PUT");
       
   244 
       
   245         method("newBuilder(TEST_URI).DELETE(fromString(\"\")).build().method() == DELETE",
       
   246                () -> HttpRequest.newBuilder(TEST_URI).DELETE(fromString("")),
       
   247                "DELETE");
       
   248 
       
   249         method("newBuilder(TEST_URI).GET().POST(fromString(\"\")).build().method() == POST",
       
   250                () -> HttpRequest.newBuilder(TEST_URI).GET().POST(fromString("")),
       
   251                "POST");
       
   252 
       
   253         method("newBuilder(TEST_URI).GET().PUT(fromString(\"\")).build().method() == PUT",
       
   254                () -> HttpRequest.newBuilder(TEST_URI).GET().PUT(fromString("")),
       
   255                "PUT");
       
   256 
       
   257         method("newBuilder(TEST_URI).GET().DELETE(fromString(\"\")).build().method() == DELETE",
       
   258                () -> HttpRequest.newBuilder(TEST_URI).GET().DELETE(fromString("")),
       
   259                "DELETE");
       
   260 
       
   261 
       
   262 
   119     }
   263     }
   120 
   264 
   121     private static boolean shouldFail(Class<? extends Exception> ...exceptions) {
   265     private static boolean shouldFail(Class<? extends Exception> ...exceptions) {
   122         return exceptions != null && exceptions.length > 0;
   266         return exceptions != null && exceptions.length > 0;
   123     }
   267     }
   131         return expected != null && Stream.of(expected)
   275         return expected != null && Stream.of(expected)
   132                 .filter(c -> c.isInstance(x))
   276                 .filter(c -> c.isInstance(x))
   133                 .findAny().isPresent();
   277                 .findAny().isPresent();
   134     }
   278     }
   135 
   279 
       
   280     static void method(String name,
       
   281                        Supplier<HttpRequest.Builder> supplier,
       
   282                        String expectedMethod) {
       
   283         HttpRequest request = supplier.get().build();
       
   284         String method = request.method();
       
   285         if (request.method().equals("GET") && request.bodyPublisher().isPresent())
       
   286             throw new AssertionError("failed: " + name
       
   287                     + ". Unexpected body processor for GET: "
       
   288                     + request.bodyPublisher().get());
       
   289 
       
   290         if (expectedMethod.equals(method)) {
       
   291             System.out.println("success: " + name);
       
   292         } else {
       
   293             throw new AssertionError("failed: " + name
       
   294                     + ". Expected " + expectedMethod + ", got " + method);
       
   295         }
       
   296     }
       
   297 
       
   298     static void test0(String name,
       
   299                       Runnable r,
       
   300                       Class<? extends Exception> ...ex) {
       
   301         try {
       
   302             r.run();
       
   303             if (!shouldFail(ex)) {
       
   304                 System.out.println("success: " + name);
       
   305                 return;
       
   306             } else {
       
   307                 throw new AssertionError("Expected " + expectedNames(ex)
       
   308                         + " not raised for " + name);
       
   309             }
       
   310         } catch (Exception x) {
       
   311             if (!isExpected(x, ex)) {
       
   312                 throw x;
       
   313             } else {
       
   314                 System.out.println("success: " + name +
       
   315                         " - Got expected exception: " + x);
       
   316             }
       
   317         }
       
   318     }
       
   319 
   136     public static <R,P> R test1(String name, R receiver, Function<P, R> m, P arg,
   320     public static <R,P> R test1(String name, R receiver, Function<P, R> m, P arg,
   137                                Class<? extends Exception> ...ex) {
   321                                Class<? extends Exception> ...ex) {
       
   322         String argMessage = arg == null ? "null" : arg.toString();
       
   323         if (arg instanceof String[]) {
       
   324             argMessage = Arrays.asList((String[])arg).toString();
       
   325         }
   138         try {
   326         try {
   139             R result =  m.apply(arg);
   327             R result =  m.apply(arg);
   140             if (!shouldFail(ex)) {
   328             if (!shouldFail(ex)) {
   141                 System.out.println("success: " + name + "(" + arg + ")");
   329                 System.out.println("success: " + name + "(" + argMessage + ")");
   142                 return result;
   330                 return result;
   143             } else {
   331             } else {
   144                 throw new AssertionError("Expected " + expectedNames(ex)
   332                 throw new AssertionError("Expected " + expectedNames(ex)
   145                     + " not raised for " + name + "(" + arg + ")");
   333                     + " not raised for " + name + "(" + argMessage + ")");
   146             }
   334             }
   147         } catch (Exception x) {
   335         } catch (Exception x) {
   148             if (!isExpected(x, ex)) {
   336             if (!isExpected(x, ex)) {
   149                 throw x;
   337                 throw x;
   150             } else {
   338             } else {
   151                 System.out.println("success: " + name + "(" + arg + ")" +
   339                 System.out.println("success: " + name + "(" + argMessage + ")" +
   152                         " - Got expected exception: " + x);
   340                         " - Got expected exception: " + x);
   153                 return receiver;
   341                 return receiver;
   154             }
   342             }
   155         }
   343         }
   156     }
   344     }