test/jdk/java/net/httpclient/HeadersTest.java
changeset 49765 ee6f7a61f3a5
parent 48083 b1c1b4ef4be2
child 50681 4254bed3c09d
child 56451 9585061fdb04
equal deleted inserted replaced
49707:f7fd051519ac 49765:ee6f7a61f3a5
     1 /*
     1 /*
     2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2015, 2018, 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;
    24 import java.net.http.HttpClient;
       
    25 import java.net.http.HttpHeaders;
       
    26 import java.net.http.HttpRequest;
    25 import java.net.URI;
    27 import java.net.URI;
       
    28 import java.net.http.HttpResponse;
       
    29 import java.time.Duration;
       
    30 import java.util.ArrayList;
       
    31 import java.util.HashMap;
       
    32 import java.util.List;
       
    33 import java.util.Map;
       
    34 import java.util.Optional;
       
    35 import static java.net.http.HttpClient.Builder.NO_PROXY;
    26 
    36 
    27 /**
    37 /**
    28  * @test
    38  * @test
    29  * @bug 8087112
    39  * @bug 8087112
    30  * @summary Basic test for headers
    40  * @summary Basic test for headers, uri, and duration
    31  */
    41  */
    32 public class HeadersTest {
    42 public class HeadersTest {
    33 
    43 
    34     static final URI TEST_URI = URI.create("http://www.foo.com/");
    44     static final URI TEST_URI = URI.create("http://www.foo.com/");
    35 
    45     static final HttpClient client = HttpClient.newBuilder().proxy(NO_PROXY).build();
    36     static void bad(String name) {
    46 
       
    47     static final class HttpHeadersStub extends HttpHeaders {
       
    48         Map<String, List<String>> map;
       
    49         HttpHeadersStub(Map<String, List<String>> map) {
       
    50             this.map = map;
       
    51         }
       
    52         @Override
       
    53         public Map<String, List<String>> map() {
       
    54             return map;
       
    55         }
       
    56     }
       
    57 
       
    58     static void bad(String name) throws Exception {
    37         HttpRequest.Builder builder = HttpRequest.newBuilder(TEST_URI);
    59         HttpRequest.Builder builder = HttpRequest.newBuilder(TEST_URI);
    38         try {
    60         try {
    39             builder.header(name, "foo");
    61             builder.header(name, "foo");
    40             throw new RuntimeException("Expected IAE for header:" + name);
    62             throw new RuntimeException("Expected IAE for header:" + name);
    41         } catch (IllegalArgumentException expected) { }
    63         } catch (IllegalArgumentException expected)  {
       
    64             System.out.println("Got expected IAE: " + expected);
       
    65         }
       
    66         try {
       
    67             HttpRequest req = new HttpRequest() {
       
    68                 @Override public Optional<BodyPublisher> bodyPublisher() {
       
    69                     return Optional.of(BodyPublishers.noBody());
       
    70                 }
       
    71                 @Override public String method() {
       
    72                     return "GET";
       
    73                 }
       
    74                 @Override public Optional<Duration> timeout() {
       
    75                     return Optional.empty();
       
    76                 }
       
    77                 @Override public boolean expectContinue() {
       
    78                     return false;
       
    79                 }
       
    80                 @Override public URI uri() {
       
    81                     return TEST_URI;
       
    82                 }
       
    83                 @Override public Optional<HttpClient.Version> version() {
       
    84                     return Optional.empty();
       
    85                 }
       
    86                 @Override public HttpHeaders headers() {
       
    87                     Map<String, List<String>> map = Map.of(name, List.of("foo"));
       
    88                     return new HttpHeadersStub(map);
       
    89                 }
       
    90             };
       
    91             client.send(req, HttpResponse.BodyHandlers.ofString());
       
    92             throw new RuntimeException("Expected IAE for header:" + name);
       
    93         } catch (IllegalArgumentException expected) {
       
    94             System.out.println("Got expected IAE: " + expected);
       
    95         }
       
    96     }
       
    97 
       
    98     static void badValue(String value) throws Exception {
       
    99         HttpRequest.Builder builder = HttpRequest.newBuilder(TEST_URI);
       
   100         try {
       
   101             builder.header("x-bad", value);
       
   102             throw new RuntimeException("Expected IAE for header x-bad: "
       
   103                     + value.replace("\r", "\\r")
       
   104                     .replace("\n", "\\n"));
       
   105         } catch (IllegalArgumentException expected)  {
       
   106             System.out.println("Got expected IAE: " + expected);
       
   107         }
       
   108         try {
       
   109             HttpRequest req = new HttpRequest() {
       
   110                 @Override public Optional<BodyPublisher> bodyPublisher() {
       
   111                     return Optional.of(BodyPublishers.noBody());
       
   112                 }
       
   113                 @Override public String method() {
       
   114                     return "GET";
       
   115                 }
       
   116                 @Override public Optional<Duration> timeout() {
       
   117                     return Optional.empty();
       
   118                 }
       
   119                 @Override public boolean expectContinue() {
       
   120                     return false;
       
   121                 }
       
   122                 @Override public URI uri() {
       
   123                     return TEST_URI;
       
   124                 }
       
   125                 @Override public Optional<HttpClient.Version> version() {
       
   126                     return Optional.empty();
       
   127                 }
       
   128                 @Override public HttpHeaders headers() {
       
   129                     Map<String, List<String>> map = Map.of("x-bad", List.of(value));
       
   130                     return new HttpHeadersStub(map);
       
   131                 }
       
   132             };
       
   133             client.send(req, HttpResponse.BodyHandlers.ofString());
       
   134             throw new RuntimeException("Expected IAE for header x-bad:"
       
   135                     + value.replace("\r", "\\r")
       
   136                     .replace("\n", "\\n"));
       
   137         } catch (IllegalArgumentException expected) {
       
   138             System.out.println("Got expected IAE: " + expected);
       
   139         }
       
   140     }
       
   141 
       
   142     static void nullName() throws Exception {
       
   143         HttpRequest.Builder builder = HttpRequest.newBuilder(TEST_URI);
       
   144         try {
       
   145             builder.header(null, "foo");
       
   146             throw new RuntimeException("Expected NPE for null header name");
       
   147         } catch (NullPointerException expected)  {
       
   148             System.out.println("Got expected NPE: " + expected);
       
   149         }
       
   150         try {
       
   151             HttpRequest req = new HttpRequest() {
       
   152                 @Override public Optional<BodyPublisher> bodyPublisher() {
       
   153                     return Optional.of(BodyPublishers.noBody());
       
   154                 }
       
   155                 @Override public String method() {
       
   156                     return "GET";
       
   157                 }
       
   158                 @Override public Optional<Duration> timeout() {
       
   159                     return Optional.empty();
       
   160                 }
       
   161                 @Override public boolean expectContinue() {
       
   162                     return false;
       
   163                 }
       
   164                 @Override public URI uri() {
       
   165                     return TEST_URI;
       
   166                 }
       
   167                 @Override public Optional<HttpClient.Version> version() {
       
   168                     return Optional.empty();
       
   169                 }
       
   170                 @Override public HttpHeaders headers() {
       
   171                     Map<String, List<String>> map = new HashMap<>();
       
   172                     map.put(null, List.of("foo"));
       
   173                     return new HttpHeadersStub(map);
       
   174                 }
       
   175             };
       
   176             client.send(req, HttpResponse.BodyHandlers.ofString());
       
   177             throw new RuntimeException("Expected NPE for null header name");
       
   178         } catch (NullPointerException expected) {
       
   179             System.out.println("Got expected NPE: " + expected);
       
   180         }
       
   181     }
       
   182 
       
   183     static void nullValue() throws Exception {
       
   184         HttpRequest.Builder builder = HttpRequest.newBuilder(TEST_URI);
       
   185         try {
       
   186             builder.header("x-bar", null);
       
   187             throw new RuntimeException("Expected NPE for null header value");
       
   188         } catch (NullPointerException expected)  {
       
   189             System.out.println("Got expected NPE: " + expected);
       
   190         }
       
   191         try {
       
   192             HttpRequest req = new HttpRequest() {
       
   193                 @Override public Optional<BodyPublisher> bodyPublisher() {
       
   194                     return Optional.of(BodyPublishers.noBody());
       
   195                 }
       
   196                 @Override public String method() {
       
   197                     return "GET";
       
   198                 }
       
   199                 @Override public Optional<Duration> timeout() {
       
   200                     return Optional.empty();
       
   201                 }
       
   202                 @Override public boolean expectContinue() {
       
   203                     return false;
       
   204                 }
       
   205                 @Override public URI uri() {
       
   206                     return TEST_URI;
       
   207                 }
       
   208                 @Override public Optional<HttpClient.Version> version() {
       
   209                     return Optional.empty();
       
   210                 }
       
   211                 @Override public HttpHeaders headers() {
       
   212                     Map<String, List<String>> map = new HashMap<>();
       
   213                     map.put("x-bar", null);
       
   214                     return new HttpHeadersStub(map);
       
   215                 }
       
   216             };
       
   217             client.send(req, HttpResponse.BodyHandlers.ofString());
       
   218             throw new RuntimeException("Expected NPE for null header values");
       
   219         } catch (NullPointerException expected) {
       
   220             System.out.println("Got expected NPE: " + expected);
       
   221         }
       
   222         try {
       
   223             HttpRequest req = new HttpRequest() {
       
   224                 @Override public Optional<BodyPublisher> bodyPublisher() {
       
   225                     return Optional.of(BodyPublishers.noBody());
       
   226                 }
       
   227                 @Override public String method() {
       
   228                     return "GET";
       
   229                 }
       
   230                 @Override public Optional<Duration> timeout() {
       
   231                     return Optional.empty();
       
   232                 }
       
   233                 @Override public boolean expectContinue() {
       
   234                     return false;
       
   235                 }
       
   236                 @Override public URI uri() {
       
   237                     return TEST_URI;
       
   238                 }
       
   239                 @Override public Optional<HttpClient.Version> version() {
       
   240                     return Optional.empty();
       
   241                 }
       
   242                 @Override public HttpHeaders headers() {
       
   243                     List<String> values = new ArrayList<>();
       
   244                     values.add("foo");
       
   245                     values.add(null);
       
   246                     return new HttpHeadersStub(Map.of("x-bar", values));
       
   247                 }
       
   248             };
       
   249             client
       
   250                     .send(req, HttpResponse.BodyHandlers.ofString());
       
   251             throw new RuntimeException("Expected NPE for null header value");
       
   252         } catch (NullPointerException expected) {
       
   253             System.out.println("Got expected NPE: " + expected);
       
   254         }
       
   255     }
       
   256 
       
   257     static void nullHeaders() throws Exception {
       
   258         try {
       
   259             HttpRequest req = new HttpRequest() {
       
   260                 @Override public Optional<BodyPublisher> bodyPublisher() {
       
   261                     return Optional.of(BodyPublishers.noBody());
       
   262                 }
       
   263                 @Override public String method() {
       
   264                     return "GET";
       
   265                 }
       
   266                 @Override public Optional<Duration> timeout() {
       
   267                     return Optional.empty();
       
   268                 }
       
   269                 @Override public boolean expectContinue() {
       
   270                     return false;
       
   271                 }
       
   272                 @Override public URI uri() {
       
   273                     return TEST_URI;
       
   274                 }
       
   275                 @Override public Optional<HttpClient.Version> version() {
       
   276                     return Optional.empty();
       
   277                 }
       
   278                 @Override public HttpHeaders headers() {
       
   279                     return new HttpHeadersStub(null);
       
   280                 }
       
   281             };
       
   282             client.send(req, HttpResponse.BodyHandlers.ofString());
       
   283             throw new RuntimeException("Expected NPE for null header name");
       
   284         } catch (NullPointerException expected) {
       
   285             System.out.println("Got expected NPE: " + expected);
       
   286         }
       
   287         try {
       
   288             HttpRequest req = new HttpRequest() {
       
   289                 @Override public Optional<BodyPublisher> bodyPublisher() {
       
   290                     return Optional.of(BodyPublishers.noBody());
       
   291                 }
       
   292                 @Override public String method() {
       
   293                     return "GET";
       
   294                 }
       
   295                 @Override public Optional<Duration> timeout() {
       
   296                     return Optional.empty();
       
   297                 }
       
   298                 @Override public boolean expectContinue() {
       
   299                     return false;
       
   300                 }
       
   301                 @Override public URI uri() {
       
   302                     return TEST_URI;
       
   303                 }
       
   304                 @Override public Optional<HttpClient.Version> version() {
       
   305                     return Optional.empty();
       
   306                 }
       
   307                 @Override public HttpHeaders headers() {
       
   308                     return null;
       
   309                 }
       
   310             };
       
   311             client.send(req, HttpResponse.BodyHandlers.ofString());
       
   312             throw new RuntimeException("Expected NPE for null header name");
       
   313         } catch (NullPointerException expected) {
       
   314             System.out.println("Got expected NPE: " + expected);
       
   315         }
    42     }
   316     }
    43 
   317 
    44     static void good(String name) {
   318     static void good(String name) {
    45         HttpRequest.Builder builder = HttpRequest.newBuilder(TEST_URI);
   319         HttpRequest.Builder builder = HttpRequest.newBuilder(TEST_URI);
    46         try {
   320         try {
    48         } catch (IllegalArgumentException e) {
   322         } catch (IllegalArgumentException e) {
    49             throw new RuntimeException("Unexpected IAE for header:" + name);
   323             throw new RuntimeException("Unexpected IAE for header:" + name);
    50         }
   324         }
    51     }
   325     }
    52 
   326 
    53     public static void main(String[] args) {
   327     static void goodValue(String value) {
       
   328         HttpRequest.Builder builder = HttpRequest.newBuilder(TEST_URI);
       
   329         try {
       
   330             builder.header("x-good", value);
       
   331         } catch (IllegalArgumentException e) {
       
   332             throw new RuntimeException("Unexpected IAE for x-good: " + value);
       
   333         }
       
   334     }
       
   335 
       
   336     static void badURI() throws Exception {
       
   337         HttpRequest.Builder builder = HttpRequest.newBuilder();
       
   338         URI uri = URI.create(TEST_URI.toString().replace("http", "ftp"));
       
   339         try {
       
   340             builder.uri(uri);
       
   341             throw new RuntimeException("Expected IAE for uri: " + uri);
       
   342         } catch (IllegalArgumentException expected)  {
       
   343             System.out.println("Got expected IAE: " + expected);
       
   344         }
       
   345         try {
       
   346             HttpRequest.newBuilder(uri);
       
   347             throw new RuntimeException("Expected IAE for uri: " + uri);
       
   348         } catch (IllegalArgumentException expected)  {
       
   349             System.out.println("Got expected IAE: " + expected);
       
   350         }
       
   351         try {
       
   352             HttpRequest req = new HttpRequest() {
       
   353                 @Override public Optional<BodyPublisher> bodyPublisher() {
       
   354                     return Optional.of(BodyPublishers.noBody());
       
   355                 }
       
   356                 @Override public String method() {
       
   357                     return "GET";
       
   358                 }
       
   359                 @Override public Optional<Duration> timeout() {
       
   360                     return Optional.empty();
       
   361                 }
       
   362                 @Override public boolean expectContinue() {
       
   363                     return false;
       
   364                 }
       
   365                 @Override public URI uri() {
       
   366                     return uri;
       
   367                 }
       
   368                 @Override public Optional<HttpClient.Version> version() {
       
   369                     return Optional.empty();
       
   370                 }
       
   371                 @Override public HttpHeaders headers() {
       
   372                     Map<String, List<String>> map = Map.of("x-good", List.of("foo"));
       
   373                     return new HttpHeadersStub(map);
       
   374                 }
       
   375             };
       
   376             client.send(req, HttpResponse.BodyHandlers.ofString());
       
   377             throw new RuntimeException("Expected IAE for uri:" + uri);
       
   378         } catch (IllegalArgumentException expected) {
       
   379             System.out.println("Got expected IAE: " + expected);
       
   380         }
       
   381     }
       
   382 
       
   383     static void nullURI() throws Exception {
       
   384         HttpRequest.Builder builder = HttpRequest.newBuilder();
       
   385         try {
       
   386             builder.uri(null);
       
   387             throw new RuntimeException("Expected NPE for null URI");
       
   388         } catch (NullPointerException expected)  {
       
   389             System.out.println("Got expected NPE: " + expected);
       
   390         }
       
   391         try {
       
   392             HttpRequest.newBuilder(null);
       
   393             throw new RuntimeException("Expected NPE for null uri");
       
   394         } catch (NullPointerException expected)  {
       
   395             System.out.println("Got expected NPE: " + expected);
       
   396         }
       
   397         try {
       
   398             HttpRequest req = new HttpRequest() {
       
   399                 @Override public Optional<BodyPublisher> bodyPublisher() {
       
   400                     return Optional.of(BodyPublishers.noBody());
       
   401                 }
       
   402                 @Override public String method() {
       
   403                     return "GET";
       
   404                 }
       
   405                 @Override public Optional<Duration> timeout() {
       
   406                     return Optional.empty();
       
   407                 }
       
   408                 @Override public boolean expectContinue() {
       
   409                     return false;
       
   410                 }
       
   411                 @Override public URI uri() {
       
   412                     return null;
       
   413                 }
       
   414                 @Override public Optional<HttpClient.Version> version() {
       
   415                     return Optional.empty();
       
   416                 }
       
   417                 @Override public HttpHeaders headers() {
       
   418                     Map<String, List<String>> map = Map.of("x-good", List.of("foo"));
       
   419                     return new HttpHeadersStub(map);
       
   420                 }
       
   421             };
       
   422             client.send(req, HttpResponse.BodyHandlers.ofString());
       
   423             throw new RuntimeException("Expected NPE for null uri");
       
   424         } catch (NullPointerException expected) {
       
   425             System.out.println("Got expected NPE: " + expected);
       
   426         }
       
   427     }
       
   428 
       
   429     static void badTimeout() throws Exception {
       
   430         HttpRequest.Builder builder = HttpRequest.newBuilder(TEST_URI);
       
   431         Duration zero = Duration.ofSeconds(0);
       
   432         Duration negative = Duration.ofSeconds(-10);
       
   433         for (Duration bad : List.of(zero, negative)) {
       
   434             try {
       
   435                 builder.timeout(zero);
       
   436                 throw new RuntimeException("Expected IAE for timeout: " + bad);
       
   437             } catch (IllegalArgumentException expected) {
       
   438                 System.out.println("Got expected IAE: " + expected);
       
   439             }
       
   440             try {
       
   441                 HttpRequest req = new HttpRequest() {
       
   442                     @Override
       
   443                     public Optional<BodyPublisher> bodyPublisher() {
       
   444                         return Optional.of(BodyPublishers.noBody());
       
   445                     }
       
   446 
       
   447                     @Override
       
   448                     public String method() {
       
   449                         return "GET";
       
   450                     }
       
   451 
       
   452                     @Override
       
   453                     public Optional<Duration> timeout() {
       
   454                         return Optional.of(bad);
       
   455                     }
       
   456 
       
   457                     @Override
       
   458                     public boolean expectContinue() {
       
   459                         return false;
       
   460                     }
       
   461 
       
   462                     @Override
       
   463                     public URI uri() {
       
   464                         return TEST_URI;
       
   465                     }
       
   466 
       
   467                     @Override
       
   468                     public Optional<HttpClient.Version> version() {
       
   469                         return Optional.empty();
       
   470                     }
       
   471 
       
   472                     @Override
       
   473                     public HttpHeaders headers() {
       
   474                         Map<String, List<String>> map = Map.of("x-good", List.of("foo"));
       
   475                         return new HttpHeadersStub(map);
       
   476                     }
       
   477                 };
       
   478                 client.send(req, HttpResponse.BodyHandlers.ofString());
       
   479                 throw new RuntimeException("Expected IAE for timeout:" + bad);
       
   480             } catch (IllegalArgumentException expected) {
       
   481                 System.out.println("Got expected IAE: " + expected);
       
   482             }
       
   483         }
       
   484     }
       
   485 
       
   486     static void nullTimeout() throws Exception {
       
   487         HttpRequest.Builder builder = HttpRequest.newBuilder(TEST_URI);
       
   488         try {
       
   489             builder.timeout(null);
       
   490             throw new RuntimeException("Expected NPE for null timeout");
       
   491         } catch (NullPointerException expected) {
       
   492             System.out.println("Got expected NPE: " + expected);
       
   493         }
       
   494         try {
       
   495             HttpRequest req = new HttpRequest() {
       
   496                 @Override
       
   497                 public Optional<BodyPublisher> bodyPublisher() {
       
   498                     return Optional.of(BodyPublishers.noBody());
       
   499                 }
       
   500 
       
   501                 @Override
       
   502                 public String method() {
       
   503                     return "GET";
       
   504                 }
       
   505 
       
   506                 @Override
       
   507                 public Optional<Duration> timeout() {
       
   508                     return null;
       
   509                 }
       
   510 
       
   511                 @Override
       
   512                 public boolean expectContinue() {
       
   513                     return false;
       
   514                 }
       
   515 
       
   516                 @Override
       
   517                 public URI uri() {
       
   518                     return TEST_URI;
       
   519                 }
       
   520 
       
   521                 @Override
       
   522                 public Optional<HttpClient.Version> version() {
       
   523                     return Optional.empty();
       
   524                 }
       
   525 
       
   526                 @Override
       
   527                 public HttpHeaders headers() {
       
   528                     Map<String, List<String>> map = Map.of("x-good", List.of("foo"));
       
   529                     return new HttpHeadersStub(map);
       
   530                 }
       
   531             };
       
   532             client.send(req, HttpResponse.BodyHandlers.ofString());
       
   533             throw new RuntimeException("Expected NPE for null timeout");
       
   534         } catch (NullPointerException expected) {
       
   535             System.out.println("Got expected NPE: " + expected);
       
   536         }
       
   537     }
       
   538 
       
   539     public static void main(String[] args) throws Exception {
    54         bad("bad:header");
   540         bad("bad:header");
    55         bad("Foo\n");
   541         bad("Foo\n");
    56         good("X-Foo!");
   542         good("X-Foo!");
    57         good("Bar~");
   543         good("Bar~");
    58         good("x");
   544         good("x");
    59         bad(" ");
   545         bad(" ");
    60         bad("Bar\r\n");
   546         bad("Bar\r\n");
    61         good("Hello#world");
   547         good("Hello#world");
    62         good("Qwer#ert");
   548         good("Qwer#ert");
       
   549         badValue("blah\r\n blah");
       
   550         goodValue("blah blah");
       
   551         goodValue("blah  blah");
       
   552         goodValue("\"blah\\\"  \\\"blah\"");
       
   553         nullName();
       
   554         nullValue();
       
   555         nullHeaders();
       
   556         badURI();
       
   557         nullURI();
       
   558         badTimeout();
       
   559         nullTimeout();
    63     }
   560     }
    64 }
   561 }