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 8087112 28 * @modules java.httpclient 29 * jdk.httpserver 30 * @run main/othervm ImmutableHeaders 31 * @summary ImmutableHeaders 32 */ 33 34 import com.sun.net.httpserver.HttpContext; 35 import com.sun.net.httpserver.HttpExchange; 36 import com.sun.net.httpserver.HttpHandler; 37 import com.sun.net.httpserver.HttpServer; 38 import com.sun.net.httpserver.Headers; 39 import java.io.IOException; 40 import java.io.InputStream; 41 import java.io.OutputStream; 42 import java.net.InetSocketAddress; 43 import java.net.PasswordAuthentication; 44 import java.net.URI; 45 import java.net.http.*; 46 import java.util.concurrent.ExecutorService; 47 import java.util.concurrent.Executors; 48 import java.util.List; 49 import static java.nio.charset.StandardCharsets.US_ASCII; 50 51 public class ImmutableHeaders { 52 53 final static String RESPONSE = "Hello world"; 54 55 public static void main(String[] args) throws Exception { 56 HttpServer server = HttpServer.create(new InetSocketAddress(0), 10); 57 ExecutorService e = Executors.newCachedThreadPool(); 58 Handler h = new Handler(); 59 HttpContext serverContext = server.createContext("/test", h); 60 int port = server.getAddress().getPort(); 61 System.out.println("Server port = " + port); 62 63 server.setExecutor(e); 64 server.start(); 65 HttpClient client = HttpClient.create() 66 .build(); 67 68 try { 69 URI uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/test/foo"); 70 HttpRequest req = client.request(uri) 71 .headers("X-Foo", "bar") 72 .headers("X-Bar", "foo") 73 .GET(); 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 = req.response(); 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 client.executorService().shutdownNow(); 97 server.stop(0); 98 e.shutdownNow(); 99 } 100 System.out.println("OK"); 101 } 102 103 static class Handler 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 }