Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/CookieHandler/LocalHostCookie.java
38812 views
/*1* Copyright (c) 2013, 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 java.util.concurrent.Executors;3132/*33* @test34* @bug 716914235* @summary CookieHandler does not work with localhost36* @run main/othervm LocalHostCookie37*/38public class LocalHostCookie {3940public static void main(String[] args) throws Exception {41new LocalHostCookie().runTest();42}4344public void runTest() throws Exception {45Server s = null;46try {47s = new Server();48s.startServer();49URL url = new URL("http","localhost", s.getPort(), "/");50HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();51urlConnection.setRequestMethod("GET");52urlConnection.setDoOutput(true);53urlConnection.connect();54urlConnection.getInputStream();5556CookieHandler cookieHandler = CookieHandler.getDefault();57if (cookieHandler == null) {58cookieHandler = new java.net.CookieManager();59CookieHandler.setDefault(cookieHandler);60}61cookieHandler.put(urlConnection.getURL().toURI(),62urlConnection.getHeaderFields());63Map<String, List<String>> map =64cookieHandler.get(urlConnection.getURL().toURI(),65urlConnection.getHeaderFields());66if (map.containsKey("Cookie")) {67List<String> list = map.get("Cookie");68// name-value list will be empty if ".local" is not appended69if (list == null || list.size() == 0) {70throw new RuntimeException("Test failed!");71}72}73} finally {74if (s != null) {75s.stopServer();76}77}78}7980class Server {81HttpServer server;8283public void startServer() {84InetSocketAddress addr = new InetSocketAddress(0);85try {86server = HttpServer.create(addr, 0);87} catch (IOException ioe) {88throw new RuntimeException("Server could not be created");89}9091server.createContext("/", new MyCookieHandler());92server.start();93}9495public int getPort() {96return server.getAddress().getPort();97}9899public void stopServer() {100if (server != null) {101server.stop(0);102}103}104}105106class MyCookieHandler implements HttpHandler {107108@Override109public void handle(HttpExchange exchange) throws IOException {110String requestMethod = exchange.getRequestMethod();111if (requestMethod.equalsIgnoreCase("GET")){112Headers responseHeaders = exchange.getResponseHeaders();113responseHeaders.set("Content-Type", "text/plain");114responseHeaders.set("Date", "June 13th 2012");115// No domain value set116responseHeaders.set("Set-Cookie2", "name=value");117exchange.sendResponseHeaders(200, 0);118OutputStream os = exchange.getResponseBody();119String str = "This is what the server sent!";120os.write(str.getBytes());121os.flush();122os.close();123}124}125}126}127128129130