Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/CookieHandler/EmptyCookieHeader.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*/2223/*24* @test25* @bug 801579926* @summary HttpURLConnection.getHeaderFields() throws IllegalArgumentException27*/2829import com.sun.net.httpserver.*;30import java.io.IOException;31import java.io.OutputStream;32import java.net.*;33import java.util.*;3435public class EmptyCookieHeader {3637public static void main(String[] args) throws Exception {38new EmptyCookieHeader().runTest();39}4041public void runTest() throws Exception {42final CookieHandler oldHandler = CookieHandler.getDefault();43CookieHandler.setDefault(new TestCookieHandler());44HttpServer s = HttpServer.create(new InetSocketAddress(0), 0);45try {46startServer(s);47URL url = new URL("http://localhost:" + s.getAddress().getPort() + "/");48HttpURLConnection c = (HttpURLConnection)url.openConnection();49c.getHeaderFields();50} finally {51CookieHandler.setDefault(oldHandler);52s.stop(0);53}54}5556static void startServer(HttpServer server) throws IOException {57server.createContext("/", new EmptyCookieHandler());58server.start();59}6061static class EmptyCookieHandler implements HttpHandler {6263@Override64public void handle(HttpExchange exchange) throws IOException {65String requestMethod = exchange.getRequestMethod();66if (requestMethod.equalsIgnoreCase("GET")) {67Headers responseHeaders = exchange.getResponseHeaders();68responseHeaders.set("Content-Type", "text/plain");69responseHeaders.set("Date", "June 13th 2012");7071// No domain value set72responseHeaders.set("Set-Cookie2", "name=value");73responseHeaders.set("Set-Cookie2", "");74exchange.sendResponseHeaders(200, 0);75try (OutputStream os = exchange.getResponseBody()) {76String str = "This is what the server sent!";77os.write(str.getBytes());78}79}80}81}8283class TestCookieHandler extends CookieHandler {84@Override85public Map<String,List<String>> get(URI uri,86Map<String,List<String>> respH) {87return new HashMap<>();88}8990@Override91public void put(URI uri, Map<String,List<String >> respH) { }92}93}949596