test/jdk/java/net/httpclient/HeadersTest1.java
author chegar
Thu, 16 Nov 2017 10:29:18 +0000
branchhttp-client-branch
changeset 55819 18e431209168
parent 47216 71c04702a3d5
child 55873 b5e6a3201081
permissions -rw-r--r--
http-client-branch: Update copyright years in tests
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
     1
/*
55819
18e431209168 http-client-branch: Update copyright years in tests
chegar
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
     4
 *
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
     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: 41103
diff changeset
     7
 * published by the Free Software Foundation.
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
     8
 *
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    13
 * accompanied this code).
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    14
 *
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    18
 *
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    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: 38465
diff changeset
    21
 * questions.
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    22
 */
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    23
41103
c0b875b35b1e 8166285: Missing dependencies java.httpclient for tests from java/net pachage
skovalev
parents: 40684
diff changeset
    24
/*
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    25
 * @test
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    26
 * @bug 8153142
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    27
 * @modules jdk.incubator.httpclient
41103
c0b875b35b1e 8166285: Missing dependencies java.httpclient for tests from java/net pachage
skovalev
parents: 40684
diff changeset
    28
 *          jdk.httpserver
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    29
 * @run testng/othervm HeadersTest1
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    30
 */
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    31
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    32
import java.io.IOException;
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    33
import java.io.OutputStream;
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    34
import java.net.InetSocketAddress;
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    35
import java.net.URI;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    36
import jdk.incubator.http.HttpClient;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    37
import jdk.incubator.http.HttpHeaders;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    38
import jdk.incubator.http.HttpRequest;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    39
import jdk.incubator.http.HttpResponse;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    40
import java.util.HashSet;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    41
import java.util.List;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    42
import java.util.Map;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    43
import java.util.Set;
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    44
import java.util.concurrent.ExecutorService;
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    45
import java.util.concurrent.Executors;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    46
import com.sun.net.httpserver.Headers;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    47
import com.sun.net.httpserver.HttpExchange;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    48
import com.sun.net.httpserver.HttpHandler;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    49
import com.sun.net.httpserver.HttpServer;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    50
import org.testng.annotations.Test;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    51
import static jdk.incubator.http.HttpResponse.BodyHandler.asString;
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    52
import static java.nio.charset.StandardCharsets.US_ASCII;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    53
import static org.testng.Assert.assertEquals;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    54
import static org.testng.Assert.assertNotNull;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    55
import static org.testng.Assert.assertTrue;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    56
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    57
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    58
public class HeadersTest1 {
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    59
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    60
    private static final String RESPONSE = "Hello world";
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    61
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    62
    @Test
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    63
    public void test() throws Exception {
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    64
        HttpServer server = HttpServer.create(new InetSocketAddress(0), 10);
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    65
        Handler h = new Handler();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    66
        server.createContext("/test", h);
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    67
        int port = server.getAddress().getPort();
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    68
        System.out.println("Server port = " + port);
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    69
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    70
        ExecutorService e = Executors.newCachedThreadPool();
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    71
        server.setExecutor(e);
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    72
        server.start();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    73
        HttpClient client = HttpClient.newBuilder()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    74
                                      .executor(e)
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    75
                                      .build();
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    76
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    77
        try {
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    78
            URI uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/test/foo");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    79
            HttpRequest req = HttpRequest.newBuilder(uri)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    80
                                         .headers("X-Bar", "foo1")
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    81
                                         .headers("X-Bar", "foo2")
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    82
                                         .GET()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    83
                                         .build();
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    84
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    85
            HttpResponse<?> resp = client.send(req, asString());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    86
            if (resp.statusCode() != 200) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    87
                throw new RuntimeException("Expected 200, got: " + resp.statusCode());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    88
            }
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    89
            HttpHeaders hd = resp.headers();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    90
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    91
            assertTrue(!hd.firstValue("Non-Existent-Header").isPresent());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    92
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    93
            List<String> v1 = hd.allValues("Non-Existent-Header");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    94
            assertNotNull(v1);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    95
            assertTrue(v1.isEmpty(), String.valueOf(v1));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    96
            TestKit.assertUnmodifiableList(v1);
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
    97
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    98
            List<String> v2 = hd.allValues("X-Foo-Response");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    99
            assertNotNull(v2);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
   100
            assertEquals(new HashSet<>(v2), Set.of("resp1", "resp2"));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
   101
            TestKit.assertUnmodifiableList(v2);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
   102
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
   103
            Map<String, List<String>> map = hd.map();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
   104
            TestKit.assertUnmodifiableMap(map);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
   105
            for (List<String> values : map.values()) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
   106
                TestKit.assertUnmodifiableList(values);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
   107
            }
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   108
        } finally {
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   109
            server.stop(0);
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   110
            e.shutdownNow();
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   111
        }
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   112
        System.out.println("OK");
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   113
    }
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   114
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
   115
    private static final class Handler implements HttpHandler {
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   116
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   117
        @Override
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   118
        public void handle(HttpExchange he) throws IOException {
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   119
            List<String> l = he.getRequestHeaders().get("X-Bar");
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   120
            if (!l.contains("foo1") || !l.contains("foo2")) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
   121
                for (String s : l) {
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   122
                    System.out.println("HH: " + s);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
   123
                }
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   124
                he.sendResponseHeaders(500, -1);
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   125
                he.close();
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   126
                return;
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   127
            }
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   128
            Headers h = he.getResponseHeaders();
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   129
            h.add("X-Foo-Response", "resp1");
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   130
            h.add("X-Foo-Response", "resp2");
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   131
            he.sendResponseHeaders(200, RESPONSE.length());
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   132
            OutputStream os = he.getResponseBody();
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   133
            os.write(RESPONSE.getBytes(US_ASCII));
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   134
            os.close();
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   135
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
   136
    }
38465
587c3935e850 8153142: Cannot resolve multiple values from one response header
michaelm
parents:
diff changeset
   137
}