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