Path: blob/master/test/jdk/java/net/CookieHandler/LocalHostCookie.java
41152 views
/*1* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324import com.sun.net.httpserver.*;25import java.io.IOException;26import java.io.OutputStream;27import java.net.*;28import java.util.List;29import java.util.Map;30import static java.net.Proxy.NO_PROXY;3132/*33* @test34* @bug 716914235* @key intermittent36* @modules jdk.httpserver37* @summary CookieHandler does not work with localhost. This requires38* binding to the wildcard address and might fail intermittently39* due to port reuse issues.40* @run main/othervm LocalHostCookie41*/42public class LocalHostCookie {4344public static void main(String[] args) throws Exception {45new LocalHostCookie().runTest();46}4748public void runTest() throws Exception {49Server s = null;50try {51s = new Server();52s.startServer();53URL url = new URL("http","localhost", s.getPort(), "/");54HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(NO_PROXY);55urlConnection.setRequestMethod("GET");56urlConnection.setDoOutput(true);57urlConnection.connect();58urlConnection.getInputStream();5960CookieHandler cookieHandler = CookieHandler.getDefault();61if (cookieHandler == null) {62cookieHandler = new java.net.CookieManager();63CookieHandler.setDefault(cookieHandler);64}65cookieHandler.put(urlConnection.getURL().toURI(),66urlConnection.getHeaderFields());67Map<String, List<String>> map =68cookieHandler.get(urlConnection.getURL().toURI(),69urlConnection.getHeaderFields());70if (map.containsKey("Cookie")) {71List<String> list = map.get("Cookie");72// name-value list will be empty if ".local" is not appended73if (list == null || list.size() == 0) {74throw new RuntimeException("Test failed!");75}76}77} finally {78if (s != null) {79s.stopServer();80}81}82}8384class Server {85HttpServer server;8687public void startServer() {88InetSocketAddress addr = new InetSocketAddress(0);89try {90server = HttpServer.create(addr, 0);91} catch (IOException ioe) {92throw new RuntimeException("Server could not be created");93}9495server.createContext("/", new MyCookieHandler());96server.start();97}9899public int getPort() {100return server.getAddress().getPort();101}102103public void stopServer() {104if (server != null) {105server.stop(0);106}107}108}109110class MyCookieHandler implements HttpHandler {111112@Override113public void handle(HttpExchange exchange) throws IOException {114String requestMethod = exchange.getRequestMethod();115if (requestMethod.equalsIgnoreCase("GET")){116Headers responseHeaders = exchange.getResponseHeaders();117responseHeaders.set("Content-Type", "text/plain");118responseHeaders.set("Date", "June 13th 2012");119// No domain value set120responseHeaders.set("Set-Cookie2", "name=value");121exchange.sendResponseHeaders(200, 0);122OutputStream os = exchange.getResponseBody();123String str = "This is what the server sent!";124os.write(str.getBytes());125os.flush();126os.close();127}128}129}130}131132133