Path: blob/master/test/jdk/sun/net/www/http/HttpClient/CookieHttpClientTest.java
41154 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/*24* @test25* @bug 712908326* @library /test/lib27* @summary Cookiemanager does not store cookies if url is read28* before setting cookiemanager29*/3031import java.net.CookieHandler;32import java.net.CookieManager;33import java.net.CookiePolicy;34import java.net.InetAddress;35import java.net.InetSocketAddress;36import java.net.ServerSocket;37import java.net.Socket;38import java.net.URL;39import java.io.InputStream;40import java.io.IOException;4142import jdk.test.lib.net.URIBuilder;4344public class CookieHttpClientTest implements Runnable {45final ServerSocket ss;46static final int TIMEOUT = 10 * 1000;4748static final String replyString = "HTTP/1.1 200 OK\r\n" +49"Set-Cookie: name=test\r\n" +50"Content-Length: 10\r\n\r\n" +51"1234567890";5253// HTTP server, reply with Set-Cookie54@Override55public void run() {56Socket s = null;57try {58s = ss.accept();59s.setSoTimeout(TIMEOUT);60readOneRequest(s.getInputStream());61s.getOutputStream().write(replyString.getBytes());6263readOneRequest(s.getInputStream());64s.getOutputStream().write(replyString.getBytes());65} catch (Exception e) {66e.printStackTrace();67} finally {68try { if (s != null) { s.close(); } ss.close(); }69catch (IOException unused) { /* gulp!burp! */ }70}71}7273static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n' };7475// Read until the end of a HTTP request76static void readOneRequest(InputStream is) throws IOException {77int requestEndCount = 0, r;78while ((r = is.read()) != -1) {79if (r == requestEnd[requestEndCount]) {80requestEndCount++;81if (requestEndCount == 4) {82break;83}84} else {85requestEndCount = 0;86}87}88}8990CookieHttpClientTest() throws Exception {91/* start the server */92ss = new ServerSocket();93ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));94(new Thread(this)).start();9596URL url = URIBuilder.newBuilder()97.scheme("http")98.loopback()99.port(ss.getLocalPort())100.path("/").toURL();101102// Run without a CookieHandler first103InputStream in = url.openConnection().getInputStream();104while (in.read() != -1); // read response body so connection can be reused105106// Set a CookeHandler and retest using the HttpClient from the KAC107CookieManager manager = new CookieManager(null, CookiePolicy.ACCEPT_ALL);108CookieHandler.setDefault(manager);109110in = url.openConnection().getInputStream();111while (in.read() != -1);112113if (manager.getCookieStore().getCookies().isEmpty()) {114throw new RuntimeException("Failed: No cookies in the cookie Handler.");115}116}117118public static void main(String args[]) throws Exception {119new CookieHttpClientTest();120}121}122123124