Path: blob/master/test/jdk/sun/net/www/protocol/http/GetErrorStream.java
41159 views
/*1* Copyright (c) 2000, 2018, 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 416049926* @modules jdk.httpserver27* @summary sun.net.www.protocol.http.HttpURLConnection.getErrorStream not hooked up28*/29import static java.net.HttpURLConnection.HTTP_NOT_FOUND;3031import java.io.FileNotFoundException;32import java.io.IOException;33import java.io.InputStream;34import java.io.OutputStream;35import java.net.HttpURLConnection;36import java.net.InetAddress;37import java.net.InetSocketAddress;38import java.net.URL;39import java.net.URLConnection;40import java.util.concurrent.ExecutorService;41import java.util.concurrent.Executors;4243import com.sun.net.httpserver.HttpExchange;44import com.sun.net.httpserver.HttpHandler;45import com.sun.net.httpserver.HttpServer;4647public class GetErrorStream {48public static void main(String[] args) throws Exception {49InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);50HttpServer server = HttpServer.create(addr, 10);51server.createContext("/" + HTTP_NOT_FOUND, he -> {52final String RESPONSE = "Test: File Not Found.";53he.sendResponseHeaders(HTTP_NOT_FOUND, RESPONSE.length());54OutputStream os = he.getResponseBody();55os.write(RESPONSE.getBytes());56os.close();57});58int port = server.getAddress().getPort();59System.out.println("Server port = " + port);6061ExecutorService executor = Executors.newCachedThreadPool();62server.setExecutor(executor);63server.start();6465URL url = new URL("http://localhost:" + port + "/" + HTTP_NOT_FOUND);66URLConnection conn = url.openConnection();6768try {69InputStream is = conn.getInputStream();70throw new RuntimeException("Expect HTTP_NOT_FOUND!");71} catch (FileNotFoundException e) {72try {73int respCode = ((HttpURLConnection) conn).getResponseCode();74InputStream es = ((HttpURLConnection) conn).getErrorStream();75if (respCode == HTTP_NOT_FOUND && es != null) {76System.out.println("Passed!");77} else {78throw new RuntimeException("getErrorStream failure.");79}80} catch (Exception ex) {81}82} finally {83server.stop(0);84executor.shutdownNow();85}8687}88}899091