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