Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/HttpClient/CookieHttpClientTest.java
38867 views
/*1* Copyright (c) 2012, 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 712908326* @summary Cookiemanager does not store cookies if url is read27* before setting cookiemanager28*/2930import java.net.CookieHandler;31import java.net.CookieManager;32import java.net.CookiePolicy;33import java.net.ServerSocket;34import java.net.Socket;35import java.net.URL;36import java.io.InputStream;37import java.io.IOException;3839public class CookieHttpClientTest implements Runnable {40final ServerSocket ss;41static final int TIMEOUT = 10 * 1000;4243static final String replyString = "HTTP/1.1 200 OK\r\n" +44"Set-Cookie: name=test\r\n" +45"Content-Length: 10\r\n\r\n" +46"1234567890";4748// HTTP server, reply with Set-Cookie49@Override50public void run() {51Socket s = null;52try {53s = ss.accept();54s.setSoTimeout(TIMEOUT);55readOneRequest(s.getInputStream());56s.getOutputStream().write(replyString.getBytes());5758readOneRequest(s.getInputStream());59s.getOutputStream().write(replyString.getBytes());60} catch (Exception e) {61e.printStackTrace();62} finally {63try { if (s != null) { s.close(); } ss.close(); }64catch (IOException unused) { /* gulp!burp! */ }65}66}6768static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n' };6970// Read until the end of a HTTP request71static void readOneRequest(InputStream is) throws IOException {72int requestEndCount = 0, r;73while ((r = is.read()) != -1) {74if (r == requestEnd[requestEndCount]) {75requestEndCount++;76if (requestEndCount == 4) {77break;78}79} else {80requestEndCount = 0;81}82}83}8485CookieHttpClientTest() throws Exception {86/* start the server */87ss = new ServerSocket(0);88(new Thread(this)).start();8990URL url = new URL("http://localhost:" + ss.getLocalPort() +"/");9192// Run without a CookieHandler first93InputStream in = url.openConnection().getInputStream();94while (in.read() != -1); // read response body so connection can be reused9596// Set a CookeHandler and retest using the HttpClient from the KAC97CookieManager manager = new CookieManager(null, CookiePolicy.ACCEPT_ALL);98CookieHandler.setDefault(manager);99100in = url.openConnection().getInputStream();101while (in.read() != -1);102103if (manager.getCookieStore().getCookies().isEmpty()) {104throw new RuntimeException("Failed: No cookies in the cookie Handler.");105}106}107108public static void main(String args[]) throws Exception {109new CookieHttpClientTest();110}111}112113114