jdk/test/java/net/CookieHandler/EmptyCookieHeader.java
author jjg
Wed, 04 Jan 2017 18:33:20 -0800
changeset 43029 1cd1c816581e
parent 38883 d5de564f8089
permissions -rw-r--r--
8172260: remove tests from ProblemList Reviewed-by: rfield
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18577
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
     1
/*
38883
d5de564f8089 8151913: Fix module dependencies in java/net tests
mli
parents: 18577
diff changeset
     2
 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
18577
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
     4
 *
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
     8
 *
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    13
 * accompanied this code).
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    14
 *
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    18
 *
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    21
 * questions.
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    22
 */
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    23
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    24
/*
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    25
 * @test
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    26
 * @bug 8015799
38883
d5de564f8089 8151913: Fix module dependencies in java/net tests
mli
parents: 18577
diff changeset
    27
 * @modules jdk.httpserver
18577
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    28
 * @summary HttpURLConnection.getHeaderFields() throws IllegalArgumentException
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    29
 */
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    30
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    31
import com.sun.net.httpserver.*;
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    32
import java.io.IOException;
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    33
import java.io.OutputStream;
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    34
import java.net.*;
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    35
import java.util.*;
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    36
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    37
public class EmptyCookieHeader {
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    38
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    39
    public static void main(String[] args) throws Exception {
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    40
        new EmptyCookieHeader().runTest();
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    41
    }
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    42
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    43
    public void runTest() throws Exception {
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    44
        final CookieHandler oldHandler = CookieHandler.getDefault();
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    45
        CookieHandler.setDefault(new TestCookieHandler());
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    46
        HttpServer s = HttpServer.create(new InetSocketAddress(0), 0);
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    47
        try {
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    48
            startServer(s);
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    49
            URL url = new URL("http://localhost:" + s.getAddress().getPort() + "/");
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    50
            HttpURLConnection c = (HttpURLConnection)url.openConnection();
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    51
            c.getHeaderFields();
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    52
        } finally {
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    53
            CookieHandler.setDefault(oldHandler);
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    54
            s.stop(0);
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    55
        }
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    56
    }
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    57
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    58
    static void startServer(HttpServer server) throws IOException {
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    59
        server.createContext("/", new EmptyCookieHandler());
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    60
        server.start();
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    61
    }
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    62
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    63
    static class EmptyCookieHandler implements HttpHandler {
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    64
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    65
        @Override
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    66
        public void handle(HttpExchange exchange) throws IOException {
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    67
            String requestMethod = exchange.getRequestMethod();
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    68
            if (requestMethod.equalsIgnoreCase("GET")) {
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    69
                Headers responseHeaders = exchange.getResponseHeaders();
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    70
                responseHeaders.set("Content-Type", "text/plain");
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    71
                responseHeaders.set("Date", "June 13th 2012");
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    72
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    73
                // No domain value set
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    74
                responseHeaders.set("Set-Cookie2", "name=value");
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    75
                responseHeaders.set("Set-Cookie2", "");
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    76
                exchange.sendResponseHeaders(200, 0);
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    77
                try (OutputStream os = exchange.getResponseBody()) {
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    78
                    String str = "This is what the server sent!";
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    79
                    os.write(str.getBytes());
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    80
                }
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    81
            }
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    82
        }
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    83
    }
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    84
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    85
    class TestCookieHandler extends CookieHandler {
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    86
        @Override
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    87
        public Map<String,List<String>> get(URI uri,
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    88
                                            Map<String,List<String>> respH) {
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    89
            return new HashMap<>();
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    90
        }
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    91
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    92
        @Override
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    93
        public void put(URI uri, Map<String,List<String >> respH) { }
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    94
    }
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents:
diff changeset
    95
}