< prev index next >

test/jdk/java/net/httpclient/HeadersTest1.java

Print this page


   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  */


  78             URI uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/test/foo");
  79             HttpRequest req = HttpRequest.newBuilder(uri)
  80                                          .headers("X-Bar", "foo1")
  81                                          .headers("X-Bar", "foo2")
  82                                          .GET()
  83                                          .build();
  84 
  85             HttpResponse<?> resp = client.send(req, asString());
  86             if (resp.statusCode() != 200) {
  87                 throw new RuntimeException("Expected 200, got: " + resp.statusCode());
  88             }
  89             HttpHeaders hd = resp.headers();
  90 
  91             assertTrue(!hd.firstValue("Non-Existent-Header").isPresent());
  92 
  93             List<String> v1 = hd.allValues("Non-Existent-Header");
  94             assertNotNull(v1);
  95             assertTrue(v1.isEmpty(), String.valueOf(v1));
  96             TestKit.assertUnmodifiableList(v1);
  97 
  98             List<String> v2 = hd.allValues("X-Foo-Response");





  99             assertNotNull(v2);
 100             assertEquals(new HashSet<>(v2), Set.of("resp1", "resp2"));
 101             TestKit.assertUnmodifiableList(v2);

 102 
 103             Map<String, List<String>> map = hd.map();
 104             TestKit.assertUnmodifiableMap(map);
 105             for (List<String> values : map.values()) {
 106                 TestKit.assertUnmodifiableList(values);
 107             }




 108         } finally {
 109             server.stop(0);
 110             e.shutdownNow();
 111         }
 112         System.out.println("OK");
 113     }
 114 
 115     private static final class Handler implements HttpHandler {
 116 
 117         @Override
 118         public void handle(HttpExchange he) throws IOException {
 119             List<String> l = he.getRequestHeaders().get("X-Bar");
 120             if (!l.contains("foo1") || !l.contains("foo2")) {
 121                 for (String s : l) {
 122                     System.out.println("HH: " + s);
 123                 }
 124                 he.sendResponseHeaders(500, -1);
 125                 he.close();
 126                 return;
 127             }
   1 /*
   2  * Copyright (c) 2015, 2017, 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  */


  78             URI uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/test/foo");
  79             HttpRequest req = HttpRequest.newBuilder(uri)
  80                                          .headers("X-Bar", "foo1")
  81                                          .headers("X-Bar", "foo2")
  82                                          .GET()
  83                                          .build();
  84 
  85             HttpResponse<?> resp = client.send(req, asString());
  86             if (resp.statusCode() != 200) {
  87                 throw new RuntimeException("Expected 200, got: " + resp.statusCode());
  88             }
  89             HttpHeaders hd = resp.headers();
  90 
  91             assertTrue(!hd.firstValue("Non-Existent-Header").isPresent());
  92 
  93             List<String> v1 = hd.allValues("Non-Existent-Header");
  94             assertNotNull(v1);
  95             assertTrue(v1.isEmpty(), String.valueOf(v1));
  96             TestKit.assertUnmodifiableList(v1);
  97 
  98             // case insensitive
  99             List<String> headernames = List.of("X-Foo-Response",
 100                                                "x-foo-Response",
 101                                                "x-fOo-REspoNse");
 102             for (String headerName : headernames) {
 103                 List<String> v2 = hd.allValues(headerName);
 104                 assertNotNull(v2);
 105                 assertEquals(new HashSet<>(v2), Set.of("resp1", "resp2"));
 106                 TestKit.assertUnmodifiableList(v2);
 107             }
 108 
 109             Map<String, List<String>> map = hd.map();
 110             TestKit.assertUnmodifiableMap(map);
 111             for (List<String> values : map.values()) {
 112                 TestKit.assertUnmodifiableList(values);
 113             }
 114 
 115             // toString
 116             hd.toString().toLowerCase().contains("content-length");
 117             hd.toString().toLowerCase().contains("x-foo-response");
 118         } finally {
 119             server.stop(0);
 120             e.shutdownNow();
 121         }
 122         System.out.println("OK");
 123     }
 124 
 125     private static final class Handler implements HttpHandler {
 126 
 127         @Override
 128         public void handle(HttpExchange he) throws IOException {
 129             List<String> l = he.getRequestHeaders().get("X-Bar");
 130             if (!l.contains("foo1") || !l.contains("foo2")) {
 131                 for (String s : l) {
 132                     System.out.println("HH: " + s);
 133                 }
 134                 he.sendResponseHeaders(500, -1);
 135                 he.close();
 136                 return;
 137             }
< prev index next >