author | chegar |
Thu, 07 Dec 2017 11:54:55 +0000 | |
branch | http-client-branch |
changeset 55973 | 4d9b002587db |
parent 55963 | ae2750657f98 |
parent 48083 | b1c1b4ef4be2 |
child 56053 | 8588095e95b0 |
permissions | -rw-r--r-- |
36131 | 1 |
/* |
48083 | 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 |
||
24 |
import java.io.IOException; |
|
48083 | 25 |
import java.net.URI; |
26 |
import java.util.concurrent.CompletableFuture; |
|
27 |
import javax.net.ssl.SSLContext; |
|
28 |
import javax.net.ServerSocketFactory; |
|
29 |
import javax.net.ssl.SSLServerSocketFactory; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
30 |
import jdk.incubator.http.HttpClient; |
48083 | 31 |
import jdk.incubator.http.HttpClient.Version; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
32 |
import jdk.incubator.http.HttpRequest; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
33 |
import jdk.incubator.http.HttpResponse; |
48083 | 34 |
import jdk.testlibrary.SimpleSSLContext; |
35 |
import static java.lang.System.out; |
|
36 |
import static java.lang.String.format; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
37 |
import static jdk.incubator.http.HttpResponse.BodyHandler.asString; |
36131 | 38 |
|
39 |
/** |
|
40 |
* @test |
|
41 |
* @bug 8087112 |
|
48083 | 42 |
* @library /lib/testlibrary |
43 |
* @build jdk.testlibrary.SimpleSSLContext |
|
44 |
* @build MockServer |
|
45 |
* @run main/othervm -Djdk.internal.httpclient.debug=true -Djdk.httpclient.HttpClient.log=all SplitResponse |
|
36131 | 46 |
*/ |
47 |
||
48 |
/** |
|
49 |
* Similar test to QuickResponses except that each byte of the response |
|
50 |
* is sent in a separate packet, which tests the stability of the implementation |
|
48083 | 51 |
* for receiving unusual packet sizes. Additionally, tests scenarios there |
52 |
* connections that are retrieved from the connection pool may reach EOF before |
|
53 |
* being reused. |
|
36131 | 54 |
*/ |
55 |
public class SplitResponse { |
|
56 |
||
48083 | 57 |
static String response(String body, boolean serverKeepalive) { |
58 |
StringBuilder sb = new StringBuilder(); |
|
59 |
sb.append("HTTP/1.1 200 OK\r\n"); |
|
60 |
if (!serverKeepalive) |
|
61 |
sb.append("Connection: Close\r\n"); |
|
36131 | 62 |
|
48083 | 63 |
sb.append("Content-length: ").append(body.length()).append("\r\n"); |
64 |
sb.append("\r\n"); |
|
65 |
sb.append(body); |
|
66 |
return sb.toString(); |
|
36131 | 67 |
} |
68 |
||
69 |
static final String responses[] = { |
|
70 |
"Lorem ipsum", |
|
71 |
"dolor sit amet", |
|
72 |
"consectetur adipiscing elit, sed do eiusmod tempor", |
|
73 |
"quis nostrud exercitation ullamco", |
|
74 |
"laboris nisi", |
|
75 |
"ut", |
|
76 |
"aliquip ex ea commodo consequat." + |
|
77 |
"Duis aute irure dolor in reprehenderit in voluptate velit esse" + |
|
78 |
"cillum dolore eu fugiat nulla pariatur.", |
|
79 |
"Excepteur sint occaecat cupidatat non proident." |
|
80 |
}; |
|
81 |
||
48083 | 82 |
final ServerSocketFactory factory; |
83 |
final SSLContext context; |
|
84 |
final boolean useSSL; |
|
85 |
SplitResponse(boolean useSSL) throws IOException { |
|
86 |
this.useSSL = useSSL; |
|
87 |
context = new SimpleSSLContext().get(); |
|
88 |
SSLContext.setDefault(context); |
|
89 |
factory = useSSL ? SSLServerSocketFactory.getDefault() |
|
90 |
: ServerSocketFactory.getDefault(); |
|
91 |
} |
|
92 |
||
93 |
public HttpClient newHttpClient() { |
|
94 |
HttpClient client; |
|
95 |
if (useSSL) { |
|
96 |
client = HttpClient.newBuilder() |
|
97 |
.sslContext(context) |
|
98 |
.build(); |
|
99 |
} else { |
|
100 |
client = HttpClient.newHttpClient(); |
|
101 |
} |
|
102 |
return client; |
|
103 |
} |
|
104 |
||
36131 | 105 |
public static void main(String[] args) throws Exception { |
48083 | 106 |
boolean useSSL = false; |
107 |
if (args != null && args.length == 1) { |
|
108 |
useSSL = "SSL".equals(args[0]); |
|
109 |
} |
|
110 |
SplitResponse sp = new SplitResponse(useSSL); |
|
111 |
||
112 |
for (Version version : Version.values()) { |
|
113 |
for (boolean serverKeepalive : new boolean[]{ true, false }) { |
|
114 |
// Note: the mock server doesn't support Keep-Alive, but |
|
115 |
// pretending that it might exercises code paths in and out of |
|
116 |
// the connection pool, and retry logic |
|
117 |
for (boolean async : new boolean[]{ true, false }) { |
|
118 |
sp.test(version, serverKeepalive, async); |
|
119 |
} |
|
120 |
} |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
// @Test |
|
125 |
void test(Version version, boolean serverKeepalive, boolean async) |
|
126 |
throws Exception |
|
127 |
{ |
|
128 |
out.println(format("*** version %s, serverKeepAlive: %s, async: %s ***", |
|
129 |
version, serverKeepalive, async)); |
|
130 |
MockServer server = new MockServer(0, factory); |
|
36131 | 131 |
URI uri = new URI(server.getURL()); |
48083 | 132 |
out.println("server is: " + uri); |
37816
b06d70715a79
8155888: java/net/httpclient/QuickResponses.java intermittently failed with java.util.ConcurrentModificationException
michaelm
parents:
36131
diff
changeset
|
133 |
server.start(); |
36131 | 134 |
|
48083 | 135 |
|
136 |
// The following code can be uncommented to verify that the |
|
137 |
// MockServer will reject rogue requests whose URI does not |
|
138 |
// contain "/foo/". |
|
139 |
// |
|
140 |
// Thread rogue = new Thread() { |
|
141 |
// public void run() { |
|
142 |
// try { |
|
143 |
// HttpClient client = newHttpClient(); |
|
144 |
// URI uri2 = URI.create(uri.toString().replace("/foo/","/")); |
|
145 |
// HttpRequest request = HttpRequest |
|
146 |
// .newBuilder(uri2).version(version).build(); |
|
147 |
// while (true) { |
|
148 |
// try { |
|
149 |
// client.send(request, HttpResponse.BodyHandler.asString()); |
|
150 |
// } catch (IOException ex) { |
|
151 |
// System.out.println("Client rejected " + request); |
|
152 |
// } |
|
153 |
// sleep(250); |
|
154 |
// } |
|
155 |
// } catch ( Throwable x) { |
|
156 |
// } |
|
157 |
// } |
|
158 |
// }; |
|
159 |
// rogue.setDaemon(true); |
|
160 |
// rogue.start(); |
|
161 |
||
162 |
||
163 |
HttpClient client = newHttpClient(); |
|
164 |
HttpRequest request = HttpRequest.newBuilder(uri).version(version).build(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
165 |
HttpResponse<String> r; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
166 |
CompletableFuture<HttpResponse<String>> cf1; |
36131 | 167 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
168 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
169 |
for (int i=0; i<responses.length; i++) { |
48083 | 170 |
out.println("----- iteration " + i + " -----"); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
171 |
String body = responses[i]; |
48083 | 172 |
Thread t = sendSplitResponse(response(body, serverKeepalive), server); |
36131 | 173 |
|
48083 | 174 |
if (async) { |
175 |
out.println("send async: " + request); |
|
176 |
cf1 = client.sendAsync(request, asString()); |
|
177 |
r = cf1.get(); |
|
178 |
} else { // sync |
|
179 |
out.println("send sync: " + request); |
|
180 |
r = client.send(request, asString()); |
|
181 |
} |
|
182 |
||
183 |
if (r.statusCode() != 200) |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
184 |
throw new RuntimeException("Failed"); |
36131 | 185 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
186 |
String rxbody = r.body(); |
48083 | 187 |
out.println("received " + rxbody); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
188 |
if (!rxbody.equals(body)) |
48083 | 189 |
throw new RuntimeException(format("Expected:%s, got:%s", body, rxbody)); |
190 |
||
191 |
t.join(); |
|
192 |
conn.close(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
193 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
194 |
} finally { |
48083 | 195 |
server.close(); |
36131 | 196 |
} |
197 |
System.out.println("OK"); |
|
198 |
} |
|
199 |
||
48083 | 200 |
// required for cleanup |
201 |
volatile MockServer.Connection conn; |
|
202 |
||
203 |
// Sends the response, mostly, one byte at a time with a small delay |
|
204 |
// between bytes, to encourage that each byte is read in a separate read |
|
205 |
Thread sendSplitResponse(String s, MockServer server) { |
|
36131 | 206 |
System.out.println("Sending: "); |
207 |
Thread t = new Thread(() -> { |
|
48083 | 208 |
System.out.println("Waiting for server to receive headers"); |
209 |
conn = server.activity(); |
|
210 |
System.out.println("Start sending response"); |
|
211 |
||
36131 | 212 |
try { |
213 |
int len = s.length(); |
|
48083 | 214 |
out.println("sending " + s); |
36131 | 215 |
for (int i = 0; i < len; i++) { |
216 |
String onechar = s.substring(i, i + 1); |
|
217 |
conn.send(onechar); |
|
48083 | 218 |
Thread.sleep(10); |
36131 | 219 |
} |
48083 | 220 |
out.println("sent " + s); |
36131 | 221 |
} catch (IOException | InterruptedException e) { |
48083 | 222 |
throw new RuntimeException(e); |
36131 | 223 |
} |
224 |
}); |
|
225 |
t.setDaemon(true); |
|
226 |
t.start(); |
|
48083 | 227 |
return t; |
36131 | 228 |
} |
229 |
} |