test/jdk/java/net/httpclient/ImmutableHeaders.java
changeset 49765 ee6f7a61f3a5
parent 48083 b1c1b4ef4be2
child 56451 9585061fdb04
equal deleted inserted replaced
49707:f7fd051519ac 49765:ee6f7a61f3a5
     1 /*
     1 /*
     2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2015, 2018, 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.
     7  * published by the Free Software Foundation.
    22  */
    22  */
    23 
    23 
    24 /**
    24 /**
    25  * @test
    25  * @test
    26  * @bug 8087112
    26  * @bug 8087112
    27  * @modules jdk.incubator.httpclient
    27  * @modules java.net.http
    28  *          jdk.httpserver
    28  *          jdk.httpserver
    29  * @run main/othervm ImmutableHeaders
    29  * @run main/othervm ImmutableHeaders
    30  * @summary ImmutableHeaders
    30  * @summary ImmutableHeaders
    31  */
    31  */
    32 
    32 
    35 import com.sun.net.httpserver.HttpServer;
    35 import com.sun.net.httpserver.HttpServer;
    36 import com.sun.net.httpserver.Headers;
    36 import com.sun.net.httpserver.Headers;
    37 import java.io.IOException;
    37 import java.io.IOException;
    38 import java.io.InputStream;
    38 import java.io.InputStream;
    39 import java.io.OutputStream;
    39 import java.io.OutputStream;
       
    40 import java.net.InetAddress;
    40 import java.net.InetSocketAddress;
    41 import java.net.InetSocketAddress;
    41 import java.net.URI;
    42 import java.net.URI;
    42 import jdk.incubator.http.*;
    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;
    43 import java.util.concurrent.ExecutorService;
    48 import java.util.concurrent.ExecutorService;
    44 import java.util.concurrent.Executors;
    49 import java.util.concurrent.Executors;
    45 import java.util.List;
    50 import java.util.List;
    46 import static java.nio.charset.StandardCharsets.US_ASCII;
    51 import static java.nio.charset.StandardCharsets.US_ASCII;
    47 import static jdk.incubator.http.HttpResponse.BodyHandler.discard;
       
    48 
    52 
    49 public class ImmutableHeaders {
    53 public class ImmutableHeaders {
    50 
    54 
    51     final static String RESPONSE = "Hello world";
    55     final static String RESPONSE = "Hello world";
    52 
    56 
    53     public static void main(String[] args) throws Exception {
    57     public static void main(String[] args) throws Exception {
    54         HttpServer server = HttpServer.create(new InetSocketAddress(0), 10);
    58         InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
       
    59         HttpServer server = HttpServer.create(addr, 10);
    55         ExecutorService serverExecutor = Executors.newCachedThreadPool();
    60         ExecutorService serverExecutor = Executors.newCachedThreadPool();
    56         ExecutorService clientExecutor = Executors.newCachedThreadPool();
    61         ExecutorService clientExecutor = Executors.newCachedThreadPool();
    57         server.createContext("/test", new ImmutableHeadersHandler());
    62         server.createContext("/test", new ImmutableHeadersHandler());
    58         int port = server.getAddress().getPort();
    63         int port = server.getAddress().getPort();
    59         System.out.println("Server port = " + port);
    64         System.out.println("Server port = " + port);
    63         HttpClient client = HttpClient.newBuilder()
    68         HttpClient client = HttpClient.newBuilder()
    64                                       .executor(clientExecutor)
    69                                       .executor(clientExecutor)
    65                                       .build();
    70                                       .build();
    66 
    71 
    67         try {
    72         try {
    68             URI uri = new URI("http://127.0.0.1:" + port + "/test/foo");
    73             URI uri = new URI("http://localhost:" + port + "/test/foo");
    69             HttpRequest req = HttpRequest.newBuilder(uri)
    74             HttpRequest req = HttpRequest.newBuilder(uri)
    70                                          .headers("X-Foo", "bar")
    75                                          .headers("X-Foo", "bar")
    71                                          .headers("X-Bar", "foo")
    76                                          .headers("X-Bar", "foo")
    72                                          .GET()
    77                                          .GET()
    73                                          .build();
    78                                          .build();
    79                     throw new RuntimeException("Test failed");
    84                     throw new RuntimeException("Test failed");
    80                 v.add("XX");
    85                 v.add("XX");
    81                 throw new RuntimeException("Test failed");
    86                 throw new RuntimeException("Test failed");
    82             } catch (UnsupportedOperationException ex) {
    87             } catch (UnsupportedOperationException ex) {
    83             }
    88             }
    84             HttpResponse resp = client.send(req, discard(null));
    89             HttpResponse resp = client.send(req, BodyHandlers.discarding());
    85             try {
    90             try {
    86                 HttpHeaders hd = resp.headers();
    91                 HttpHeaders hd = resp.headers();
    87                 List<String> v = hd.allValues("X-Foo-Response");
    92                 List<String> v = hd.allValues("X-Foo-Response");
    88                 if (!v.get(0).equals("resp"))
    93                 if (!v.get(0).equals("resp"))
    89                     throw new RuntimeException("Test failed");
    94                     throw new RuntimeException("Test failed");