|
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 8153142 |
|
27 * @modules jdk.incubator.httpclient |
|
28 * jdk.httpserver |
|
29 * @run testng/othervm HeadersTest1 |
|
30 */ |
|
31 |
|
32 import java.io.IOException; |
|
33 import java.io.OutputStream; |
|
34 import java.net.InetSocketAddress; |
|
35 import java.net.URI; |
|
36 import jdk.incubator.http.HttpClient; |
|
37 import jdk.incubator.http.HttpHeaders; |
|
38 import jdk.incubator.http.HttpRequest; |
|
39 import jdk.incubator.http.HttpResponse; |
|
40 import java.util.HashSet; |
|
41 import java.util.List; |
|
42 import java.util.Map; |
|
43 import java.util.Set; |
|
44 import java.util.concurrent.ExecutorService; |
|
45 import java.util.concurrent.Executors; |
|
46 import com.sun.net.httpserver.Headers; |
|
47 import com.sun.net.httpserver.HttpExchange; |
|
48 import com.sun.net.httpserver.HttpHandler; |
|
49 import com.sun.net.httpserver.HttpServer; |
|
50 import org.testng.annotations.Test; |
|
51 import static jdk.incubator.http.HttpResponse.BodyHandler.asString; |
|
52 import static java.nio.charset.StandardCharsets.US_ASCII; |
|
53 import static org.testng.Assert.assertEquals; |
|
54 import static org.testng.Assert.assertNotNull; |
|
55 import static org.testng.Assert.assertTrue; |
|
56 |
|
57 |
|
58 public class HeadersTest1 { |
|
59 |
|
60 private static final String RESPONSE = "Hello world"; |
|
61 |
|
62 @Test |
|
63 public void test() throws Exception { |
|
64 HttpServer server = HttpServer.create(new InetSocketAddress(0), 10); |
|
65 Handler h = new Handler(); |
|
66 server.createContext("/test", h); |
|
67 int port = server.getAddress().getPort(); |
|
68 System.out.println("Server port = " + port); |
|
69 |
|
70 ExecutorService e = Executors.newCachedThreadPool(); |
|
71 server.setExecutor(e); |
|
72 server.start(); |
|
73 HttpClient client = HttpClient.newBuilder() |
|
74 .executor(e) |
|
75 .build(); |
|
76 |
|
77 try { |
|
78 URI uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/test/foo"); |
|
79 HttpRequest req = HttpRequest.newBuilder(uri) |
|
80 .headers("X-Bar", "foo1") |
|
81 .headers("X-Bar", "foo2") |
|
82 .GET() |
|
83 .build(); |
|
84 |
|
85 HttpResponse<?> resp = client.send(req, asString()); |
|
86 if (resp.statusCode() != 200) { |
|
87 throw new RuntimeException("Expected 200, got: " + resp.statusCode()); |
|
88 } |
|
89 HttpHeaders hd = resp.headers(); |
|
90 |
|
91 assertTrue(!hd.firstValue("Non-Existent-Header").isPresent()); |
|
92 |
|
93 List<String> v1 = hd.allValues("Non-Existent-Header"); |
|
94 assertNotNull(v1); |
|
95 assertTrue(v1.isEmpty(), String.valueOf(v1)); |
|
96 TestKit.assertUnmodifiableList(v1); |
|
97 |
|
98 List<String> v2 = hd.allValues("X-Foo-Response"); |
|
99 assertNotNull(v2); |
|
100 assertEquals(new HashSet<>(v2), Set.of("resp1", "resp2")); |
|
101 TestKit.assertUnmodifiableList(v2); |
|
102 |
|
103 Map<String, List<String>> map = hd.map(); |
|
104 TestKit.assertUnmodifiableMap(map); |
|
105 for (List<String> values : map.values()) { |
|
106 TestKit.assertUnmodifiableList(values); |
|
107 } |
|
108 } finally { |
|
109 server.stop(0); |
|
110 e.shutdownNow(); |
|
111 } |
|
112 System.out.println("OK"); |
|
113 } |
|
114 |
|
115 private static final class Handler implements HttpHandler { |
|
116 |
|
117 @Override |
|
118 public void handle(HttpExchange he) throws IOException { |
|
119 List<String> l = he.getRequestHeaders().get("X-Bar"); |
|
120 if (!l.contains("foo1") || !l.contains("foo2")) { |
|
121 for (String s : l) { |
|
122 System.out.println("HH: " + s); |
|
123 } |
|
124 he.sendResponseHeaders(500, -1); |
|
125 he.close(); |
|
126 return; |
|
127 } |
|
128 Headers h = he.getResponseHeaders(); |
|
129 h.add("X-Foo-Response", "resp1"); |
|
130 h.add("X-Foo-Response", "resp2"); |
|
131 he.sendResponseHeaders(200, RESPONSE.length()); |
|
132 OutputStream os = he.getResponseBody(); |
|
133 os.write(RESPONSE.getBytes(US_ASCII)); |
|
134 os.close(); |
|
135 } |
|
136 } |
|
137 } |