1 /* |
1 /* |
2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
20 * or visit www.oracle.com if you need additional information or have any |
20 * or visit www.oracle.com if you need additional information or have any |
21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
24 import java.io.IOException; |
24 import java.io.IOException; |
|
25 import java.net.InetAddress; |
|
26 import java.net.InetSocketAddress; |
25 import java.net.ServerSocket; |
27 import java.net.ServerSocket; |
26 import java.net.URI; |
28 import java.net.URI; |
27 import jdk.incubator.http.HttpClient; |
29 import java.net.http.HttpClient; |
28 import jdk.incubator.http.HttpRequest; |
30 import java.net.http.HttpRequest; |
29 import jdk.incubator.http.HttpResponse; |
31 import java.net.http.HttpResponse; |
30 import jdk.incubator.http.HttpTimeoutException; |
32 import java.net.http.HttpResponse.BodyHandlers; |
|
33 import java.net.http.HttpTimeoutException; |
31 import jdk.testlibrary.SimpleSSLContext; |
34 import jdk.testlibrary.SimpleSSLContext; |
32 |
35 |
33 import javax.net.ServerSocketFactory; |
36 import javax.net.ServerSocketFactory; |
34 import javax.net.ssl.SSLContext; |
37 import javax.net.ssl.SSLContext; |
35 import javax.net.ssl.SSLServerSocketFactory; |
38 import javax.net.ssl.SSLServerSocketFactory; |
38 import java.util.List; |
41 import java.util.List; |
39 import java.util.concurrent.CompletionException; |
42 import java.util.concurrent.CompletionException; |
40 import java.util.function.Function; |
43 import java.util.function.Function; |
41 |
44 |
42 import static java.lang.System.out; |
45 import static java.lang.System.out; |
43 import static jdk.incubator.http.HttpResponse.BodyHandler.discard; |
|
44 |
46 |
45 /** |
47 /** |
46 * @test |
48 * @test |
47 * @library /lib/testlibrary |
49 * @library /lib/testlibrary |
48 * @build jdk.testlibrary.SimpleSSLContext |
50 * @build jdk.testlibrary.SimpleSSLContext |
94 } |
96 } |
95 } |
97 } |
96 } |
98 } |
97 |
99 |
98 static HttpRequest.Builder DELETE(HttpRequest.Builder builder) { |
100 static HttpRequest.Builder DELETE(HttpRequest.Builder builder) { |
99 HttpRequest.BodyPublisher noBody = HttpRequest.BodyPublisher.noBody(); |
101 return builder.DELETE(); |
100 return builder.DELETE(noBody); |
|
101 } |
102 } |
102 |
103 |
103 static HttpRequest.Builder PUT(HttpRequest.Builder builder) { |
104 static HttpRequest.Builder PUT(HttpRequest.Builder builder) { |
104 HttpRequest.BodyPublisher noBody = HttpRequest.BodyPublisher.noBody(); |
105 HttpRequest.BodyPublisher noBody = HttpRequest.BodyPublishers.noBody(); |
105 return builder.PUT(noBody); |
106 return builder.PUT(noBody); |
106 } |
107 } |
107 |
108 |
108 static HttpRequest.Builder POST(HttpRequest.Builder builder) { |
109 static HttpRequest.Builder POST(HttpRequest.Builder builder) { |
109 HttpRequest.BodyPublisher noBody = HttpRequest.BodyPublisher.noBody(); |
110 HttpRequest.BodyPublisher noBody = HttpRequest.BodyPublishers.noBody(); |
110 return builder.POST(noBody); |
111 return builder.POST(noBody); |
111 } |
112 } |
112 |
113 |
113 static HttpRequest newRequest(URI uri, |
114 static HttpRequest newRequest(URI uri, |
114 Duration duration, |
115 Duration duration, |
138 HttpClient.Builder builder = HttpClient.newBuilder() |
139 HttpClient.Builder builder = HttpClient.newBuilder() |
139 .proxy(HttpClient.Builder.NO_PROXY); |
140 .proxy(HttpClient.Builder.NO_PROXY); |
140 if (version != null) builder.version(version); |
141 if (version != null) builder.version(version); |
141 HttpClient client = builder.build(); |
142 HttpClient client = builder.build(); |
142 out.printf("%ntest(version=%s, reqVersion=%s, scheme=%s)%n", version, reqVersion, scheme); |
143 out.printf("%ntest(version=%s, reqVersion=%s, scheme=%s)%n", version, reqVersion, scheme); |
143 try (ServerSocket ss = ssf.createServerSocket(0, 20)) { |
144 try (ServerSocket ss = ssf.createServerSocket()) { |
|
145 ss.setReuseAddress(false); |
|
146 ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); |
144 int port = ss.getLocalPort(); |
147 int port = ss.getLocalPort(); |
145 URI uri = new URI(scheme +"://127.0.0.1:" + port + "/"); |
148 URI uri = new URI(scheme +"://localhost:" + port + "/"); |
146 |
149 |
147 out.println("--- TESTING Async"); |
150 out.println("--- TESTING Async"); |
148 int count = 0; |
151 int count = 0; |
149 for (Duration duration : TIMEOUTS) { |
152 for (Duration duration : TIMEOUTS) { |
150 out.println(" with duration of " + duration); |
153 out.println(" with duration of " + duration); |
151 HttpRequest request = newRequest(uri, duration, reqVersion, method); |
154 HttpRequest request = newRequest(uri, duration, reqVersion, method); |
152 if (request == null) continue; |
155 if (request == null) continue; |
153 count++; |
156 count++; |
154 try { |
157 try { |
155 HttpResponse<?> resp = client.sendAsync(request, discard(null)).join(); |
158 HttpResponse<?> resp = client.sendAsync(request, BodyHandlers.discarding()).join(); |
|
159 out.println("Unexpected response for: " + request); |
|
160 out.println("\t from " + ss.getLocalSocketAddress()); |
|
161 out.println("Response is: " + resp); |
|
162 out.println("Headers: " + resp.headers().map()); |
|
163 out.println("Body (should be null): " + resp.body()); |
156 throw new RuntimeException("Unexpected response: " + resp.statusCode()); |
164 throw new RuntimeException("Unexpected response: " + resp.statusCode()); |
157 } catch (CompletionException e) { |
165 } catch (CompletionException e) { |
158 if (!(e.getCause() instanceof HttpTimeoutException)) { |
166 if (!(e.getCause() instanceof HttpTimeoutException)) { |
159 throw new RuntimeException("Unexpected exception: " + e.getCause()); |
167 throw new RuntimeException("Unexpected exception: " + e.getCause()); |
160 } else { |
168 } else { |
170 out.println(" with duration of " + duration); |
178 out.println(" with duration of " + duration); |
171 HttpRequest request = newRequest(uri, duration, reqVersion, method); |
179 HttpRequest request = newRequest(uri, duration, reqVersion, method); |
172 if (request == null) continue; |
180 if (request == null) continue; |
173 count++; |
181 count++; |
174 try { |
182 try { |
175 client.send(request, discard(null)); |
183 client.send(request, BodyHandlers.discarding()); |
176 } catch (HttpTimeoutException e) { |
184 } catch (HttpTimeoutException e) { |
177 out.println("Caught expected timeout: " + e); |
185 out.println("Caught expected timeout: " + e); |
178 } |
186 } |
179 } |
187 } |
180 assert count >= TIMEOUTS.size() -1; |
188 assert count >= TIMEOUTS.size() -1; |