Path: blob/master/test/jdk/java/net/CookieHandler/EmptyCookieHeader.java
41149 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*/2223/*24* @test25* @bug 801579926* @modules jdk.httpserver27* @summary HttpURLConnection.getHeaderFields() throws IllegalArgumentException28* @library /test/lib29* @run main EmptyCookieHeader30* @run main/othervm -Djava.net.preferIPv6Addresses=true EmptyCookieHeader31*/3233import com.sun.net.httpserver.*;34import java.io.IOException;35import java.io.OutputStream;36import java.net.*;37import java.util.*;38import jdk.test.lib.net.URIBuilder;3940public class EmptyCookieHeader {4142public static void main(String[] args) throws Exception {43new EmptyCookieHeader().runTest();44}4546public void runTest() throws Exception {47final CookieHandler oldHandler = CookieHandler.getDefault();48CookieHandler.setDefault(new TestCookieHandler());49InetAddress loopback = InetAddress.getLoopbackAddress();50HttpServer s = HttpServer.create(new InetSocketAddress(loopback, 0), 0);51try {52startServer(s);53URL url = URIBuilder.newBuilder()54.scheme("http")55.loopback()56.port(s.getAddress().getPort())57.path("/")58.toURL();59HttpURLConnection c = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);60c.getHeaderFields();61} finally {62CookieHandler.setDefault(oldHandler);63s.stop(0);64}65}6667static void startServer(HttpServer server) throws IOException {68server.createContext("/", new EmptyCookieHandler());69server.start();70}7172static class EmptyCookieHandler implements HttpHandler {7374@Override75public void handle(HttpExchange exchange) throws IOException {76String requestMethod = exchange.getRequestMethod();77if (requestMethod.equalsIgnoreCase("GET")) {78Headers responseHeaders = exchange.getResponseHeaders();79responseHeaders.set("Content-Type", "text/plain");80responseHeaders.set("Date", "June 13th 2012");8182// No domain value set83responseHeaders.set("Set-Cookie2", "name=value");84responseHeaders.set("Set-Cookie2", "");85exchange.sendResponseHeaders(200, 0);86try (OutputStream os = exchange.getResponseBody()) {87String str = "This is what the server sent!";88os.write(str.getBytes());89}90}91}92}9394class TestCookieHandler extends CookieHandler {95@Override96public Map<String,List<String>> get(URI uri,97Map<String,List<String>> respH) {98return new HashMap<>();99}100101@Override102public void put(URI uri, Map<String,List<String >> respH) { }103}104}105106107