Path: blob/master/test/jdk/java/net/HttpCookie/IllegalCookieNameTest.java
41152 views
/*1* Copyright (c) 2012, 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/* @test24* @bug 718329225* @library /test/lib26* @modules jdk.httpserver27* @run main IllegalCookieNameTest28* @run main/othervm -Djava.net.preferIPv6Addresses=true IllegalCookieNameTest29*/30import java.net.*;31import java.util.*;32import java.io.*;33import com.sun.net.httpserver.*;34import jdk.test.lib.net.URIBuilder;3536public class IllegalCookieNameTest {37public static void main(String[] args) throws Exception {38HttpServer s = null;39try {40InetAddress loopback = InetAddress.getLoopbackAddress();41InetSocketAddress addr = new InetSocketAddress(loopback, 0);42s = HttpServer.create(addr, 10);43s.createContext("/", new HHandler());44s.start();45String u = URIBuilder.newBuilder()46.scheme("http")47.loopback()48.port(s.getAddress().getPort())49.path("/")50.build().toString();51CookieHandler.setDefault(new TestCookieHandler());52URL url = new URL(u);53HttpURLConnection c = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);54c.getHeaderFields();55System.out.println ("OK");56} finally {57s.stop(1);58}59}60}6162class TestCookieHandler extends CookieHandler {63@Override64public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) {65return new HashMap<String, List<String>>();66}6768@Override69public void put(URI uri, Map<String, List<String>> responseHeaders) {70}71}7273class HHandler implements HttpHandler {74public void handle (HttpExchange e) {75try {76Headers h = e.getResponseHeaders();77h.set ("Set-Cookie", "domain=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=.foo.com");78e.sendResponseHeaders(200, -1);79e.close();80} catch (Exception ex) {81System.out.println (ex);82}83}84}858687