author | chegar |
Thu, 07 Dec 2017 11:54:55 +0000 | |
branch | http-client-branch |
changeset 55973 | 4d9b002587db |
parent 55874 | ee17449ff241 |
parent 48083 | b1c1b4ef4be2 |
child 56083 | 32ff53628380 |
permissions | -rw-r--r-- |
36131 | 1 |
/* |
45535 | 2 |
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
36131 | 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 |
||
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
24 |
/* |
36131 | 25 |
* @test |
45708
3512073b446f
8178699: Fail to send async requests if server doesn't response the first one
xiaofeya
parents:
45535
diff
changeset
|
26 |
* @bug 8087112 8178699 |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
27 |
* @modules jdk.incubator.httpclient |
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
28 |
* java.logging |
38883 | 29 |
* jdk.httpserver |
37720 | 30 |
* @library /lib/testlibrary/ / |
45535 | 31 |
* @build jdk.testlibrary.SimpleSSLContext ProxyServer |
36131 | 32 |
* @compile ../../../com/sun/net/httpserver/LogFilter.java |
45535 | 33 |
* @compile ../../../com/sun/net/httpserver/EchoHandler.java |
36131 | 34 |
* @compile ../../../com/sun/net/httpserver/FileServerHandler.java |
48083 | 35 |
* @run main/othervm -Djdk.internal.httpclient.debug=true -Djdk.httpclient.HttpClient.log=errors,trace SmokeTest |
36131 | 36 |
*/ |
37 |
||
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
38 |
import com.sun.net.httpserver.Headers; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
39 |
import com.sun.net.httpserver.HttpContext; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
40 |
import com.sun.net.httpserver.HttpExchange; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
41 |
import com.sun.net.httpserver.HttpHandler; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
42 |
import com.sun.net.httpserver.HttpServer; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
43 |
import com.sun.net.httpserver.HttpsConfigurator; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
44 |
import com.sun.net.httpserver.HttpsParameters; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
45 |
import com.sun.net.httpserver.HttpsServer; |
48083 | 46 |
import java.net.Proxy; |
47 |
import java.net.SocketAddress; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
48 |
import java.util.concurrent.atomic.AtomicInteger; |
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
49 |
import java.net.InetSocketAddress; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
50 |
import java.net.PasswordAuthentication; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
51 |
import java.net.ProxySelector; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
52 |
import java.net.URI; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
53 |
import jdk.incubator.http.HttpClient; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
54 |
import jdk.incubator.http.HttpRequest; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
55 |
import jdk.incubator.http.HttpResponse; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
56 |
import java.nio.file.StandardOpenOption; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
57 |
import java.io.File; |
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
58 |
import java.io.FileInputStream; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
59 |
import java.io.FileOutputStream; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
60 |
import java.io.FileNotFoundException; |
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
61 |
import java.io.IOException; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
62 |
import java.io.BufferedInputStream; |
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
63 |
import java.io.InputStream; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
64 |
import java.io.OutputStream; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
65 |
import java.io.UncheckedIOException; |
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
66 |
import java.util.concurrent.BlockingQueue; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
67 |
import java.util.concurrent.CompletableFuture; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
68 |
import java.util.concurrent.CompletionException; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
69 |
import java.util.concurrent.CyclicBarrier; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
70 |
import java.util.concurrent.Executors; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
71 |
import java.util.concurrent.ExecutorService; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
72 |
import java.util.concurrent.LinkedBlockingQueue; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
73 |
import java.util.concurrent.TimeUnit; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
74 |
import javax.net.ssl.SSLContext; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
75 |
import javax.net.ssl.SSLParameters; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
76 |
import java.nio.file.Files; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
77 |
import java.nio.file.Path; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
78 |
import java.nio.file.Paths; |
36131 | 79 |
import java.util.HashSet; |
80 |
import java.util.LinkedList; |
|
81 |
import java.util.List; |
|
82 |
import java.util.Random; |
|
83 |
import jdk.testlibrary.SimpleSSLContext; |
|
48083 | 84 |
import static jdk.incubator.http.HttpRequest.BodyPublisher.fromFile; |
85 |
import static jdk.incubator.http.HttpRequest.BodyPublisher.fromInputStream; |
|
86 |
import static jdk.incubator.http.HttpRequest.BodyPublisher.fromString; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
87 |
import static jdk.incubator.http.HttpResponse.*; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
88 |
import static jdk.incubator.http.HttpResponse.BodyHandler.asFile; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
89 |
import static jdk.incubator.http.HttpResponse.BodyHandler.asString; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
90 |
import java.util.concurrent.CountDownLatch; |
36131 | 91 |
import java.util.logging.ConsoleHandler; |
92 |
import java.util.logging.Level; |
|
93 |
import java.util.logging.Logger; |
|
94 |
||
95 |
/** |
|
96 |
* * Basic smoke test for Http/1.1 client |
|
97 |
* - basic request response |
|
98 |
* - request body POST |
|
99 |
* - response body GET |
|
100 |
* - redirect |
|
101 |
* - chunked request/response |
|
102 |
* - SSL |
|
103 |
* - proxies |
|
104 |
* - 100 continue |
|
105 |
* - check keep alive appears to be working |
|
106 |
* - cancel of long request |
|
107 |
* |
|
108 |
* Uses a FileServerHandler serving a couple of known files |
|
109 |
* in docs directory. |
|
110 |
*/ |
|
111 |
public class SmokeTest { |
|
112 |
static SSLContext ctx; |
|
37720 | 113 |
static SSLParameters sslparams; |
36131 | 114 |
static HttpServer s1 ; |
115 |
static HttpsServer s2; |
|
116 |
static ExecutorService executor; |
|
117 |
static int port; |
|
118 |
static int httpsport; |
|
119 |
static String httproot; |
|
120 |
static String httpsroot; |
|
121 |
static HttpClient client; |
|
122 |
static ProxyServer proxy; |
|
123 |
static int proxyPort; |
|
124 |
static RedirectErrorHandler redirectErrorHandler, redirectErrorHandlerSecure; |
|
125 |
static RedirectHandler redirectHandler, redirectHandlerSecure; |
|
126 |
static DelayHandler delayHandler; |
|
127 |
final static String midSizedFilename = "/files/notsobigfile.txt"; |
|
128 |
final static String smallFilename = "/files/smallfile.txt"; |
|
129 |
static Path midSizedFile; |
|
130 |
static Path smallFile; |
|
131 |
static String fileroot; |
|
132 |
||
133 |
static String getFileContent(String path) throws IOException { |
|
134 |
FileInputStream fis = new FileInputStream(path); |
|
135 |
byte[] buf = new byte[2000]; |
|
136 |
StringBuilder sb = new StringBuilder(); |
|
137 |
int n; |
|
138 |
while ((n=fis.read(buf)) != -1) { |
|
139 |
sb.append(new String(buf, 0, n, "US-ASCII")); |
|
140 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
141 |
fis.close(); |
36131 | 142 |
return sb.toString(); |
143 |
} |
|
144 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
145 |
static void cmpFileContent(Path path1, Path path2) throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
146 |
InputStream fis1 = new BufferedInputStream(new FileInputStream(path1.toFile())); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
147 |
InputStream fis2 = new BufferedInputStream(new FileInputStream(path2.toFile())); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
148 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
149 |
int n1, n2; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
150 |
while ((n1=fis1.read()) != -1) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
151 |
n2 = fis2.read(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
152 |
if (n1 != n2) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
153 |
throw new IOException("Content not the same"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
154 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
155 |
fis1.close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
156 |
fis2.close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
157 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
158 |
|
36131 | 159 |
public static void main(String[] args) throws Exception { |
160 |
initServer(); |
|
161 |
fileroot = System.getProperty ("test.src", ".")+ "/docs"; |
|
162 |
midSizedFile = Paths.get(fileroot + midSizedFilename); |
|
163 |
smallFile = Paths.get(fileroot + smallFilename); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
164 |
ExecutorService e = Executors.newCachedThreadPool(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
165 |
System.out.println(e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
166 |
client = HttpClient.newBuilder() |
36131 | 167 |
.sslContext(ctx) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
168 |
.executor(e) |
44854
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
42460
diff
changeset
|
169 |
.version(HttpClient.Version.HTTP_1_1) |
37720 | 170 |
.sslParameters(sslparams) |
36131 | 171 |
.followRedirects(HttpClient.Redirect.ALWAYS) |
172 |
.build(); |
|
173 |
||
174 |
try { |
|
175 |
test1(httproot + "files/foo.txt", true); |
|
176 |
test1(httproot + "files/foo.txt", false); |
|
177 |
test1(httpsroot + "files/foo.txt", true); |
|
178 |
test1(httpsroot + "files/foo.txt", false); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
179 |
|
36131 | 180 |
test2(httproot + "echo/foo", "This is a short test"); |
181 |
test2(httpsroot + "echo/foo", "This is a short test"); |
|
182 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
183 |
test2a(httproot + "echo/foo"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
184 |
test2a(httpsroot + "echo/foo"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
185 |
|
36131 | 186 |
test3(httproot + "redirect/foo.txt"); |
187 |
test3(httpsroot + "redirect/foo.txt"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
188 |
|
36131 | 189 |
test4(httproot + "files/foo.txt"); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
190 |
|
36131 | 191 |
test4(httpsroot + "files/foo.txt"); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
192 |
|
36131 | 193 |
test5(httproot + "echo/foo", true); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
194 |
|
36131 | 195 |
test5(httpsroot + "echo/foo", true); |
196 |
test5(httproot + "echo/foo", false); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
197 |
|
36131 | 198 |
test5(httpsroot + "echo/foo", false); |
199 |
||
200 |
test6(httproot + "echo/foo", true); |
|
201 |
test6(httpsroot + "echo/foo", true); |
|
202 |
test6(httproot + "echo/foo", false); |
|
203 |
test6(httpsroot + "echo/foo", false); |
|
204 |
||
205 |
test7(httproot + "keepalive/foo"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
206 |
/* |
36131 | 207 |
test10(httproot + "redirecterror/foo.txt"); |
208 |
||
209 |
test10(httpsroot + "redirecterror/foo.txt"); |
|
210 |
||
211 |
test11(httproot + "echo/foo"); |
|
212 |
test11(httpsroot + "echo/foo"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
213 |
*/ |
36131 | 214 |
//test12(httproot + "delay/foo", delayHandler); |
215 |
||
216 |
} finally { |
|
217 |
s1.stop(0); |
|
218 |
s2.stop(0); |
|
219 |
proxy.close(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
220 |
e.shutdownNow(); |
36131 | 221 |
executor.shutdownNow(); |
222 |
} |
|
223 |
} |
|
224 |
||
225 |
static class Auth extends java.net.Authenticator { |
|
226 |
volatile int count = 0; |
|
227 |
@Override |
|
228 |
protected PasswordAuthentication getPasswordAuthentication() { |
|
229 |
if (count++ == 0) { |
|
230 |
return new PasswordAuthentication("user", "passwd".toCharArray()); |
|
231 |
} else { |
|
232 |
return new PasswordAuthentication("user", "goober".toCharArray()); |
|
233 |
} |
|
234 |
} |
|
235 |
int count() { |
|
236 |
return count; |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
// Basic test |
|
241 |
static void test1(String target, boolean fixedLen) throws Exception { |
|
242 |
System.out.print("test1: " + target); |
|
243 |
URI uri = new URI(target); |
|
244 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
245 |
HttpRequest.Builder builder = HttpRequest.newBuilder().uri(uri).GET(); |
36131 | 246 |
|
247 |
if (fixedLen) { |
|
248 |
builder.header("XFixed", "yes"); |
|
249 |
} |
|
250 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
251 |
HttpRequest request = builder.build(); |
36131 | 252 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
253 |
HttpResponse<String> response = client.send(request, asString()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
254 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
255 |
String body = response.body(); |
36131 | 256 |
if (!body.equals("This is foo.txt\r\n")) { |
48083 | 257 |
throw new RuntimeException("Did not get expected body: " |
258 |
+ "\n\t expected \"This is foo.txt\\r\\n\"" |
|
259 |
+ "\n\t received \"" |
|
260 |
+ body.replace("\r", "\\r").replace("\n","\\n") + "\""); |
|
36131 | 261 |
} |
262 |
||
263 |
// repeat async |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
264 |
HttpResponse<String> response1 = client.sendAsync(request, asString()) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
265 |
.join(); |
36131 | 266 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
267 |
String body1 = response1.body(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
268 |
if (!body1.equals("This is foo.txt\r\n")) { |
36131 | 269 |
throw new RuntimeException(); |
270 |
} |
|
271 |
System.out.println(" OK"); |
|
272 |
} |
|
273 |
||
274 |
// POST use echo to check reply |
|
275 |
static void test2(String s, String body) throws Exception { |
|
276 |
System.out.print("test2: " + s); |
|
277 |
URI uri = new URI(s); |
|
278 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
279 |
HttpRequest request = HttpRequest.newBuilder(uri) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
280 |
.POST(fromString(body)) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
281 |
.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
282 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
283 |
HttpResponse<String> response = client.send(request, asString()); |
36131 | 284 |
|
285 |
if (response.statusCode() != 200) { |
|
286 |
throw new RuntimeException( |
|
287 |
"Expected 200, got [ " + response.statusCode() + " ]"); |
|
288 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
289 |
String reply = response.body(); |
36131 | 290 |
if (!reply.equals(body)) { |
291 |
throw new RuntimeException( |
|
292 |
"Body mismatch: expected [" + body + "], got [" + reply + "]"); |
|
293 |
} |
|
294 |
System.out.println(" OK"); |
|
295 |
} |
|
296 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
297 |
// POST use echo to check reply |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
298 |
static void test2a(String s) throws Exception { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
299 |
System.out.print("test2a: " + s); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
300 |
URI uri = new URI(s); |
48083 | 301 |
Path p = getTempFile(128 * 1024); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
302 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
303 |
HttpRequest request = HttpRequest.newBuilder(uri) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
304 |
.POST(fromFile(p)) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
305 |
.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
306 |
|
48083 | 307 |
Path resp = getTempFile(1); // will be overwritten |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
308 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
309 |
HttpResponse<Path> response = |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
310 |
client.send(request, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
311 |
BodyHandler.asFile(resp, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
312 |
StandardOpenOption.TRUNCATE_EXISTING, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
313 |
StandardOpenOption.WRITE)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
314 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
315 |
if (response.statusCode() != 200) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
316 |
throw new RuntimeException( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
317 |
"Expected 200, got [ " + response.statusCode() + " ]"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
318 |
} |
48083 | 319 |
// no redirection, etc, should be no previous response |
320 |
if (response.previousResponse().isPresent()) { |
|
321 |
throw new RuntimeException( |
|
322 |
"Unexpected previous response: " + response.previousResponse().get()); |
|
323 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
324 |
Path reply = response.body(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
325 |
//System.out.println("Reply stored in " + reply.toString()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
326 |
cmpFileContent(reply, p); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
327 |
System.out.println(" OK"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
328 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
329 |
|
36131 | 330 |
// Redirect |
331 |
static void test3(String s) throws Exception { |
|
332 |
System.out.print("test3: " + s); |
|
333 |
URI uri = new URI(s); |
|
334 |
RedirectHandler handler = uri.getScheme().equals("https") |
|
335 |
? redirectHandlerSecure : redirectHandler; |
|
336 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
337 |
HttpRequest request = HttpRequest.newBuilder() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
338 |
.uri(uri) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
339 |
.GET() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
340 |
.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
341 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
342 |
HttpResponse<Path> response = client.send(request, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
343 |
asFile(Paths.get("redir1.txt"))); |
36131 | 344 |
|
345 |
if (response.statusCode() != 200) { |
|
346 |
throw new RuntimeException( |
|
347 |
"Expected 200, got [ " + response.statusCode() + " ]"); |
|
348 |
} else { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
349 |
response.body(); |
36131 | 350 |
} |
351 |
||
352 |
Path downloaded = Paths.get("redir1.txt"); |
|
353 |
if (Files.size(downloaded) != Files.size(midSizedFile)) { |
|
354 |
throw new RuntimeException("Size mismatch"); |
|
355 |
} |
|
48083 | 356 |
checkPreviousRedirectResponses(request, response); |
36131 | 357 |
System.out.printf(" (count: %d) ", handler.count()); |
358 |
// repeat with async api |
|
359 |
||
360 |
handler.reset(); |
|
361 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
362 |
request = HttpRequest.newBuilder(uri).build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
363 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
364 |
response = client.sendAsync(request, asFile(Paths.get("redir2.txt"))).join(); |
36131 | 365 |
|
366 |
if (response.statusCode() != 200) { |
|
367 |
throw new RuntimeException( |
|
368 |
"Expected 200, got [ " + response.statusCode() + " ]"); |
|
369 |
} else { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
370 |
response.body(); |
36131 | 371 |
} |
372 |
||
373 |
downloaded = Paths.get("redir2.txt"); |
|
374 |
if (Files.size(downloaded) != Files.size(midSizedFile)) { |
|
375 |
throw new RuntimeException("Size mismatch 2"); |
|
376 |
} |
|
48083 | 377 |
|
378 |
checkPreviousRedirectResponses(request, response); |
|
36131 | 379 |
System.out.printf(" (count: %d) ", handler.count()); |
380 |
System.out.println(" OK"); |
|
381 |
} |
|
382 |
||
48083 | 383 |
static void checkPreviousRedirectResponses(HttpRequest initialRequest, |
384 |
HttpResponse<?> finalResponse) { |
|
385 |
// there must be at least one previous response |
|
386 |
finalResponse.previousResponse() |
|
387 |
.orElseThrow(() -> new RuntimeException("no previous response")); |
|
388 |
||
389 |
HttpResponse<?> response = finalResponse; |
|
390 |
do { |
|
391 |
URI uri = response.uri(); |
|
392 |
response = response.previousResponse().get(); |
|
393 |
check(300 <= response.statusCode() && response.statusCode() <= 309, |
|
394 |
"Expected 300 <= code <= 309, got:" + response.statusCode()); |
|
395 |
check(response.body() == null, "Unexpected body: " + response.body()); |
|
396 |
String locationHeader = response.headers().firstValue("Location") |
|
397 |
.orElseThrow(() -> new RuntimeException("no previous Location")); |
|
398 |
check(uri.toString().endsWith(locationHeader), |
|
399 |
"URI: " + uri + ", Location: " + locationHeader); |
|
400 |
} while (response.previousResponse().isPresent()); |
|
401 |
||
402 |
// initial |
|
403 |
check(initialRequest.equals(response.request()), |
|
404 |
"Expected initial request [%s] to equal last prev req [%s]", |
|
405 |
initialRequest, response.request()); |
|
406 |
} |
|
407 |
||
408 |
static void check(boolean cond, Object... msg) { |
|
409 |
if (cond) |
|
410 |
return; |
|
411 |
StringBuilder sb = new StringBuilder(); |
|
412 |
for (Object o : msg) |
|
413 |
sb.append(o); |
|
414 |
throw new RuntimeException(sb.toString()); |
|
415 |
} |
|
416 |
||
417 |
/** |
|
418 |
* A Proxy Selector that wraps a ProxySelector.of(), and counts the number |
|
419 |
* of times its select method has been invoked. This can be used to ensure |
|
420 |
* that the Proxy Selector is invoked only once per HttpClient.sendXXX |
|
421 |
* invocation. |
|
422 |
*/ |
|
423 |
static class CountingProxySelector extends ProxySelector { |
|
424 |
private final ProxySelector proxySelector; |
|
425 |
private volatile int count; // 0 |
|
426 |
private CountingProxySelector(InetSocketAddress proxyAddress) { |
|
427 |
proxySelector = ProxySelector.of(proxyAddress); |
|
428 |
} |
|
429 |
||
430 |
public static CountingProxySelector of(InetSocketAddress proxyAddress) { |
|
431 |
return new CountingProxySelector(proxyAddress); |
|
432 |
} |
|
433 |
||
434 |
int count() { return count; } |
|
435 |
||
436 |
@Override |
|
437 |
public List<Proxy> select(URI uri) { |
|
438 |
count++; |
|
439 |
return proxySelector.select(uri); |
|
440 |
} |
|
441 |
||
442 |
@Override |
|
443 |
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { |
|
444 |
proxySelector.connectFailed(uri, sa, ioe); |
|
445 |
} |
|
446 |
} |
|
447 |
||
36131 | 448 |
// Proxies |
449 |
static void test4(String s) throws Exception { |
|
450 |
System.out.print("test4: " + s); |
|
451 |
URI uri = new URI(s); |
|
452 |
InetSocketAddress proxyAddr = new InetSocketAddress("127.0.0.1", proxyPort); |
|
453 |
String filename = fileroot + uri.getPath(); |
|
454 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
455 |
ExecutorService e = Executors.newCachedThreadPool(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
456 |
|
48083 | 457 |
CountingProxySelector ps = CountingProxySelector.of(proxyAddr); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
458 |
HttpClient cl = HttpClient.newBuilder() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
459 |
.executor(e) |
48083 | 460 |
.proxy(ps) |
36131 | 461 |
.sslContext(ctx) |
37720 | 462 |
.sslParameters(sslparams) |
36131 | 463 |
.build(); |
464 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
465 |
HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
466 |
|
48083 | 467 |
CompletableFuture<String> fut = cl.sendAsync(request, asString()) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
468 |
.thenApply((response) -> response.body()); |
36131 | 469 |
|
470 |
String body = fut.get(5, TimeUnit.HOURS); |
|
471 |
||
472 |
String fc = getFileContent(filename); |
|
473 |
||
474 |
if (!body.equals(fc)) { |
|
475 |
throw new RuntimeException( |
|
476 |
"Body mismatch: expected [" + body + "], got [" + fc + "]"); |
|
477 |
} |
|
48083 | 478 |
if (ps.count() != 1) { |
479 |
throw new RuntimeException("CountingProxySelector. Expected 1, got " + ps.count()); |
|
480 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
481 |
e.shutdownNow(); |
36131 | 482 |
System.out.println(" OK"); |
483 |
} |
|
484 |
||
485 |
// 100 Continue: use echo target |
|
486 |
static void test5(String target, boolean fixedLen) throws Exception { |
|
487 |
System.out.print("test5: " + target); |
|
488 |
URI uri = new URI(target); |
|
489 |
String requestBody = generateString(12 * 1024 + 13); |
|
490 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
491 |
HttpRequest.Builder builder = HttpRequest.newBuilder(uri) |
36131 | 492 |
.expectContinue(true) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
493 |
.POST(fromString(requestBody)); |
36131 | 494 |
|
495 |
if (fixedLen) { |
|
496 |
builder.header("XFixed", "yes"); |
|
497 |
} |
|
498 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
499 |
HttpRequest request = builder.build(); |
36131 | 500 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
501 |
HttpResponse<String> response = client.send(request, asString()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
502 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
503 |
String body = response.body(); |
36131 | 504 |
|
505 |
if (!body.equals(requestBody)) { |
|
506 |
throw new RuntimeException( |
|
507 |
"Body mismatch: expected [" + body + "], got [" + body + "]"); |
|
508 |
} |
|
509 |
System.out.println(" OK"); |
|
510 |
} |
|
511 |
||
512 |
// use echo |
|
513 |
static void test6(String target, boolean fixedLen) throws Exception { |
|
514 |
System.out.print("test6: " + target); |
|
515 |
URI uri = new URI(target); |
|
516 |
String requestBody = generateString(12 * 1024 + 3); |
|
517 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
518 |
HttpRequest.Builder builder = HttpRequest.newBuilder(uri).GET(); |
36131 | 519 |
|
520 |
if (fixedLen) { |
|
521 |
builder.header("XFixed", "yes"); |
|
522 |
} |
|
523 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
524 |
HttpRequest request = builder.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
525 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
526 |
HttpResponse<String> response = client.send(request, asString()); |
36131 | 527 |
|
528 |
if (response.statusCode() != 200) { |
|
529 |
throw new RuntimeException( |
|
530 |
"Expected 200, got [ " + response.statusCode() + " ]"); |
|
531 |
} |
|
532 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
533 |
String responseBody = response.body(); |
36131 | 534 |
|
535 |
if (responseBody.equals(requestBody)) { |
|
536 |
throw new RuntimeException( |
|
537 |
"Body mismatch: expected [" + requestBody + "], got [" + responseBody + "]"); |
|
538 |
} |
|
539 |
System.out.println(" OK"); |
|
540 |
} |
|
541 |
||
542 |
@SuppressWarnings("rawtypes") |
|
543 |
static void test7(String target) throws Exception { |
|
544 |
System.out.print("test7: " + target); |
|
48083 | 545 |
Path requestBody = getTempFile(128 * 1024); |
36131 | 546 |
// First test |
547 |
URI uri = new URI(target); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
548 |
HttpRequest request = HttpRequest.newBuilder().uri(uri).GET().build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
549 |
|
36131 | 550 |
for (int i=0; i<4; i++) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
551 |
HttpResponse<String> r = client.send(request, asString()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
552 |
String body = r.body(); |
36131 | 553 |
if (!body.equals("OK")) { |
554 |
throw new RuntimeException("Expected OK, got: " + body); |
|
555 |
} |
|
556 |
} |
|
557 |
||
558 |
// Second test: 4 x parallel |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
559 |
request = HttpRequest.newBuilder().uri(uri).POST(fromFile(requestBody)).build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
560 |
List<CompletableFuture<String>> futures = new LinkedList<>(); |
36131 | 561 |
for (int i=0; i<4; i++) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
562 |
futures.add(client.sendAsync(request, asString()) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
563 |
.thenApply((response) -> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
564 |
if (response.statusCode() == 200) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
565 |
return response.body(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
566 |
else |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
567 |
return "ERROR"; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
568 |
})); |
36131 | 569 |
} |
570 |
// all sent? |
|
571 |
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) |
|
572 |
.join(); |
|
573 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
574 |
for (CompletableFuture<String> future : futures) { |
36131 | 575 |
String body = future.get(); |
576 |
if (!body.equals("OK")) { |
|
577 |
throw new RuntimeException("Expected OK, got: " + body); |
|
578 |
} |
|
579 |
} |
|
580 |
||
581 |
// Third test: Multiple of 4 parallel requests |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
582 |
request = HttpRequest.newBuilder(uri).GET().build(); |
36131 | 583 |
BlockingQueue<String> q = new LinkedBlockingQueue<>(); |
584 |
for (int i=0; i<4; i++) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
585 |
client.sendAsync(request, asString()) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
586 |
.thenApply((HttpResponse<String> resp) -> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
587 |
String body = resp.body(); |
36131 | 588 |
putQ(q, body); |
589 |
return body; |
|
590 |
}); |
|
591 |
} |
|
592 |
// we've sent four requests. Now, just send another request |
|
593 |
// as each response is received. The idea is to ensure that |
|
594 |
// only four sockets ever get used. |
|
595 |
||
596 |
for (int i=0; i<100; i++) { |
|
597 |
// block until response received |
|
598 |
String body = takeQ(q); |
|
599 |
if (!body.equals("OK")) { |
|
600 |
throw new RuntimeException(body); |
|
601 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
602 |
client.sendAsync(request, asString()) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
603 |
.thenApply((resp) -> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
604 |
if (resp.statusCode() == 200) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
605 |
putQ(q, resp.body()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
606 |
else |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
607 |
putQ(q, "ERROR"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
608 |
return null; |
36131 | 609 |
}); |
610 |
} |
|
611 |
// should be four left |
|
612 |
for (int i=0; i<4; i++) { |
|
613 |
takeQ(q); |
|
614 |
} |
|
615 |
System.out.println(" OK"); |
|
616 |
} |
|
617 |
||
618 |
static String takeQ(BlockingQueue<String> q) { |
|
619 |
String r = null; |
|
620 |
try { |
|
621 |
r = q.take(); |
|
622 |
} catch (InterruptedException e) {} |
|
623 |
||
624 |
return r; |
|
625 |
} |
|
626 |
||
627 |
static void putQ(BlockingQueue<String> q, String o) { |
|
628 |
try { |
|
629 |
q.put(o); |
|
630 |
} catch (InterruptedException e) { |
|
631 |
// can't happen |
|
632 |
} |
|
633 |
} |
|
634 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
635 |
static FileInputStream newStream() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
636 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
637 |
return new FileInputStream(smallFile.toFile()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
638 |
} catch (FileNotFoundException e) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
639 |
throw new UncheckedIOException(e); |
36131 | 640 |
} |
641 |
} |
|
642 |
// Chunked output stream |
|
643 |
static void test11(String target) throws Exception { |
|
644 |
System.out.print("test11: " + target); |
|
645 |
URI uri = new URI(target); |
|
646 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
647 |
HttpRequest request = HttpRequest.newBuilder(uri) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
648 |
.POST(fromInputStream(SmokeTest::newStream)) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
649 |
.build(); |
36131 | 650 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
651 |
Path download = Paths.get("test11.txt"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
652 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
653 |
HttpResponse<Path> response = client.send(request, asFile(download)); |
36131 | 654 |
|
655 |
if (response.statusCode() != 200) { |
|
656 |
throw new RuntimeException("Wrong response code"); |
|
657 |
} |
|
658 |
||
659 |
download.toFile().delete(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
660 |
response.body(); |
36131 | 661 |
|
662 |
if (Files.size(download) != Files.size(smallFile)) { |
|
663 |
System.out.println("Original size: " + Files.size(smallFile)); |
|
664 |
System.out.println("Downloaded size: " + Files.size(download)); |
|
665 |
throw new RuntimeException("Size mismatch"); |
|
666 |
} |
|
667 |
System.out.println(" OK"); |
|
668 |
} |
|
669 |
||
670 |
static void delay(int seconds) { |
|
671 |
try { |
|
672 |
Thread.sleep(seconds * 1000); |
|
673 |
} catch (InterruptedException e) { |
|
674 |
} |
|
675 |
} |
|
676 |
||
677 |
// Redirect loop: return an error after a certain number of redirects |
|
678 |
static void test10(String s) throws Exception { |
|
679 |
System.out.print("test10: " + s); |
|
680 |
URI uri = new URI(s); |
|
681 |
RedirectErrorHandler handler = uri.getScheme().equals("https") |
|
682 |
? redirectErrorHandlerSecure : redirectErrorHandler; |
|
683 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
684 |
HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
685 |
CompletableFuture<HttpResponse<String>> cf = |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
686 |
client.sendAsync(request, asString()); |
36131 | 687 |
|
688 |
try { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
689 |
HttpResponse<String> response = cf.join(); |
36131 | 690 |
throw new RuntimeException("Exepected Completion Exception"); |
691 |
} catch (CompletionException e) { |
|
692 |
//System.out.println(e); |
|
693 |
} |
|
694 |
||
695 |
System.out.printf(" (Calls %d) ", handler.count()); |
|
696 |
System.out.println(" OK"); |
|
697 |
} |
|
698 |
||
699 |
static final int NUM = 50; |
|
700 |
||
701 |
static Random random = new Random(); |
|
702 |
static final String alphabet = "ABCDEFGHIJKLMNOPQRST"; |
|
703 |
||
704 |
static char randomChar() { |
|
705 |
return alphabet.charAt(random.nextInt(alphabet.length())); |
|
706 |
} |
|
707 |
||
708 |
static String generateString(int length) { |
|
709 |
StringBuilder sb = new StringBuilder(length); |
|
710 |
for (int i=0; i<length; i++) { |
|
711 |
sb.append(randomChar()); |
|
712 |
} |
|
713 |
return sb.toString(); |
|
714 |
} |
|
715 |
||
716 |
static void initServer() throws Exception { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
717 |
|
36131 | 718 |
Logger logger = Logger.getLogger("com.sun.net.httpserver"); |
719 |
ConsoleHandler ch = new ConsoleHandler(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
720 |
logger.setLevel(Level.SEVERE); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
721 |
ch.setLevel(Level.SEVERE); |
36131 | 722 |
logger.addHandler(ch); |
723 |
||
48083 | 724 |
String root = System.getProperty ("test.src", ".")+ "/docs"; |
36131 | 725 |
InetSocketAddress addr = new InetSocketAddress (0); |
726 |
s1 = HttpServer.create (addr, 0); |
|
727 |
if (s1 instanceof HttpsServer) { |
|
728 |
throw new RuntimeException ("should not be httpsserver"); |
|
729 |
} |
|
730 |
s2 = HttpsServer.create (addr, 0); |
|
731 |
HttpHandler h = new FileServerHandler(root); |
|
732 |
||
733 |
HttpContext c1 = s1.createContext("/files", h); |
|
734 |
HttpContext c2 = s2.createContext("/files", h); |
|
735 |
HttpContext c3 = s1.createContext("/echo", new EchoHandler()); |
|
736 |
redirectHandler = new RedirectHandler("/redirect"); |
|
737 |
redirectHandlerSecure = new RedirectHandler("/redirect"); |
|
738 |
HttpContext c4 = s1.createContext("/redirect", redirectHandler); |
|
739 |
HttpContext c41 = s2.createContext("/redirect", redirectHandlerSecure); |
|
740 |
HttpContext c5 = s2.createContext("/echo", new EchoHandler()); |
|
741 |
HttpContext c6 = s1.createContext("/keepalive", new KeepAliveHandler()); |
|
742 |
redirectErrorHandler = new RedirectErrorHandler("/redirecterror"); |
|
743 |
redirectErrorHandlerSecure = new RedirectErrorHandler("/redirecterror"); |
|
744 |
HttpContext c7 = s1.createContext("/redirecterror", redirectErrorHandler); |
|
745 |
HttpContext c71 = s2.createContext("/redirecterror", redirectErrorHandlerSecure); |
|
746 |
delayHandler = new DelayHandler(); |
|
747 |
HttpContext c8 = s1.createContext("/delay", delayHandler); |
|
748 |
HttpContext c81 = s2.createContext("/delay", delayHandler); |
|
749 |
||
750 |
executor = Executors.newCachedThreadPool(); |
|
751 |
s1.setExecutor(executor); |
|
752 |
s2.setExecutor(executor); |
|
753 |
ctx = new SimpleSSLContext().get(); |
|
37720 | 754 |
sslparams = ctx.getSupportedSSLParameters(); |
755 |
s2.setHttpsConfigurator(new Configurator(ctx)); |
|
36131 | 756 |
s1.start(); |
757 |
s2.start(); |
|
758 |
||
759 |
port = s1.getAddress().getPort(); |
|
760 |
System.out.println("HTTP server port = " + port); |
|
761 |
httpsport = s2.getAddress().getPort(); |
|
762 |
System.out.println("HTTPS server port = " + httpsport); |
|
763 |
httproot = "http://127.0.0.1:" + port + "/"; |
|
764 |
httpsroot = "https://127.0.0.1:" + httpsport + "/"; |
|
765 |
||
766 |
proxy = new ProxyServer(0, false); |
|
767 |
proxyPort = proxy.getPort(); |
|
768 |
System.out.println("Proxy port = " + proxyPort); |
|
769 |
} |
|
770 |
||
48083 | 771 |
static class RedirectHandler implements HttpHandler { |
772 |
private final String root; |
|
773 |
private volatile int count = 0; |
|
774 |
||
775 |
RedirectHandler(String root) { |
|
776 |
this.root = root; |
|
777 |
} |
|
37720 | 778 |
|
48083 | 779 |
@Override |
780 |
public synchronized void handle(HttpExchange t) throws IOException { |
|
781 |
byte[] buf = new byte[2048]; |
|
782 |
try (InputStream is = t.getRequestBody()) { |
|
783 |
while (is.read(buf) != -1) ; |
|
784 |
} |
|
37720 | 785 |
|
48083 | 786 |
Headers responseHeaders = t.getResponseHeaders(); |
36131 | 787 |
|
48083 | 788 |
if (count++ < 1) { |
789 |
responseHeaders.add("Location", root + "/foo/" + count); |
|
790 |
} else { |
|
791 |
responseHeaders.add("Location", SmokeTest.midSizedFilename); |
|
792 |
} |
|
793 |
t.sendResponseHeaders(301, 64 * 1024); |
|
794 |
byte[] bb = new byte[1024]; |
|
795 |
OutputStream os = t.getResponseBody(); |
|
796 |
for (int i=0; i<64; i++) { |
|
797 |
os.write(bb); |
|
798 |
} |
|
799 |
os.close(); |
|
800 |
t.close(); |
|
801 |
} |
|
36131 | 802 |
|
48083 | 803 |
int count() { |
804 |
return count; |
|
805 |
} |
|
36131 | 806 |
|
48083 | 807 |
void reset() { |
808 |
count = 0; |
|
36131 | 809 |
} |
810 |
} |
|
811 |
||
48083 | 812 |
static class RedirectErrorHandler implements HttpHandler { |
813 |
private final String root; |
|
814 |
private volatile int count = 1; |
|
36131 | 815 |
|
48083 | 816 |
RedirectErrorHandler(String root) { |
817 |
this.root = root; |
|
36131 | 818 |
} |
819 |
||
48083 | 820 |
synchronized int count() { |
821 |
return count; |
|
822 |
} |
|
36131 | 823 |
|
48083 | 824 |
synchronized void increment() { |
825 |
count++; |
|
36131 | 826 |
} |
827 |
||
48083 | 828 |
@Override |
829 |
public synchronized void handle(HttpExchange t) throws IOException { |
|
830 |
try (InputStream is = t.getRequestBody()) { |
|
831 |
is.readAllBytes(); |
|
832 |
} |
|
36131 | 833 |
|
48083 | 834 |
Headers map = t.getResponseHeaders(); |
835 |
String redirect = root + "/foo/" + Integer.toString(count); |
|
836 |
increment(); |
|
837 |
map.add("Location", redirect); |
|
838 |
t.sendResponseHeaders(301, -1); |
|
839 |
t.close(); |
|
840 |
} |
|
36131 | 841 |
} |
842 |
||
48083 | 843 |
static class DelayHandler implements HttpHandler { |
844 |
||
845 |
CyclicBarrier bar1 = new CyclicBarrier(2); |
|
846 |
CyclicBarrier bar2 = new CyclicBarrier(2); |
|
847 |
CyclicBarrier bar3 = new CyclicBarrier(2); |
|
36131 | 848 |
|
48083 | 849 |
CyclicBarrier barrier1() { |
850 |
return bar1; |
|
851 |
} |
|
852 |
||
853 |
CyclicBarrier barrier2() { |
|
854 |
return bar2; |
|
36131 | 855 |
} |
856 |
||
48083 | 857 |
@Override |
858 |
public synchronized void handle(HttpExchange he) throws IOException { |
|
859 |
he.getRequestBody().readAllBytes(); |
|
860 |
try { |
|
861 |
bar1.await(); |
|
862 |
bar2.await(); |
|
863 |
} catch (Exception e) { } |
|
864 |
he.sendResponseHeaders(200, -1); // will probably fail |
|
865 |
he.close(); |
|
866 |
} |
|
36131 | 867 |
} |
868 |
||
48083 | 869 |
static class Configurator extends HttpsConfigurator { |
870 |
public Configurator(SSLContext ctx) { |
|
871 |
super(ctx); |
|
872 |
} |
|
36131 | 873 |
|
48083 | 874 |
public void configure (HttpsParameters params) { |
875 |
params.setSSLParameters (getSSLContext().getSupportedSSLParameters()); |
|
36131 | 876 |
} |
877 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
878 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
879 |
static Path getTempFile(int size) throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
880 |
File f = File.createTempFile("test", "txt"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
881 |
f.deleteOnExit(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
882 |
byte[] buf = new byte[2048]; |
48083 | 883 |
for (int i = 0; i < buf.length; i++) |
884 |
buf[i] = (byte) i; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
885 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
886 |
FileOutputStream fos = new FileOutputStream(f); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
887 |
while (size > 0) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
888 |
int amount = Math.min(size, buf.length); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
889 |
fos.write(buf, 0, amount); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
890 |
size -= amount; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
891 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
892 |
fos.close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
893 |
return f.toPath(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
894 |
} |
36131 | 895 |
} |
896 |
||
897 |
// check for simple hardcoded sequence and use remote address |
|
898 |
// to check. |
|
899 |
// First 4 requests executed in sequence (should use same connection/address) |
|
900 |
// Next 4 requests parallel (should use different addresses) |
|
901 |
// Then send 4 requests in parallel x 100 times (same four addresses used all time) |
|
902 |
||
903 |
class KeepAliveHandler implements HttpHandler { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
904 |
AtomicInteger counter = new AtomicInteger(0); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
905 |
AtomicInteger nparallel = new AtomicInteger(0); |
36131 | 906 |
|
907 |
HashSet<Integer> portSet = new HashSet<>(); |
|
908 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
909 |
int[] ports = new int[8]; |
36131 | 910 |
|
911 |
void sleep(int n) { |
|
912 |
try { |
|
913 |
Thread.sleep(n); |
|
914 |
} catch (InterruptedException e) {} |
|
915 |
} |
|
916 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
917 |
synchronized void setPort(int index, int value) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
918 |
ports[index] = value; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
919 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
920 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
921 |
synchronized int getPort(int index) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
922 |
return ports[index]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
923 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
924 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
925 |
synchronized void getPorts(int[] dest, int from) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
926 |
dest[0] = ports[from+0]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
927 |
dest[1] = ports[from+1]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
928 |
dest[2] = ports[from+2]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
929 |
dest[3] = ports[from+3]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
930 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
931 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
932 |
static CountDownLatch latch = new CountDownLatch(4); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
933 |
|
36131 | 934 |
@Override |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
935 |
public void handle (HttpExchange t) |
36131 | 936 |
throws IOException |
937 |
{ |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
938 |
int np = nparallel.incrementAndGet(); |
36131 | 939 |
int remotePort = t.getRemoteAddress().getPort(); |
940 |
String result = "OK"; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
941 |
int[] lports = new int[4]; |
36131 | 942 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
943 |
int n = counter.getAndIncrement(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
944 |
|
36131 | 945 |
/// First test |
946 |
if (n < 4) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
947 |
setPort(n, remotePort); |
36131 | 948 |
} |
949 |
if (n == 3) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
950 |
getPorts(lports, 0); |
36131 | 951 |
// check all values in ports[] are the same |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
952 |
if (lports[0] != lports[1] || lports[2] != lports[3] |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
953 |
|| lports[0] != lports[2]) { |
36131 | 954 |
result = "Error " + Integer.toString(n); |
955 |
System.out.println(result); |
|
956 |
} |
|
957 |
} |
|
958 |
// Second test |
|
959 |
if (n >=4 && n < 8) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
960 |
// delay so that this connection doesn't get reused |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
961 |
// before all 4 requests sent |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
962 |
setPort(n, remotePort); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
963 |
latch.countDown(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
964 |
try {latch.await();} catch (InterruptedException e) {} |
36131 | 965 |
} |
966 |
if (n == 7) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
967 |
getPorts(lports, 4); |
36131 | 968 |
// should be all different |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
969 |
if (lports[0] == lports[1] || lports[2] == lports[3] |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
970 |
|| lports[0] == lports[2]) { |
36131 | 971 |
result = "Error " + Integer.toString(n); |
972 |
System.out.println(result); |
|
973 |
} |
|
974 |
// setup for third test |
|
975 |
for (int i=0; i<4; i++) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
976 |
portSet.add(lports[i]); |
36131 | 977 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
978 |
System.out.printf("Ports: %d, %d, %d, %d\n", lports[0], lports[1], lports[2], lports[3]); |
36131 | 979 |
} |
980 |
// Third test |
|
981 |
if (n > 7) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
982 |
if (np > 4) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
983 |
System.err.println("XXX np = " + np); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
984 |
} |
36131 | 985 |
// just check that port is one of the ones in portSet |
986 |
if (!portSet.contains(remotePort)) { |
|
987 |
System.out.println ("UNEXPECTED REMOTE PORT " + remotePort); |
|
988 |
result = "Error " + Integer.toString(n); |
|
989 |
System.out.println(result); |
|
990 |
} |
|
991 |
} |
|
992 |
byte[] buf = new byte[2048]; |
|
993 |
||
994 |
try (InputStream is = t.getRequestBody()) { |
|
995 |
while (is.read(buf) != -1) ; |
|
996 |
} |
|
997 |
t.sendResponseHeaders(200, result.length()); |
|
998 |
OutputStream o = t.getResponseBody(); |
|
999 |
o.write(result.getBytes("US-ASCII")); |
|
1000 |
t.close(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
1001 |
nparallel.getAndDecrement(); |
36131 | 1002 |
} |
1003 |
} |