< prev index next >

test/jdk/java/net/httpclient/CustomResponseSubscriber.java

Print this page

  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 /*
 25  * @test
 26  * @summary Tests response body subscribers's onComplete is not invoked before onSubscribe
 27  * @library /test/lib /test/jdk/java/net/httpclient/lib
 28  * @build jdk.test.lib.net.SimpleSSLContext jdk.httpclient.test.lib.http2.Http2TestServer

 29  * @run testng/othervm CustomResponseSubscriber
 30  */
 31 
 32 import java.io.IOException;
 33 import java.io.InputStream;
 34 import java.net.InetAddress;
 35 import java.net.InetSocketAddress;
 36 import java.net.URI;
 37 import java.nio.ByteBuffer;
 38 import java.util.List;
 39 import java.util.concurrent.CompletionStage;
 40 import java.util.concurrent.Executor;
 41 import java.util.concurrent.Executors;
 42 import java.util.concurrent.Flow;
 43 import com.sun.net.httpserver.HttpExchange;
 44 import com.sun.net.httpserver.HttpHandler;
 45 import com.sun.net.httpserver.HttpServer;
 46 import com.sun.net.httpserver.HttpsConfigurator;
 47 import com.sun.net.httpserver.HttpsServer;
 48 import java.net.http.HttpClient;
 49 import java.net.http.HttpHeaders;
 50 import java.net.http.HttpRequest;
 51 import java.net.http.HttpResponse;
 52 import java.net.http.HttpResponse.BodyHandler;
 53 import java.net.http.HttpResponse.BodySubscriber;
 54 import java.net.http.HttpResponse.BodySubscribers;


 55 import jdk.httpclient.test.lib.http2.Http2TestServer;
 56 import jdk.httpclient.test.lib.http2.Http2TestExchange;
 57 import jdk.httpclient.test.lib.http2.Http2Handler;
 58 import javax.net.ssl.SSLContext;
 59 import jdk.test.lib.net.SimpleSSLContext;
 60 import org.testng.annotations.AfterTest;
 61 import org.testng.annotations.BeforeTest;
 62 import org.testng.annotations.DataProvider;
 63 import org.testng.annotations.Test;
 64 import static java.lang.System.out;
 65 import static java.nio.charset.StandardCharsets.UTF_8;
 66 import static org.testng.Assert.assertEquals;
 67 import static org.testng.Assert.assertTrue;
 68 
 69 public class CustomResponseSubscriber {
 70 
 71     SSLContext sslContext;
 72     HttpServer httpTestServer;         // HTTP/1.1    [ 4 servers ]
 73     HttpsServer httpsTestServer;       // HTTPS/1.1
 74     Http2TestServer http2TestServer;   // HTTP/2 ( h2c )

183                 + server.getAddress().getPort();
184     }
185 
186     @BeforeTest
187     public void setup() throws Exception {
188         sslContext = new SimpleSSLContext().get();
189         if (sslContext == null)
190             throw new AssertionError("Unexpected null sslContext");
191 
192         // HTTP/1.1
193         HttpHandler h1_fixedLengthHandler = new HTTP1_FixedLengthHandler();
194         HttpHandler h1_chunkHandler = new HTTP1_ChunkedHandler();
195         InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
196         httpTestServer = HttpServer.create(sa, 0);
197         httpTestServer.createContext("/http1/fixed", h1_fixedLengthHandler);
198         httpTestServer.createContext("/http1/chunk", h1_chunkHandler);
199         httpURI_fixed = "http://" + serverAuthority(httpTestServer) + "/http1/fixed";
200         httpURI_chunk = "http://" + serverAuthority(httpTestServer) + "/http1/chunk";
201 
202         httpsTestServer = HttpsServer.create(sa, 0);
203         httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
204         httpsTestServer.createContext("/https1/fixed", h1_fixedLengthHandler);
205         httpsTestServer.createContext("/https1/chunk", h1_chunkHandler);
206         httpsURI_fixed = "https://" + serverAuthority(httpsTestServer) + "/https1/fixed";
207         httpsURI_chunk = "https://" + serverAuthority(httpsTestServer) + "/https1/chunk";
208 
209         // HTTP/2
210         Http2Handler h2_fixedLengthHandler = new HTTP2_FixedLengthHandler();
211         Http2Handler h2_chunkedHandler = new HTTP2_VariableHandler();
212 
213         http2TestServer = new Http2TestServer("localhost", false, 0);
214         http2TestServer.addHandler(h2_fixedLengthHandler, "/http2/fixed");
215         http2TestServer.addHandler(h2_chunkedHandler, "/http2/chunk");
216         http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed";
217         http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk";
218 
219         https2TestServer = new Http2TestServer("localhost", true, sslContext);
220         https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
221         https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
222         https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed";
223         https2URI_chunk = "https://" + https2TestServer.serverAuthority() + "/https2/chunk";

  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 /*
 25  * @test
 26  * @summary Tests response body subscribers's onComplete is not invoked before onSubscribe
 27  * @library /test/lib /test/jdk/java/net/httpclient/lib
 28  * @build jdk.test.lib.net.SimpleSSLContext jdk.httpclient.test.lib.http2.Http2TestServer
 29  *        jdk.httpclient.test.lib.common.TestServerConfigurator
 30  * @run testng/othervm CustomResponseSubscriber
 31  */
 32 
 33 import java.io.IOException;
 34 import java.io.InputStream;
 35 import java.net.InetAddress;
 36 import java.net.InetSocketAddress;
 37 import java.net.URI;
 38 import java.nio.ByteBuffer;
 39 import java.util.List;
 40 import java.util.concurrent.CompletionStage;
 41 import java.util.concurrent.Executor;
 42 import java.util.concurrent.Executors;
 43 import java.util.concurrent.Flow;
 44 import com.sun.net.httpserver.HttpExchange;
 45 import com.sun.net.httpserver.HttpHandler;
 46 import com.sun.net.httpserver.HttpServer;

 47 import com.sun.net.httpserver.HttpsServer;
 48 import java.net.http.HttpClient;
 49 import java.net.http.HttpHeaders;
 50 import java.net.http.HttpRequest;
 51 import java.net.http.HttpResponse;
 52 import java.net.http.HttpResponse.BodyHandler;
 53 import java.net.http.HttpResponse.BodySubscriber;
 54 import java.net.http.HttpResponse.BodySubscribers;
 55 
 56 import jdk.httpclient.test.lib.common.TestServerConfigurator;
 57 import jdk.httpclient.test.lib.http2.Http2TestServer;
 58 import jdk.httpclient.test.lib.http2.Http2TestExchange;
 59 import jdk.httpclient.test.lib.http2.Http2Handler;
 60 import javax.net.ssl.SSLContext;
 61 import jdk.test.lib.net.SimpleSSLContext;
 62 import org.testng.annotations.AfterTest;
 63 import org.testng.annotations.BeforeTest;
 64 import org.testng.annotations.DataProvider;
 65 import org.testng.annotations.Test;
 66 import static java.lang.System.out;
 67 import static java.nio.charset.StandardCharsets.UTF_8;
 68 import static org.testng.Assert.assertEquals;
 69 import static org.testng.Assert.assertTrue;
 70 
 71 public class CustomResponseSubscriber {
 72 
 73     SSLContext sslContext;
 74     HttpServer httpTestServer;         // HTTP/1.1    [ 4 servers ]
 75     HttpsServer httpsTestServer;       // HTTPS/1.1
 76     Http2TestServer http2TestServer;   // HTTP/2 ( h2c )

185                 + server.getAddress().getPort();
186     }
187 
188     @BeforeTest
189     public void setup() throws Exception {
190         sslContext = new SimpleSSLContext().get();
191         if (sslContext == null)
192             throw new AssertionError("Unexpected null sslContext");
193 
194         // HTTP/1.1
195         HttpHandler h1_fixedLengthHandler = new HTTP1_FixedLengthHandler();
196         HttpHandler h1_chunkHandler = new HTTP1_ChunkedHandler();
197         InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
198         httpTestServer = HttpServer.create(sa, 0);
199         httpTestServer.createContext("/http1/fixed", h1_fixedLengthHandler);
200         httpTestServer.createContext("/http1/chunk", h1_chunkHandler);
201         httpURI_fixed = "http://" + serverAuthority(httpTestServer) + "/http1/fixed";
202         httpURI_chunk = "http://" + serverAuthority(httpTestServer) + "/http1/chunk";
203 
204         httpsTestServer = HttpsServer.create(sa, 0);
205         httpsTestServer.setHttpsConfigurator(new TestServerConfigurator(sa.getAddress(), sslContext));
206         httpsTestServer.createContext("/https1/fixed", h1_fixedLengthHandler);
207         httpsTestServer.createContext("/https1/chunk", h1_chunkHandler);
208         httpsURI_fixed = "https://" + serverAuthority(httpsTestServer) + "/https1/fixed";
209         httpsURI_chunk = "https://" + serverAuthority(httpsTestServer) + "/https1/chunk";
210 
211         // HTTP/2
212         Http2Handler h2_fixedLengthHandler = new HTTP2_FixedLengthHandler();
213         Http2Handler h2_chunkedHandler = new HTTP2_VariableHandler();
214 
215         http2TestServer = new Http2TestServer("localhost", false, 0);
216         http2TestServer.addHandler(h2_fixedLengthHandler, "/http2/fixed");
217         http2TestServer.addHandler(h2_chunkedHandler, "/http2/chunk");
218         http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed";
219         http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk";
220 
221         https2TestServer = new Http2TestServer("localhost", true, sslContext);
222         https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
223         https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
224         https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed";
225         https2URI_chunk = "https://" + https2TestServer.serverAuthority() + "/https2/chunk";
< prev index next >