author | jboes |
Fri, 08 Nov 2019 11:15:16 +0000 | |
changeset 59029 | 3786a0962570 |
parent 49765 | ee6f7a61f3a5 |
child 56451 | 9585061fdb04 |
permissions | -rw-r--r-- |
36131 | 1 |
/* |
49765 | 2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
36131 | 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 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
7 |
* published by the Free Software Foundation. |
36131 | 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 |
|
40684
2e37c119dc2a
8164982: Fix legal notices in java/lang, java/net, java/util tests.
shurailine
parents:
38883
diff
changeset
|
21 |
* questions. |
36131 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* @test |
|
26 |
* @bug 8087112 |
|
49765 | 27 |
* @modules java.net.http |
38883 | 28 |
* jdk.httpserver |
36131 | 29 |
* @run main/othervm ImmutableHeaders |
30 |
* @summary ImmutableHeaders |
|
31 |
*/ |
|
32 |
||
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; |
|
49765 | 40 |
import java.net.InetAddress; |
36131 | 41 |
import java.net.InetSocketAddress; |
42 |
import java.net.URI; |
|
49765 | 43 |
import java.net.http.HttpClient; |
44 |
import java.net.http.HttpHeaders; |
|
45 |
import java.net.http.HttpRequest; |
|
46 |
import java.net.http.HttpResponse; |
|
47 |
import java.net.http.HttpResponse.BodyHandlers; |
|
36131 | 48 |
import java.util.concurrent.ExecutorService; |
49 |
import java.util.concurrent.Executors; |
|
50 |
import java.util.List; |
|
51 |
import static java.nio.charset.StandardCharsets.US_ASCII; |
|
52 |
||
53 |
public class ImmutableHeaders { |
|
54 |
||
55 |
final static String RESPONSE = "Hello world"; |
|
56 |
||
57 |
public static void main(String[] args) throws Exception { |
|
49765 | 58 |
InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0); |
59 |
HttpServer server = HttpServer.create(addr, 10); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
60 |
ExecutorService serverExecutor = Executors.newCachedThreadPool(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
61 |
ExecutorService clientExecutor = Executors.newCachedThreadPool(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
62 |
server.createContext("/test", new ImmutableHeadersHandler()); |
36131 | 63 |
int port = server.getAddress().getPort(); |
64 |
System.out.println("Server port = " + port); |
|
65 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
66 |
server.setExecutor(serverExecutor); |
36131 | 67 |
server.start(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
68 |
HttpClient client = HttpClient.newBuilder() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
69 |
.executor(clientExecutor) |
36131 | 70 |
.build(); |
71 |
||
72 |
try { |
|
49765 | 73 |
URI uri = new URI("http://localhost:" + port + "/test/foo"); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
74 |
HttpRequest req = HttpRequest.newBuilder(uri) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
75 |
.headers("X-Foo", "bar") |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
76 |
.headers("X-Bar", "foo") |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
77 |
.GET() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
78 |
.build(); |
36131 | 79 |
|
80 |
try { |
|
81 |
HttpHeaders hd = req.headers(); |
|
82 |
List<String> v = hd.allValues("X-Foo"); |
|
83 |
if (!v.get(0).equals("bar")) |
|
84 |
throw new RuntimeException("Test failed"); |
|
85 |
v.add("XX"); |
|
86 |
throw new RuntimeException("Test failed"); |
|
87 |
} catch (UnsupportedOperationException ex) { |
|
88 |
} |
|
49765 | 89 |
HttpResponse resp = client.send(req, BodyHandlers.discarding()); |
36131 | 90 |
try { |
91 |
HttpHeaders hd = resp.headers(); |
|
92 |
List<String> v = hd.allValues("X-Foo-Response"); |
|
93 |
if (!v.get(0).equals("resp")) |
|
94 |
throw new RuntimeException("Test failed"); |
|
95 |
v.add("XX"); |
|
96 |
throw new RuntimeException("Test failed"); |
|
97 |
} catch (UnsupportedOperationException ex) { |
|
98 |
} |
|
99 |
||
100 |
} finally { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
101 |
clientExecutor.shutdownNow(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
102 |
serverExecutor.shutdownNow(); |
36131 | 103 |
server.stop(0); |
104 |
} |
|
105 |
System.out.println("OK"); |
|
106 |
} |
|
107 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
40684
diff
changeset
|
108 |
static class ImmutableHeadersHandler implements HttpHandler { |
36131 | 109 |
|
110 |
@Override |
|
111 |
public void handle(HttpExchange he) throws IOException { |
|
112 |
String method = he.getRequestMethod(); |
|
113 |
InputStream is = he.getRequestBody(); |
|
114 |
Headers h = he.getResponseHeaders(); |
|
115 |
h.add("X-Foo-Response", "resp"); |
|
116 |
he.sendResponseHeaders(200, RESPONSE.length()); |
|
117 |
OutputStream os = he.getResponseBody(); |
|
118 |
os.write(RESPONSE.getBytes(US_ASCII)); |
|
119 |
os.close(); |
|
120 |
} |
|
121 |
||
122 |
} |
|
123 |
} |