jdk/test/java/net/CookieHandler/LocalHostCookie.java
author mikael
Fri, 04 Dec 2015 15:08:49 -0800
changeset 35090 1f5b6aa795d0
parent 19826 460110102fd4
child 38883 d5de564f8089
permissions -rw-r--r--
8144748: Move assembler/macroAssembler inline function definitions to corresponding inline.hpp files Reviewed-by: kvn, coleenp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18174
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
     1
/*
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
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
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    36
 * @summary CookieHandler does not work with localhost
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    37
 * @run main/othervm LocalHostCookie
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    38
 */
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    39
public class LocalHostCookie {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    40
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    41
    public static void main(String[] args) throws Exception {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    42
        new LocalHostCookie().runTest();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    43
    }
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 void runTest() throws Exception {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    46
        Server s = null;
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    47
        try {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    48
            s = new Server();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    49
            s.startServer();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    50
            URL url = new URL("http","localhost", s.getPort(), "/");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    51
            HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    52
            urlConnection.setRequestMethod("GET");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    53
            urlConnection.setDoOutput(true);
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    54
            urlConnection.connect();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    55
            urlConnection.getInputStream();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    56
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    57
            CookieHandler cookieHandler = CookieHandler.getDefault();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    58
            if (cookieHandler == null) {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    59
                cookieHandler = new java.net.CookieManager();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    60
                CookieHandler.setDefault(cookieHandler);
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    61
            }
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    62
            cookieHandler.put(urlConnection.getURL().toURI(),
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    63
                    urlConnection.getHeaderFields());
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    64
            Map<String, List<String>> map =
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    65
                    cookieHandler.get(urlConnection.getURL().toURI(),
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    66
                    urlConnection.getHeaderFields());
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    67
            if (map.containsKey("Cookie")) {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    68
                List<String> list = map.get("Cookie");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    69
                // name-value list will be empty if ".local" is not appended
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    70
                if (list == null || list.size() ==  0) {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    71
                    throw new RuntimeException("Test failed!");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    72
                }
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
        } finally {
19826
460110102fd4 8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents: 18174
diff changeset
    75
            if (s != null) {
460110102fd4 8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents: 18174
diff changeset
    76
                s.stopServer();
460110102fd4 8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents: 18174
diff changeset
    77
            }
18174
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    78
        }
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
    class Server {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    82
        HttpServer server;
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
        public void startServer() {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    85
            InetSocketAddress addr = new InetSocketAddress(0);
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    86
            try {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    87
                server = HttpServer.create(addr, 0);
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    88
            } catch (IOException ioe) {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    89
                throw new RuntimeException("Server could not be created");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    90
            }
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
            server.createContext("/", new MyCookieHandler());
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    93
            server.start();
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
        public int getPort() {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
    97
            return server.getAddress().getPort();
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 void stopServer() {
19826
460110102fd4 8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents: 18174
diff changeset
   101
            if (server != null) {
460110102fd4 8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents: 18174
diff changeset
   102
                server.stop(0);
460110102fd4 8023326: [TESTBUG] java/net/CookieHandler/LocalHostCookie.java misplaced try/finally
msheppar
parents: 18174
diff changeset
   103
            }
18174
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   104
        }
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
    class MyCookieHandler implements HttpHandler {
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
        @Override
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   110
        public void handle(HttpExchange exchange) throws IOException {
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   111
            String requestMethod = exchange.getRequestMethod();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   112
            if (requestMethod.equalsIgnoreCase("GET")){
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   113
                Headers responseHeaders = exchange.getResponseHeaders();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   114
                responseHeaders.set("Content-Type", "text/plain");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   115
                responseHeaders.set("Date", "June 13th 2012");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   116
                // No domain value set
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   117
                responseHeaders.set("Set-Cookie2", "name=value");
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   118
                exchange.sendResponseHeaders(200, 0);
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   119
                OutputStream os = exchange.getResponseBody();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   120
                String str = "This is what the server sent!";
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   121
                os.write(str.getBytes());
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   122
                os.flush();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   123
                os.close();
806bf0d6f0c7 7169142: CookieHandler does not work with localhost
khazra
parents:
diff changeset
   124
            }
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