Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/6550798/test.java
38889 views
/*1* Copyright (c) 2010, 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 655079826* @summary Using InputStream.skip with ResponseCache will cause partial data to be cached27* @run main/othervm test28*/2930import java.net.*;31import com.sun.net.httpserver.*;32import java.io.*;3334public class test {3536final static int LEN = 16 * 1024;3738public static void main(String[] args) throws Exception {3940TestCache.reset();41HttpServer s = HttpServer.create (new InetSocketAddress(0), 10);42s.createContext ("/", new HttpHandler () {43public void handle (HttpExchange e) {44try {45byte[] buf = new byte [LEN];46OutputStream o = e.getResponseBody();47e.sendResponseHeaders(200, LEN);48o.write (buf);49e.close();50} catch (IOException ex) {51ex.printStackTrace();52TestCache.fail = true;53}54}55});56s.start();5758System.out.println("http request with cache hander");59URL u = new URL("http://127.0.0.1:"+s.getAddress().getPort()+"/f");60URLConnection conn = u.openConnection();6162InputStream is = null;63try {64// this calls into TestCache.get65byte[] buf = new byte[8192];66is = new BufferedInputStream(conn.getInputStream());6768is.skip(1000);6970while (is.read(buf) != -1) {71}72} finally {73if (is != null) {74// this calls into TestCache.put75// TestCache.put will check if the resource76// should be cached77is.close();78}79s.stop(0);80}8182if (TestCache.fail) {83System.out.println ("TEST FAILED");84throw new RuntimeException ();85} else {86System.out.println ("TEST OK");87}88}89}909192