Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/UserCookie.java
38867 views
/*1* Copyright (c) 2006, 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 643965126* @run main/othervm UserAuth27* @summary Sending "Cookie" header with JRE 1.5.0_07 doesn't work anymore28*/2930import java.net.*;31import com.sun.net.httpserver.*;32import java.util.*;33import java.io.*;3435public class UserCookie36{37com.sun.net.httpserver.HttpServer httpServer;3839public static void main(String[] args) {40new UserCookie();41}4243public UserCookie() {44try {45startHttpServer();46doClient();47} catch (IOException ioe) {48ioe.printStackTrace();49}50}5152void doClient() {53try {54// set default CookieHandler to accept only accepts cookies from original server.55CookieHandler.setDefault(new CookieManager());5657InetSocketAddress address = httpServer.getAddress();5859URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/");60HttpURLConnection uc = (HttpURLConnection)url.openConnection();61uc.setRequestProperty("Cookie", "value=ValueDoesNotMatter");62int resp = uc.getResponseCode();6364System.out.println("Response Code is " + resp);65if (resp != 200)66throw new RuntimeException("Failed: Cookie header was not retained");6768} catch (IOException e) {69e.printStackTrace();70} finally {71httpServer.stop(1);72}73}7475/**76* Http Server77*/78void startHttpServer() throws IOException {79httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);8081// create HttpServer context82HttpContext ctx = httpServer.createContext("/test/", new MyHandler());8384httpServer.start();85}8687class MyHandler implements HttpHandler {88public void handle(HttpExchange t) throws IOException {89Headers reqHeaders = t.getRequestHeaders();9091List<String> cookie = reqHeaders.get("Cookie");9293if (cookie == null || !cookie.get(0).equals("value=ValueDoesNotMatter"))94t.sendResponseHeaders(400, -1);9596t.sendResponseHeaders(200, -1);97t.close();98}99}100101102103}104105106