1 /* |
1 /* |
2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2017, 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. |
46 import java.io.OutputStream; |
46 import java.io.OutputStream; |
47 import java.net.InetSocketAddress; |
47 import java.net.InetSocketAddress; |
48 import java.net.URI; |
48 import java.net.URI; |
49 import java.nio.ByteBuffer; |
49 import java.nio.ByteBuffer; |
50 import java.util.Arrays; |
50 import java.util.Arrays; |
|
51 import java.util.Optional; |
51 import java.util.concurrent.CompletableFuture; |
52 import java.util.concurrent.CompletableFuture; |
52 import java.util.concurrent.Flow; |
53 import java.util.concurrent.Flow; |
53 import java.util.concurrent.atomic.AtomicBoolean; |
54 import java.util.concurrent.atomic.AtomicBoolean; |
54 import java.util.concurrent.atomic.AtomicInteger; |
55 import java.util.concurrent.atomic.AtomicInteger; |
55 import java.util.concurrent.atomic.AtomicLong; |
56 import java.util.concurrent.atomic.AtomicLong; |
56 import java.util.function.Supplier; |
57 import java.util.function.Supplier; |
57 import java.util.stream.Collectors; |
58 import java.util.stream.Collectors; |
58 import javax.net.ssl.SSLContext; |
59 import javax.net.ssl.SSLContext; |
|
60 import javax.net.ssl.SSLSession; |
59 import java.net.http.HttpClient; |
61 import java.net.http.HttpClient; |
60 import java.net.http.HttpRequest; |
62 import java.net.http.HttpRequest; |
61 import java.net.http.HttpResponse; |
63 import java.net.http.HttpResponse; |
62 import jdk.testlibrary.SimpleSSLContext; |
64 import jdk.testlibrary.SimpleSSLContext; |
63 import org.testng.annotations.AfterTest; |
65 import org.testng.annotations.AfterTest; |
64 import org.testng.annotations.BeforeTest; |
66 import org.testng.annotations.BeforeTest; |
65 import org.testng.annotations.DataProvider; |
67 import org.testng.annotations.DataProvider; |
66 import org.testng.annotations.Test; |
68 import org.testng.annotations.Test; |
67 import static java.lang.System.out; |
69 import static java.lang.System.out; |
|
70 import static java.net.http.HttpClient.Version.HTTP_1_1; |
|
71 import static java.net.http.HttpClient.Version.HTTP_2; |
68 import static java.nio.charset.StandardCharsets.US_ASCII; |
72 import static java.nio.charset.StandardCharsets.US_ASCII; |
69 import static java.net.http.HttpResponse.BodyHandler.asString; |
73 import static java.net.http.HttpResponse.BodyHandler.asString; |
70 import static org.testng.Assert.assertEquals; |
74 import static org.testng.Assert.assertEquals; |
71 import static org.testng.Assert.assertTrue; |
75 import static org.testng.Assert.assertTrue; |
|
76 import static org.testng.Assert.fail; |
72 |
77 |
73 public class CustomRequestPublisher { |
78 public class CustomRequestPublisher { |
74 |
79 |
75 SSLContext sslContext; |
80 SSLContext sslContext; |
76 HttpServer httpTestServer; // HTTP/1.1 [ 4 servers ] |
81 HttpServer httpTestServer; // HTTP/1.1 [ 4 servers ] |
108 }; |
113 }; |
109 } |
114 } |
110 |
115 |
111 static final int ITERATION_COUNT = 10; |
116 static final int ITERATION_COUNT = 10; |
112 |
117 |
|
118 /** Asserts HTTP Version, and SSLSession presence when applicable. */ |
|
119 static void assertVersionAndSession(HttpResponse response, String uri) { |
|
120 if (uri.contains("http2") || uri.contains("https2")) |
|
121 assertEquals(response.version(), HTTP_2); |
|
122 else if (uri.contains("http1") || uri.contains("https1")) |
|
123 assertEquals(response.version(), HTTP_1_1); |
|
124 else |
|
125 fail("Unknown HTTP version in test for: " + uri); |
|
126 |
|
127 Optional<SSLSession> ssl = response.sslSession(); |
|
128 if (uri.contains("https")) { |
|
129 assertTrue(ssl.isPresent(), |
|
130 "Expected optional containing SSLSession but got:" + ssl); |
|
131 try { |
|
132 ssl.get().invalidate(); |
|
133 fail("SSLSession is not immutable: " + ssl.get()); |
|
134 } catch (UnsupportedOperationException expected) { } |
|
135 } else { |
|
136 assertTrue(!ssl.isPresent(), "UNEXPECTED non-empty optional:" + ssl); |
|
137 } |
|
138 } |
|
139 |
113 @Test(dataProvider = "variants") |
140 @Test(dataProvider = "variants") |
114 void test(String uri, Supplier<BodyPublisher> bpSupplier, boolean sameClient) |
141 void test(String uri, Supplier<BodyPublisher> bpSupplier, boolean sameClient) |
115 throws Exception |
142 throws Exception |
116 { |
143 { |
117 HttpClient client = null; |
144 HttpClient client = null; |
129 out.println("Got response: " + resp); |
156 out.println("Got response: " + resp); |
130 out.println("Got body: " + resp.body()); |
157 out.println("Got body: " + resp.body()); |
131 assertTrue(resp.statusCode() == 200, |
158 assertTrue(resp.statusCode() == 200, |
132 "Expected 200, got:" + resp.statusCode()); |
159 "Expected 200, got:" + resp.statusCode()); |
133 assertEquals(resp.body(), bodyPublisher.bodyAsString()); |
160 assertEquals(resp.body(), bodyPublisher.bodyAsString()); |
|
161 |
|
162 assertVersionAndSession(resp, uri); |
134 } |
163 } |
135 } |
164 } |
136 |
165 |
137 @Test(dataProvider = "variants") |
166 @Test(dataProvider = "variants") |
138 void testAsync(String uri, Supplier<BodyPublisher> bpSupplier, boolean sameClient) |
167 void testAsync(String uri, Supplier<BodyPublisher> bpSupplier, boolean sameClient) |
154 out.println("Got response: " + resp); |
183 out.println("Got response: " + resp); |
155 out.println("Got body: " + resp.body()); |
184 out.println("Got body: " + resp.body()); |
156 assertTrue(resp.statusCode() == 200, |
185 assertTrue(resp.statusCode() == 200, |
157 "Expected 200, got:" + resp.statusCode()); |
186 "Expected 200, got:" + resp.statusCode()); |
158 assertEquals(resp.body(), bodyPublisher.bodyAsString()); |
187 assertEquals(resp.body(), bodyPublisher.bodyAsString()); |
|
188 |
|
189 assertVersionAndSession(resp, uri); |
159 } |
190 } |
160 } |
191 } |
161 |
192 |
162 /** A Publisher that returns an UNKNOWN content length. */ |
193 /** A Publisher that returns an UNKNOWN content length. */ |
163 static class UnknownLengthBodyPublisher extends BodyPublisher { |
194 static class UnknownLengthBodyPublisher extends BodyPublisher { |