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