2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
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. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. |
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
8 * |
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
9 * 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 |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
11 * 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 |
12 * version 2 for more details (a copy is included in the LICENSE file that |
24 */ |
22 */ |
25 |
23 |
26 /** |
24 /** |
27 * @test |
25 * @test |
28 * @bug 8087112 |
26 * @bug 8087112 |
29 * @modules java.httpclient |
27 * @modules jdk.incubator.httpclient |
30 * jdk.httpserver |
28 * jdk.httpserver |
31 * @run main/othervm ImmutableHeaders |
29 * @run main/othervm ImmutableHeaders |
32 * @summary ImmutableHeaders |
30 * @summary ImmutableHeaders |
33 */ |
31 */ |
34 |
32 |
35 import com.sun.net.httpserver.HttpContext; |
|
36 import com.sun.net.httpserver.HttpExchange; |
33 import com.sun.net.httpserver.HttpExchange; |
37 import com.sun.net.httpserver.HttpHandler; |
34 import com.sun.net.httpserver.HttpHandler; |
38 import com.sun.net.httpserver.HttpServer; |
35 import com.sun.net.httpserver.HttpServer; |
39 import com.sun.net.httpserver.Headers; |
36 import com.sun.net.httpserver.Headers; |
40 import java.io.IOException; |
37 import java.io.IOException; |
41 import java.io.InputStream; |
38 import java.io.InputStream; |
42 import java.io.OutputStream; |
39 import java.io.OutputStream; |
43 import java.net.InetSocketAddress; |
40 import java.net.InetSocketAddress; |
44 import java.net.PasswordAuthentication; |
|
45 import java.net.URI; |
41 import java.net.URI; |
46 import java.net.http.*; |
42 import jdk.incubator.http.*; |
47 import java.util.concurrent.ExecutorService; |
43 import java.util.concurrent.ExecutorService; |
48 import java.util.concurrent.Executors; |
44 import java.util.concurrent.Executors; |
49 import java.util.List; |
45 import java.util.List; |
50 import static java.nio.charset.StandardCharsets.US_ASCII; |
46 import static java.nio.charset.StandardCharsets.US_ASCII; |
|
47 import static jdk.incubator.http.HttpResponse.BodyHandler.discard; |
51 |
48 |
52 public class ImmutableHeaders { |
49 public class ImmutableHeaders { |
53 |
50 |
54 final static String RESPONSE = "Hello world"; |
51 final static String RESPONSE = "Hello world"; |
55 |
52 |
56 public static void main(String[] args) throws Exception { |
53 public static void main(String[] args) throws Exception { |
57 HttpServer server = HttpServer.create(new InetSocketAddress(0), 10); |
54 HttpServer server = HttpServer.create(new InetSocketAddress(0), 10); |
58 ExecutorService e = Executors.newCachedThreadPool(); |
55 ExecutorService serverExecutor = Executors.newCachedThreadPool(); |
59 Handler h = new Handler(); |
56 ExecutorService clientExecutor = Executors.newCachedThreadPool(); |
60 HttpContext serverContext = server.createContext("/test", h); |
57 server.createContext("/test", new ImmutableHeadersHandler()); |
61 int port = server.getAddress().getPort(); |
58 int port = server.getAddress().getPort(); |
62 System.out.println("Server port = " + port); |
59 System.out.println("Server port = " + port); |
63 |
60 |
64 server.setExecutor(e); |
61 server.setExecutor(serverExecutor); |
65 server.start(); |
62 server.start(); |
66 HttpClient client = HttpClient.create() |
63 HttpClient client = HttpClient.newBuilder() |
|
64 .executor(clientExecutor) |
67 .build(); |
65 .build(); |
68 |
66 |
69 try { |
67 try { |
70 URI uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/test/foo"); |
68 URI uri = new URI("http://127.0.0.1:" + port + "/test/foo"); |
71 HttpRequest req = client.request(uri) |
69 HttpRequest req = HttpRequest.newBuilder(uri) |
72 .headers("X-Foo", "bar") |
70 .headers("X-Foo", "bar") |
73 .headers("X-Bar", "foo") |
71 .headers("X-Bar", "foo") |
74 .GET(); |
72 .GET() |
|
73 .build(); |
75 |
74 |
76 try { |
75 try { |
77 HttpHeaders hd = req.headers(); |
76 HttpHeaders hd = req.headers(); |
78 List<String> v = hd.allValues("X-Foo"); |
77 List<String> v = hd.allValues("X-Foo"); |
79 if (!v.get(0).equals("bar")) |
78 if (!v.get(0).equals("bar")) |
80 throw new RuntimeException("Test failed"); |
79 throw new RuntimeException("Test failed"); |
81 v.add("XX"); |
80 v.add("XX"); |
82 throw new RuntimeException("Test failed"); |
81 throw new RuntimeException("Test failed"); |
83 } catch (UnsupportedOperationException ex) { |
82 } catch (UnsupportedOperationException ex) { |
84 } |
83 } |
85 HttpResponse resp = req.response(); |
84 HttpResponse resp = client.send(req, discard(null)); |
86 try { |
85 try { |
87 HttpHeaders hd = resp.headers(); |
86 HttpHeaders hd = resp.headers(); |
88 List<String> v = hd.allValues("X-Foo-Response"); |
87 List<String> v = hd.allValues("X-Foo-Response"); |
89 if (!v.get(0).equals("resp")) |
88 if (!v.get(0).equals("resp")) |
90 throw new RuntimeException("Test failed"); |
89 throw new RuntimeException("Test failed"); |