1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
/*
* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8011719
* @modules jdk.httpserver
* @summary Basic checks to verify behavior of returned input streams
*/
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class HttpStreams {
void client(String u) throws Exception {
byte[] ba = new byte[5];
HttpURLConnection urlc = (HttpURLConnection)(new URL(u)).openConnection();
int resp = urlc.getResponseCode();
InputStream is;
if (resp == 200)
is = urlc.getInputStream();
else
is = urlc.getErrorStream();
expectNoThrow(() -> { is.read(); }, "read on open stream should not throw :" + u);
expectNoThrow(() -> { is.close(); }, "close should never throw: " + u);
expectNoThrow(() -> { is.close(); }, "close should never throw: " + u);
expectThrow(() -> { is.read(); }, "read on closed stream should throw: " + u);
expectThrow(() -> { is.read(ba); }, "read on closed stream should throw: " + u);
expectThrow(() -> { is.read(ba, 0, 2); }, "read on closed stream should throw: " + u);
}
void test() throws Exception {
HttpServer server = null;
try {
server = startHttpServer();
String baseUrl = "http://localhost:" + server.getAddress().getPort() + "/";
client(baseUrl + "chunked/");
client(baseUrl + "fixed/");
client(baseUrl + "error/");
client(baseUrl + "chunkedError/");
// Test with a response cache
ResponseCache ch = ResponseCache.getDefault();
ResponseCache.setDefault(new TrivialCacheHandler());
try {
client(baseUrl + "chunked/");
client(baseUrl + "fixed/");
client(baseUrl + "error/");
client(baseUrl + "chunkedError/");
} finally {
ResponseCache.setDefault(ch);
}
} finally {
if (server != null)
server.stop(0);
}
System.out.println("passed: " + pass + ", failed: " + fail);
if (fail > 0)
throw new RuntimeException("some tests failed check output");
}
public static void main(String[] args) throws Exception {
(new HttpStreams()).test();
}
// HTTP Server
HttpServer startHttpServer() throws IOException {
HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
httpServer.createContext("/chunked/", new ChunkedHandler());
httpServer.createContext("/fixed/", new FixedHandler());
httpServer.createContext("/error/", new ErrorHandler());
httpServer.createContext("/chunkedError/", new ChunkedErrorHandler());
httpServer.start();
return httpServer;
}
static abstract class AbstractHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
try (InputStream is = t.getRequestBody()) {
while (is.read() != -1);
}
t.sendResponseHeaders(respCode(), length());
try (OutputStream os = t.getResponseBody()) {
os.write(message());
}
t.close();
}
abstract int respCode();
abstract int length();
abstract byte[] message();
}
static class ChunkedHandler extends AbstractHandler {
static final byte[] ba =
"Hello there from chunked handler!".getBytes(StandardCharsets.US_ASCII);
int respCode() { return 200; }
int length() { return 0; }
byte[] message() { return ba; }
}
static class FixedHandler extends AbstractHandler {
static final byte[] ba =
"Hello there from fixed handler!".getBytes(StandardCharsets.US_ASCII);
int respCode() { return 200; }
int length() { return ba.length; }
byte[] message() { return ba; }
}
static class ErrorHandler extends AbstractHandler {
static final byte[] ba =
"This is an error mesg from the server!".getBytes(StandardCharsets.US_ASCII);
int respCode() { return 400; }
int length() { return ba.length; }
byte[] message() { return ba; }
}
static class ChunkedErrorHandler extends ErrorHandler {
int length() { return 0; }
}
static class TrivialCacheHandler extends ResponseCache
{
public CacheResponse get(URI uri, String rqstMethod, Map rqstHeaders) {
return null;
}
public CacheRequest put(URI uri, URLConnection conn) {
return new TrivialCacheRequest();
}
}
static class TrivialCacheRequest extends CacheRequest
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
public void abort() {}
public OutputStream getBody() throws IOException { return baos; }
}
static interface ThrowableRunnable {
void run() throws IOException;
}
void expectThrow(ThrowableRunnable r, String msg) {
try { r.run(); fail(msg); } catch (IOException x) { pass(); }
}
void expectNoThrow(ThrowableRunnable r, String msg) {
try { r.run(); pass(); } catch (IOException x) { fail(msg, x); }
}
private int pass;
private int fail;
void pass() { pass++; }
void fail(String msg, Exception x) { System.out.println(msg); x.printStackTrace(); fail++; }
void fail(String msg) { System.out.println(msg); Thread.dumpStack(); fail++; }
}
|