jdk/test/java/net/CookieHandler/LocalHostCookie.java
author mli
Sun, 12 Jun 2016 21:55:20 -0700
changeset 38883 d5de564f8089
parent 19826 460110102fd4
permissions -rw-r--r--
8151913: Fix module dependencies in java/net tests Summary: Module dependency declaration for java/net/* tests Reviewed-by: alanb Contributed-by: John Jiang <sha.jiang@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18174
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
     1
/*
38883
d5de564f8089 8151913: Fix module dependencies in java/net tests
mli
parents: 19826
diff changeset
     2
 * Copyright (c) 2013, 2016, 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;
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    31
import java.util.concurrent.Executors;
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
38883
d5de564f8089 8151913: Fix module dependencies in java/net tests
mli
parents: 19826
diff changeset
    36
 * @modules jdk.httpserver
18174
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    37
 * @summary CookieHandler does not work with localhost
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    38
 * @run main/othervm LocalHostCookie
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    39
 */
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    40
public class LocalHostCookie {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    41
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    42
    public static void main(String[] args) throws Exception {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    43
        new LocalHostCookie().runTest();
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
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    46
    public void runTest() throws Exception {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    47
        Server s = null;
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    48
        try {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    49
            s = new Server();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    50
            s.startServer();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    51
            URL url = new URL("http","localhost", s.getPort(), "/");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    52
            HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    53
            urlConnection.setRequestMethod("GET");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    54
            urlConnection.setDoOutput(true);
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    55
            urlConnection.connect();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    56
            urlConnection.getInputStream();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    57
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    58
            CookieHandler cookieHandler = CookieHandler.getDefault();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    59
            if (cookieHandler == null) {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    60
                cookieHandler = new java.net.CookieManager();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    61
                CookieHandler.setDefault(cookieHandler);
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    62
            }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    63
            cookieHandler.put(urlConnection.getURL().toURI(),
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    64
                    urlConnection.getHeaderFields());
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    65
            Map<String, List<String>> map =
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    66
                    cookieHandler.get(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
            if (map.containsKey("Cookie")) {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    69
                List<String> list = map.get("Cookie");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    70
                // name-value list will be empty if ".local" is not appended
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    71
                if (list == null || list.size() ==  0) {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    72
                    throw new RuntimeException("Test failed!");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    73
                }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    74
            }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    75
        } finally {
19826
460110102fd4 8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents: 18174
diff changeset
    76
            if (s != null) {
460110102fd4 8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents: 18174
diff changeset
    77
                s.stopServer();
460110102fd4 8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents: 18174
diff changeset
    78
            }
18174
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    79
        }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    80
    }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    81
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    82
    class Server {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    83
        HttpServer server;
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
        public void startServer() {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    86
            InetSocketAddress addr = new InetSocketAddress(0);
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    87
            try {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    88
                server = HttpServer.create(addr, 0);
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    89
            } catch (IOException ioe) {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    90
                throw new RuntimeException("Server could not be created");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    91
            }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    92
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    93
            server.createContext("/", new MyCookieHandler());
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    94
            server.start();
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
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    97
        public int getPort() {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    98
            return server.getAddress().getPort();
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
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   101
        public void stopServer() {
19826
460110102fd4 8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents: 18174
diff changeset
   102
            if (server != null) {
460110102fd4 8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents: 18174
diff changeset
   103
                server.stop(0);
460110102fd4 8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents: 18174
diff changeset
   104
            }
18174
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   105
        }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   106
    }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   107
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   108
    class MyCookieHandler implements HttpHandler {
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
        @Override
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   111
        public void handle(HttpExchange exchange) throws IOException {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   112
            String requestMethod = exchange.getRequestMethod();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   113
            if (requestMethod.equalsIgnoreCase("GET")){
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   114
                Headers responseHeaders = exchange.getResponseHeaders();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   115
                responseHeaders.set("Content-Type", "text/plain");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   116
                responseHeaders.set("Date", "June 13th 2012");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   117
                // No domain value set
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   118
                responseHeaders.set("Set-Cookie2", "name=value");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   119
                exchange.sendResponseHeaders(200, 0);
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   120
                OutputStream os = exchange.getResponseBody();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   121
                String str = "This is what the server sent!";
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   122
                os.write(str.getBytes());
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   123
                os.flush();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   124
                os.close();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   125
            }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   126
        }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   127
    }
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