Path: blob/master/test/jdk/sun/net/www/protocol/http/B6296310.java
66646 views
/*1* Copyright (c) 2005, 2021, 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 629631026* @library /test/lib27* @run main/othervm B629631028* @run main/othervm -Djava.net.preferIPv6Addresses=true B629631029* @summary REGRESSION: AppletClassLoader.getResourceAsStream() behaviour is wrong in some cases30*/3132import java.io.IOException;33import java.io.OutputStream;34import java.net.CacheRequest;35import java.net.CacheResponse;36import java.net.HttpURLConnection;37import java.net.InetAddress;38import java.net.InetSocketAddress;39import java.net.Proxy;40import java.net.ResponseCache;41import java.net.URI;42import java.net.URL;43import java.net.URLConnection;44import java.util.Map;45import java.util.concurrent.Executors;4647import com.sun.net.httpserver.HttpExchange;48import com.sun.net.httpserver.HttpHandler;49import com.sun.net.httpserver.HttpServer;5051/*52* http server returns 200 and content-length=053* Test will throw NPE if bug still exists54*/5556public class B629631057{58static SimpleHttpTransaction httpTrans;59static HttpServer server;6061public static void main(String[] args) throws Exception62{63ResponseCache.setDefault(new MyCacheHandler());64startHttpServer();65makeHttpCall();66}6768public static void startHttpServer() throws IOException {69httpTrans = new SimpleHttpTransaction();70InetAddress loopback = InetAddress.getLoopbackAddress();71server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);72server.createContext("/", httpTrans);73server.setExecutor(Executors.newSingleThreadExecutor());74server.start();75}7677public static void makeHttpCall() throws IOException {78try {79System.out.println("http server listen on: " + server.getAddress().getPort());80URL url = new URL("http" , InetAddress.getLoopbackAddress().getHostAddress(),81server.getAddress().getPort(), "/");82HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);83System.out.println(uc.getResponseCode());84} finally {85server.stop(1);86}87}88}8990class SimpleHttpTransaction implements HttpHandler91{92/*93* Our http server which simply retruns a file with no content94*/95@Override96public void handle(HttpExchange trans) {97try {98trans.sendResponseHeaders(200, 0);99trans.close();100} catch (Exception e) {101e.printStackTrace();102}103}104}105106class MyCacheHandler extends ResponseCache107{108public CacheResponse get(URI uri, String rqstMethod, Map rqstHeaders)109{110return null;111}112113public CacheRequest put(URI uri, URLConnection conn)114{115return new MyCacheRequest();116}117}118119class MyCacheRequest extends CacheRequest120{121public void abort() {}122123public OutputStream getBody() throws IOException {124return null;125}126}127128129