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