Path: blob/master/test/jdk/sun/net/www/protocol/http/NoCache.java
41159 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 713336726* @modules jdk.httpserver27* @library /test/lib28* @summary ResponseCache.put should not be called when setUseCaches(false)29*/303132import java.net.*;33import java.io.IOException;34import java.util.List;35import java.util.Map;36import com.sun.net.httpserver.HttpExchange;37import com.sun.net.httpserver.HttpHandler;38import com.sun.net.httpserver.HttpServer;39import jdk.test.lib.net.URIBuilder;4041public class NoCache42{43public static void main(String[] args) throws IOException {44ResponseCache.setDefault(new ThrowingCache());4546HttpServer server = startHttpServer();47try {48URL url = URIBuilder.newBuilder()49.scheme("http")50.host(server.getAddress().getAddress())51.port(server.getAddress().getPort())52.path("/NoCache/")53.toURLUnchecked();54URLConnection uc = url.openConnection(Proxy.NO_PROXY);55uc.setUseCaches(false);56uc.getInputStream().close();57} finally {58server.stop(0);59// clear the system-wide cache handler, samevm/agentvm mode60ResponseCache.setDefault(null);61}62}6364static class ThrowingCache extends ResponseCache {65@Override66public CacheResponse get(URI uri, String rqstMethod,67Map<String,List<String>> rqstHeaders) {68throw new RuntimeException("ResponseCache.get should not be called");69}7071@Override72public CacheRequest put(URI uri, URLConnection conn) {73throw new RuntimeException("ResponseCache.put should not be called");74}75}7677// HTTP Server78static HttpServer startHttpServer() throws IOException {79HttpServer httpServer = HttpServer.create(new InetSocketAddress(InetAddress.getLocalHost(), 0), 0);80httpServer.createContext("/NoCache/", new SimpleHandler());81httpServer.start();82return httpServer;83}8485static class SimpleHandler implements HttpHandler {86@Override87public void handle(HttpExchange t) throws IOException {88t.sendResponseHeaders(200, -1);89t.close();90}91}92}939495