Path: blob/master/test/jdk/sun/net/www/protocol/http/6550798/test.java
41161 views
/*1* Copyright (c) 2010, 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 655079826* @library /test/lib27* @summary Using InputStream.skip with ResponseCache will cause partial data to be cached28* @modules jdk.httpserver29* @run main/othervm test30*/3132import java.net.*;33import com.sun.net.httpserver.*;34import java.io.*;3536import jdk.test.lib.net.URIBuilder;3738public class test {3940final static int LEN = 16 * 1024;4142public static void main(String[] args) throws Exception {4344TestCache.reset();45InetAddress loopback = InetAddress.getLoopbackAddress();46HttpServer s = HttpServer.create(new InetSocketAddress(loopback, 0), 10);47s.createContext("/", new HttpHandler() {48public void handle(HttpExchange e) {49try {50byte[] buf = new byte [LEN];51OutputStream o = e.getResponseBody();52e.sendResponseHeaders(200, LEN);53o.write(buf);54e.close();55} catch (IOException ex) {56ex.printStackTrace();57TestCache.fail = true;58}59}60});61s.start();6263System.out.println("http request with cache hander");64URL u = URIBuilder.newBuilder()65.scheme("http")66.loopback()67.port(s.getAddress().getPort())68.path("/f")69.toURL();70System.out.println("URL: " + u);71URLConnection conn = u.openConnection();7273InputStream is = null;74try {75// this calls into TestCache.get76byte[] buf = new byte[8192];77is = new BufferedInputStream(conn.getInputStream());7879is.skip(1000);8081while (is.read(buf) != -1) {82}83} finally {84if (is != null) {85// this calls into TestCache.put86// TestCache.put will check if the resource87// should be cached88is.close();89}90s.stop(0);91}9293if (TestCache.fail) {94System.out.println("TEST FAILED");95throw new RuntimeException();96} else {97System.out.println("TEST OK");98}99}100}101102103