Path: blob/master/test/jdk/sun/net/www/protocol/http/B6296310.java
41159 views
/*1* Copyright (c) 2005, 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 629631026* @modules java.base/sun.net.www27* @library ../../httptest/28* @build HttpCallback TestHttpServer HttpTransaction29* @run main/othervm B629631030* @run main/othervm -Djava.net.preferIPv6Addresses=true B629631031* @summary REGRESSION: AppletClassLoader.getResourceAsStream() behaviour is wrong in some cases32*/3334import java.net.*;35import java.io.*;36import java.util.*;3738/*39* http server returns 200 and content-length=040* Test will throw NPE if bug still exists41*/4243public class B629631044{45static SimpleHttpTransaction httpTrans;46static TestHttpServer server;4748public static void main(String[] args) throws Exception49{50ResponseCache.setDefault(new MyCacheHandler());51startHttpServer();52makeHttpCall();53}5455public static void startHttpServer() throws IOException {56httpTrans = new SimpleHttpTransaction();57InetAddress loopback = InetAddress.getLoopbackAddress();58server = new TestHttpServer(httpTrans, 1, 10, loopback, 0);59}6061public static void makeHttpCall() throws IOException {62try {63System.out.println("http server listen on: " + server.getLocalPort());64URL url = new URL("http" , InetAddress.getLoopbackAddress().getHostAddress(),65server.getLocalPort(), "/");66HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);67System.out.println(uc.getResponseCode());68} finally {69server.terminate();70}71}72}7374class SimpleHttpTransaction implements HttpCallback75{76/*77* Our http server which simply retruns a file with no content78*/79public void request(HttpTransaction trans) {80try {81trans.setResponseEntityBody("");82trans.sendResponse(200, "OK");83} catch (Exception e) {84e.printStackTrace();85}86}87}8889class MyCacheHandler extends ResponseCache90{91public CacheResponse get(URI uri, String rqstMethod, Map rqstHeaders)92{93return null;94}9596public CacheRequest put(URI uri, URLConnection conn)97{98return new MyCacheRequest();99}100}101102class MyCacheRequest extends CacheRequest103{104public void abort() {}105106public OutputStream getBody() throws IOException {107return null;108}109}110111112