1 /*
  2  * Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
  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 /*
 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 )
 75     Http2TestServer https2TestServer;  // HTTP/2 ( h2  )
 76     String httpURI_fixed;
 77     String httpURI_chunk;
 78     String httpsURI_fixed;
 79     String httpsURI_chunk;
 80     String http2URI_fixed;
 81     String http2URI_chunk;
 82     String https2URI_fixed;
 83     String https2URI_chunk;
 84 
 85     static final int ITERATION_COUNT = 10;
 86     // a shared executor helps reduce the amount of threads created by the test
 87     static final Executor executor = Executors.newCachedThreadPool();
 88 
 89     @DataProvider(name = "variants")
 90     public Object[][] variants() {
 91         return new Object[][]{
 92                 { httpURI_fixed,    false },
 93                 { httpURI_chunk,    false },
 94                 { httpsURI_fixed,   false },
 95                 { httpsURI_chunk,   false },
 96                 { http2URI_fixed,   false },
 97                 { http2URI_chunk,   false },
 98                 { https2URI_fixed,  false,},
 99                 { https2URI_chunk,  false },
100 
101                 { httpURI_fixed,    true },
102                 { httpURI_chunk,    true },
103                 { httpsURI_fixed,   true },
104                 { httpsURI_chunk,   true },
105                 { http2URI_fixed,   true },
106                 { http2URI_chunk,   true },
107                 { https2URI_fixed,  true,},
108                 { https2URI_chunk,  true },
109         };
110     }
111 
112     HttpClient newHttpClient() {
113         return HttpClient.newBuilder()
114                          .executor(executor)
115                          .sslContext(sslContext)
116                          .build();
117     }
118 
119     @Test(dataProvider = "variants")
120     public void testAsString(String uri, boolean sameClient) throws Exception {
121         HttpClient client = null;
122         for (int i=0; i< ITERATION_COUNT; i++) {
123             if (!sameClient || client == null)
124                 client = newHttpClient();
125 
126             HttpRequest req = HttpRequest.newBuilder(URI.create(uri))
127                                          .build();
128             BodyHandler<String> handler = new CRSBodyHandler();
129             HttpResponse<String> response = client.send(req, handler);
130             String body = response.body();
131             assertEquals(body, "");
132         }
133     }
134 
135     static class CRSBodyHandler implements BodyHandler<String> {
136         @Override
137         public BodySubscriber<String> apply(HttpResponse.ResponseInfo rinfo) {
138             assertEquals(rinfo.statusCode(), 200);
139             return new CRSBodySubscriber();
140         }
141     }
142 
143     static class CRSBodySubscriber implements BodySubscriber<String> {
144         private final BodySubscriber<String> ofString = BodySubscribers.ofString(UTF_8);
145         volatile boolean onSubscribeCalled;
146 
147         @Override
148         public void onSubscribe(Flow.Subscription subscription) {
149             //out.println("onSubscribe ");
150             onSubscribeCalled = true;
151             ofString.onSubscribe(subscription);
152         }
153 
154         @Override
155         public void onNext(List<ByteBuffer> item) {
156            // out.println("onNext " + item);
157             assertTrue(onSubscribeCalled);
158             ofString.onNext(item);
159         }
160 
161         @Override
162         public void onError(Throwable throwable) {
163             //out.println("onError");
164             assertTrue(onSubscribeCalled);
165             ofString.onError(throwable);
166         }
167 
168         @Override
169         public void onComplete() {
170             //out.println("onComplete");
171             assertTrue(onSubscribeCalled, "onComplete called before onSubscribe");
172             ofString.onComplete();
173         }
174 
175         @Override
176         public CompletionStage<String> getBody() {
177             return ofString.getBody();
178         }
179     }
180 
181     static String serverAuthority(HttpServer server) {
182         return InetAddress.getLoopbackAddress().getHostName() + ":"
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";
224 
225         httpTestServer.start();
226         httpsTestServer.start();
227         http2TestServer.start();
228         https2TestServer.start();
229     }
230 
231     @AfterTest
232     public void teardown() throws Exception {
233         httpTestServer.stop(0);
234         httpsTestServer.stop(0);
235         http2TestServer.stop();
236         https2TestServer.stop();
237     }
238 
239     static class HTTP1_FixedLengthHandler implements HttpHandler {
240         @Override
241         public void handle(HttpExchange t) throws IOException {
242             out.println("HTTP1_FixedLengthHandler received request to " + t.getRequestURI());
243             try (InputStream is = t.getRequestBody()) {
244                 is.readAllBytes();
245             }
246             t.sendResponseHeaders(200, -1);  //no body
247         }
248     }
249 
250     static class HTTP1_ChunkedHandler implements HttpHandler {
251         @Override
252         public void handle(HttpExchange t) throws IOException {
253             out.println("HTTP1_ChunkedHandler received request to " + t.getRequestURI());
254             try (InputStream is = t.getRequestBody()) {
255                 is.readAllBytes();
256             }
257             t.sendResponseHeaders(200, 0); // chunked
258             t.getResponseBody().close();   // no body
259         }
260     }
261 
262     static class HTTP2_FixedLengthHandler implements Http2Handler {
263         @Override
264         public void handle(Http2TestExchange t) throws IOException {
265             out.println("HTTP2_FixedLengthHandler received request to " + t.getRequestURI());
266             try (InputStream is = t.getRequestBody()) {
267                 is.readAllBytes();
268             }
269             t.sendResponseHeaders(200, 0);
270             t.getResponseBody().close();
271         }
272     }
273 
274     static class HTTP2_VariableHandler implements Http2Handler {
275         @Override
276         public void handle(Http2TestExchange t) throws IOException {
277             out.println("HTTP2_VariableHandler received request to " + t.getRequestURI());
278             try (InputStream is = t.getRequestBody()) {
279                 is.readAllBytes();
280             }
281             t.sendResponseHeaders(200, -1); // variable
282             t.getResponseBody().close();  //no body
283         }
284     }
285 }