test/jdk/java/net/CookieHandler/LocalHostCookie.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58365 73950479184b
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18174
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
     1
/*
58365
73950479184b 8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents: 55399
diff changeset
     2
 * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
18174
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
     4
 *
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
     7
 * published by the Free Software Foundation.
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
     8
 *
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    13
 * accompanied this code).
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    14
 *
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    18
 *
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    21
 * questions.
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    22
 */
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    23
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    24
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    25
import com.sun.net.httpserver.*;
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    26
import java.io.IOException;
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    27
import java.io.OutputStream;
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    28
import java.net.*;
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    29
import java.util.List;
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    30
import java.util.Map;
58365
73950479184b 8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents: 55399
diff changeset
    31
import static java.net.Proxy.NO_PROXY;
18174
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    32
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    33
/*
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    34
 * @test
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    35
 * @bug 7169142
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
    36
 * @key intermittent
38883
d5de564f8089 8151913: Fix module dependencies in java/net tests
mli
parents: 19826
diff changeset
    37
 * @modules jdk.httpserver
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
    38
 * @summary CookieHandler does not work with localhost. This requires
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
    39
 *    binding to the wildcard address and might fail intermittently
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
    40
 *    due to port reuse issues.
18174
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    41
 * @run main/othervm LocalHostCookie
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    42
 */
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    43
public class LocalHostCookie {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    44
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    45
    public static void main(String[] args) throws Exception {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    46
        new LocalHostCookie().runTest();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    47
    }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    48
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    49
    public void runTest() throws Exception {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    50
        Server s = null;
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    51
        try {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    52
            s = new Server();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    53
            s.startServer();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    54
            URL url = new URL("http","localhost", s.getPort(), "/");
58365
73950479184b 8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents: 55399
diff changeset
    55
            HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(NO_PROXY);
18174
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    56
            urlConnection.setRequestMethod("GET");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    57
            urlConnection.setDoOutput(true);
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    58
            urlConnection.connect();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    59
            urlConnection.getInputStream();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    60
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    61
            CookieHandler cookieHandler = CookieHandler.getDefault();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    62
            if (cookieHandler == null) {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    63
                cookieHandler = new java.net.CookieManager();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    64
                CookieHandler.setDefault(cookieHandler);
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    65
            }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    66
            cookieHandler.put(urlConnection.getURL().toURI(),
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    67
                    urlConnection.getHeaderFields());
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    68
            Map<String, List<String>> map =
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    69
                    cookieHandler.get(urlConnection.getURL().toURI(),
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    70
                    urlConnection.getHeaderFields());
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    71
            if (map.containsKey("Cookie")) {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    72
                List<String> list = map.get("Cookie");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    73
                // name-value list will be empty if ".local" is not appended
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    74
                if (list == null || list.size() ==  0) {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    75
                    throw new RuntimeException("Test failed!");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    76
                }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    77
            }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    78
        } finally {
19826
460110102fd4 8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents: 18174
diff changeset
    79
            if (s != null) {
460110102fd4 8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents: 18174
diff changeset
    80
                s.stopServer();
460110102fd4 8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents: 18174
diff changeset
    81
            }
18174
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    82
        }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    83
    }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    84
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    85
    class Server {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    86
        HttpServer server;
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    87
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    88
        public void startServer() {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    89
            InetSocketAddress addr = new InetSocketAddress(0);
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    90
            try {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    91
                server = HttpServer.create(addr, 0);
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    92
            } catch (IOException ioe) {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    93
                throw new RuntimeException("Server could not be created");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    94
            }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    95
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    96
            server.createContext("/", new MyCookieHandler());
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    97
            server.start();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    98
        }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    99
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   100
        public int getPort() {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   101
            return server.getAddress().getPort();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   102
        }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   103
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   104
        public void stopServer() {
19826
460110102fd4 8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents: 18174
diff changeset
   105
            if (server != null) {
460110102fd4 8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents: 18174
diff changeset
   106
                server.stop(0);
460110102fd4 8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents: 18174
diff changeset
   107
            }
18174
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   108
        }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   109
    }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   110
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   111
    class MyCookieHandler implements HttpHandler {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   112
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   113
        @Override
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   114
        public void handle(HttpExchange exchange) throws IOException {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   115
            String requestMethod = exchange.getRequestMethod();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   116
            if (requestMethod.equalsIgnoreCase("GET")){
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   117
                Headers responseHeaders = exchange.getResponseHeaders();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   118
                responseHeaders.set("Content-Type", "text/plain");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   119
                responseHeaders.set("Date", "June 13th 2012");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   120
                // No domain value set
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   121
                responseHeaders.set("Set-Cookie2", "name=value");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   122
                exchange.sendResponseHeaders(200, 0);
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   123
                OutputStream os = exchange.getResponseBody();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   124
                String str = "This is what the server sent!";
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   125
                os.write(str.getBytes());
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   126
                os.flush();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   127
                os.close();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   128
            }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   129
        }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   130
    }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   131
}