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