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  * @modules jdk.incubator.httpclient
  28  *          jdk.httpserver
  29  * @run main/othervm ImmutableHeaders
  30  * @summary ImmutableHeaders
  31  */
  32 
  33 import com.sun.net.httpserver.HttpExchange;
  34 import com.sun.net.httpserver.HttpHandler;
  35 import com.sun.net.httpserver.HttpServer;
  36 import com.sun.net.httpserver.Headers;
  37 import java.io.IOException;
  38 import java.io.InputStream;
  39 import java.io.OutputStream;
  40 import java.net.InetSocketAddress;
  41 import java.net.URI;
  42 import jdk.incubator.http.*;
  43 import java.util.concurrent.ExecutorService;
  44 import java.util.concurrent.Executors;
  45 import java.util.List;
  46 import static java.nio.charset.StandardCharsets.US_ASCII;
  47 import static jdk.incubator.http.HttpResponse.BodyHandler.discard;
  48 
  49 public class ImmutableHeaders {
  50 
  51     final static String RESPONSE = "Hello world";
  52 
  53     public static void main(String[] args) throws Exception {
  54         HttpServer server = HttpServer.create(new InetSocketAddress(0), 10);
  55         ExecutorService serverExecutor = Executors.newCachedThreadPool();
  56         ExecutorService clientExecutor = Executors.newCachedThreadPool();
  57         server.createContext("/test", new ImmutableHeadersHandler());
  58         int port = server.getAddress().getPort();
  59         System.out.println("Server port = " + port);
  60 
  61         server.setExecutor(serverExecutor);
  62         server.start();
  63         HttpClient client = HttpClient.newBuilder()
  64                                       .executor(clientExecutor)
  65                                       .build();
  66 
  67         try {
  68             URI uri = new URI("http://127.0.0.1:" + port + "/test/foo");
  69             HttpRequest req = HttpRequest.newBuilder(uri)
  70                                          .headers("X-Foo", "bar")
  71                                          .headers("X-Bar", "foo")
  72                                          .GET()
  73                                          .build();
  74 
  75             try {
  76                 HttpHeaders hd = req.headers();
  77                 List<String> v = hd.allValues("X-Foo");
  78                 if (!v.get(0).equals("bar"))
  79                     throw new RuntimeException("Test failed");
  80                 v.add("XX");
  81                 throw new RuntimeException("Test failed");
  82             } catch (UnsupportedOperationException ex) {
  83             }
  84             HttpResponse resp = client.send(req, discard(null));
  85             try {
  86                 HttpHeaders hd = resp.headers();
  87                 List<String> v = hd.allValues("X-Foo-Response");
  88                 if (!v.get(0).equals("resp"))
  89                     throw new RuntimeException("Test failed");
  90                 v.add("XX");
  91                 throw new RuntimeException("Test failed");
  92             } catch (UnsupportedOperationException ex) {
  93             }
  94 
  95         } finally {
  96             clientExecutor.shutdownNow();
  97             serverExecutor.shutdownNow();
  98             server.stop(0);
  99         }
 100         System.out.println("OK");
 101     }
 102 
 103    static class ImmutableHeadersHandler implements HttpHandler {
 104 
 105         @Override
 106         public void handle(HttpExchange he) throws IOException {
 107             String method = he.getRequestMethod();
 108             InputStream is = he.getRequestBody();
 109             Headers h = he.getResponseHeaders();
 110             h.add("X-Foo-Response", "resp");
 111             he.sendResponseHeaders(200, RESPONSE.length());
 112             OutputStream os = he.getResponseBody();
 113             os.write(RESPONSE.getBytes(US_ASCII));
 114             os.close();
 115         }
 116 
 117    }
 118 }