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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  */
  24 
  25 /**
  26  * @test
  27  * @bug 8153142
  28  * @run main/othervm HeadersTest1
  29  * @summary HeadersTest1
  30  */
  31 
  32 import com.sun.net.httpserver.HttpContext;
  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.PasswordAuthentication;
  42 import java.net.URI;
  43 import java.net.http.*;
  44 import java.util.concurrent.ExecutorService;
  45 import java.util.concurrent.Executors;
  46 import java.util.List;
  47 import static java.nio.charset.StandardCharsets.US_ASCII;
  48 
  49 public class HeadersTest1 {
  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 e = Executors.newCachedThreadPool();
  56         Handler h = new Handler();
  57         HttpContext serverContext = server.createContext("/test", h);
  58         int port = server.getAddress().getPort();
  59         System.out.println("Server port = " + port);
  60 
  61         server.setExecutor(e);
  62         server.start();
  63         HttpClient client = HttpClient.create()
  64                                       .build();
  65 
  66         try {
  67             URI uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/test/foo");
  68             HttpRequest req = client.request(uri)
  69                 .headers("X-Bar", "foo1")
  70                 .headers("X-Bar", "foo2")
  71                 .GET();
  72 
  73             HttpResponse resp = req.response();
  74             if (resp.statusCode() != 200)
  75                 throw new RuntimeException("Test failed: status code");
  76             HttpHeaders hd = resp.headers();
  77             List<String> v = hd.allValues("X-Foo-Response");
  78             if (!v.contains("resp1"))
  79                 throw new RuntimeException("Test failed: resp1");
  80             if (!v.contains("resp2"))
  81                 throw new RuntimeException("Test failed: resp2");
  82 
  83         } finally {
  84             client.executorService().shutdownNow();
  85             server.stop(0);
  86             e.shutdownNow();
  87         }
  88         System.out.println("OK");
  89     }
  90 
  91    static class Handler implements HttpHandler {
  92 
  93         @Override
  94         public void handle(HttpExchange he) throws IOException {
  95             String method = he.getRequestMethod();
  96             InputStream is = he.getRequestBody();
  97             List<String> l = he.getRequestHeaders().get("X-Bar");
  98             if (!l.contains("foo1") || !l.contains("foo2")) {
  99                 for (String s : l)
 100                     System.out.println("HH: " + s);
 101                 he.sendResponseHeaders(500, -1);
 102                 he.close();
 103                 return;
 104             }
 105             Headers h = he.getResponseHeaders();
 106             h.add("X-Foo-Response", "resp1");
 107             h.add("X-Foo-Response", "resp2");
 108             he.sendResponseHeaders(200, RESPONSE.length());
 109             OutputStream os = he.getResponseBody();
 110             os.write(RESPONSE.getBytes(US_ASCII));
 111             os.close();
 112         }
 113 
 114    }
 115 }