Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/HttpStreams.java
38867 views
/*1* Copyright (c) 2013, 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 801171926* @summary Basic checks to verify behavior of returned input streams27*/2829import com.sun.net.httpserver.HttpExchange;30import com.sun.net.httpserver.HttpHandler;31import com.sun.net.httpserver.HttpServer;32import java.io.*;33import java.net.*;34import java.nio.charset.StandardCharsets;35import java.util.*;3637public class HttpStreams {3839void client(String u) throws Exception {40byte[] ba = new byte[5];41HttpURLConnection urlc = (HttpURLConnection)(new URL(u)).openConnection();42int resp = urlc.getResponseCode();43InputStream is;44if (resp == 200)45is = urlc.getInputStream();46else47is = urlc.getErrorStream();4849expectNoThrow(() -> { is.read(); }, "read on open stream should not throw :" + u);50expectNoThrow(() -> { is.close(); }, "close should never throw: " + u);51expectNoThrow(() -> { is.close(); }, "close should never throw: " + u);52expectThrow(() -> { is.read(); }, "read on closed stream should throw: " + u);53expectThrow(() -> { is.read(ba); }, "read on closed stream should throw: " + u);54expectThrow(() -> { is.read(ba, 0, 2); }, "read on closed stream should throw: " + u);55}5657void test() throws Exception {58HttpServer server = null;59try {60server = startHttpServer();61String baseUrl = "http://localhost:" + server.getAddress().getPort() + "/";62client(baseUrl + "chunked/");63client(baseUrl + "fixed/");64client(baseUrl + "error/");65client(baseUrl + "chunkedError/");6667// Test with a response cache68ResponseCache ch = ResponseCache.getDefault();69ResponseCache.setDefault(new TrivialCacheHandler());70try {71client(baseUrl + "chunked/");72client(baseUrl + "fixed/");73client(baseUrl + "error/");74client(baseUrl + "chunkedError/");75} finally {76ResponseCache.setDefault(ch);77}78} finally {79if (server != null)80server.stop(0);81}8283System.out.println("passed: " + pass + ", failed: " + fail);84if (fail > 0)85throw new RuntimeException("some tests failed check output");86}8788public static void main(String[] args) throws Exception {89(new HttpStreams()).test();90}9192// HTTP Server93HttpServer startHttpServer() throws IOException {94HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);95httpServer.createContext("/chunked/", new ChunkedHandler());96httpServer.createContext("/fixed/", new FixedHandler());97httpServer.createContext("/error/", new ErrorHandler());98httpServer.createContext("/chunkedError/", new ChunkedErrorHandler());99httpServer.start();100return httpServer;101}102103static abstract class AbstractHandler implements HttpHandler {104@Override105public void handle(HttpExchange t) throws IOException {106try (InputStream is = t.getRequestBody()) {107while (is.read() != -1);108}109t.sendResponseHeaders(respCode(), length());110try (OutputStream os = t.getResponseBody()) {111os.write(message());112}113t.close();114}115116abstract int respCode();117abstract int length();118abstract byte[] message();119}120121static class ChunkedHandler extends AbstractHandler {122static final byte[] ba =123"Hello there from chunked handler!".getBytes(StandardCharsets.US_ASCII);124int respCode() { return 200; }125int length() { return 0; }126byte[] message() { return ba; }127}128129static class FixedHandler extends AbstractHandler {130static final byte[] ba =131"Hello there from fixed handler!".getBytes(StandardCharsets.US_ASCII);132int respCode() { return 200; }133int length() { return ba.length; }134byte[] message() { return ba; }135}136137static class ErrorHandler extends AbstractHandler {138static final byte[] ba =139"This is an error mesg from the server!".getBytes(StandardCharsets.US_ASCII);140int respCode() { return 400; }141int length() { return ba.length; }142byte[] message() { return ba; }143}144145static class ChunkedErrorHandler extends ErrorHandler {146int length() { return 0; }147}148149static class TrivialCacheHandler extends ResponseCache150{151public CacheResponse get(URI uri, String rqstMethod, Map rqstHeaders) {152return null;153}154155public CacheRequest put(URI uri, URLConnection conn) {156return new TrivialCacheRequest();157}158}159160static class TrivialCacheRequest extends CacheRequest161{162ByteArrayOutputStream baos = new ByteArrayOutputStream();163public void abort() {}164public OutputStream getBody() throws IOException { return baos; }165}166167static interface ThrowableRunnable {168void run() throws IOException;169}170171void expectThrow(ThrowableRunnable r, String msg) {172try { r.run(); fail(msg); } catch (IOException x) { pass(); }173}174175void expectNoThrow(ThrowableRunnable r, String msg) {176try { r.run(); pass(); } catch (IOException x) { fail(msg, x); }177}178179private int pass;180private int fail;181void pass() { pass++; }182void fail(String msg, Exception x) { System.out.println(msg); x.printStackTrace(); fail++; }183void fail(String msg) { System.out.println(msg); Thread.dumpStack(); fail++; }184}185186187