< prev index next >

test/jdk/java/net/httpclient/AsFileDownloadTest.java

Print this page

  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 com.sun.net.httpserver.HttpExchange;
 25 import com.sun.net.httpserver.HttpHandler;
 26 import com.sun.net.httpserver.HttpServer;
 27 import com.sun.net.httpserver.HttpsConfigurator;
 28 import com.sun.net.httpserver.HttpsServer;
 29 import java.io.IOException;
 30 import java.io.InputStream;
 31 import java.io.OutputStream;
 32 import java.io.UncheckedIOException;
 33 import java.lang.ref.ReferenceQueue;
 34 import java.lang.ref.WeakReference;
 35 import java.net.InetAddress;
 36 import java.net.InetSocketAddress;
 37 import java.net.URI;
 38 import java.net.http.HttpClient;
 39 import java.net.http.HttpHeaders;
 40 import java.net.http.HttpRequest;
 41 import java.net.http.HttpRequest.BodyPublishers;
 42 import java.net.http.HttpResponse;
 43 import java.net.http.HttpResponse.BodyHandler;
 44 import java.nio.file.Files;
 45 import java.nio.file.Path;
 46 import java.nio.file.Paths;
 47 import java.util.ArrayList;
 48 import java.util.Arrays;
 49 import java.util.List;
 50 import java.util.Locale;
 51 import java.util.Map;
 52 import javax.net.ssl.SSLContext;
 53 import jdk.test.lib.net.SimpleSSLContext;
 54 import jdk.test.lib.util.FileUtils;
 55 import jdk.httpclient.test.lib.common.HttpServerAdapters;

 56 import jdk.httpclient.test.lib.http2.Http2TestServer;
 57 import jdk.httpclient.test.lib.http2.Http2TestExchange;
 58 import jdk.httpclient.test.lib.http2.Http2Handler;
 59 import org.testng.annotations.AfterTest;
 60 import org.testng.annotations.BeforeTest;
 61 import org.testng.annotations.DataProvider;
 62 import org.testng.annotations.Test;
 63 import static java.lang.System.out;
 64 import static java.net.http.HttpResponse.BodyHandlers.ofFileDownload;
 65 import static java.nio.charset.StandardCharsets.UTF_8;
 66 import static java.nio.file.StandardOpenOption.*;
 67 import static org.testng.Assert.assertEquals;
 68 import static org.testng.Assert.assertTrue;
 69 import static org.testng.Assert.fail;
 70 
 71 /*
 72  * @test
 73  * @summary Basic test for ofFileDownload
 74  * @bug 8196965 8302475
 75  * @library /test/lib /test/jdk/java/net/httpclient/lib
 76  * @build jdk.httpclient.test.lib.http2.Http2TestServer jdk.test.lib.net.SimpleSSLContext
 77  *        jdk.test.lib.Platform jdk.test.lib.util.FileUtils

 78  * @run testng/othervm AsFileDownloadTest
 79  * @run testng/othervm/java.security.policy=AsFileDownloadTest.policy AsFileDownloadTest
 80  */
 81 public class AsFileDownloadTest {
 82 
 83     SSLContext sslContext;
 84     HttpServer httpTestServer;         // HTTP/1.1    [ 4 servers ]
 85     HttpsServer httpsTestServer;       // HTTPS/1.1
 86     Http2TestServer http2TestServer;   // HTTP/2 ( h2c )
 87     Http2TestServer https2TestServer;  // HTTP/2 ( h2  )
 88     String httpURI;
 89     String httpsURI;
 90     String http2URI;
 91     String https2URI;
 92     final ReferenceTracker TRACKER = ReferenceTracker.INSTANCE;
 93 
 94     Path tempDir;
 95 
 96     static final String[][] contentDispositionValues = new String[][] {
 97           // URI query     Content-Type header value         Expected filename

310         Files.createDirectories(tempDir.resolve("http2/afdt/"));
311         Files.createDirectories(tempDir.resolve("https2/afdt/"));
312 
313         // HTTP/1.1 server logging in case of security exceptions ( uncomment if needed )
314         //Logger logger = Logger.getLogger("com.sun.net.httpserver");
315         //ConsoleHandler ch = new ConsoleHandler();
316         //logger.setLevel(Level.ALL);
317         //ch.setLevel(Level.ALL);
318         //logger.addHandler(ch);
319 
320         sslContext = new SimpleSSLContext().get();
321         if (sslContext == null)
322             throw new AssertionError("Unexpected null sslContext");
323 
324         InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
325         httpTestServer = HttpServer.create(sa, 0);
326         httpTestServer.createContext("/http1/afdt", new Http1FileDispoHandler());
327         httpURI = "http://" + serverAuthority(httpTestServer) + "/http1/afdt";
328 
329         httpsTestServer = HttpsServer.create(sa, 0);
330         httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
331         httpsTestServer.createContext("/https1/afdt", new Http1FileDispoHandler());
332         httpsURI = "https://" + serverAuthority(httpsTestServer) + "/https1/afdt";
333 
334         http2TestServer = new Http2TestServer("localhost", false, 0);
335         http2TestServer.addHandler(new Http2FileDispoHandler(), "/http2/afdt");
336         http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/afdt";
337 
338         https2TestServer = new Http2TestServer("localhost", true, sslContext);
339         https2TestServer.addHandler(new Http2FileDispoHandler(), "/https2/afdt");
340         https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/afdt";
341 
342         httpTestServer.start();
343         httpsTestServer.start();
344         http2TestServer.start();
345         https2TestServer.start();
346     }
347 
348     @AfterTest
349     public void teardown() throws Exception {
350         httpTestServer.stop(0);

  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 com.sun.net.httpserver.HttpExchange;
 25 import com.sun.net.httpserver.HttpHandler;
 26 import com.sun.net.httpserver.HttpServer;

 27 import com.sun.net.httpserver.HttpsServer;
 28 import java.io.IOException;
 29 import java.io.InputStream;
 30 import java.io.OutputStream;
 31 import java.io.UncheckedIOException;
 32 import java.lang.ref.ReferenceQueue;
 33 import java.lang.ref.WeakReference;
 34 import java.net.InetAddress;
 35 import java.net.InetSocketAddress;
 36 import java.net.URI;
 37 import java.net.http.HttpClient;
 38 import java.net.http.HttpHeaders;
 39 import java.net.http.HttpRequest;
 40 import java.net.http.HttpRequest.BodyPublishers;
 41 import java.net.http.HttpResponse;
 42 import java.net.http.HttpResponse.BodyHandler;
 43 import java.nio.file.Files;
 44 import java.nio.file.Path;
 45 import java.nio.file.Paths;
 46 import java.util.ArrayList;
 47 import java.util.Arrays;
 48 import java.util.List;
 49 import java.util.Locale;
 50 import java.util.Map;
 51 import javax.net.ssl.SSLContext;
 52 import jdk.test.lib.net.SimpleSSLContext;
 53 import jdk.test.lib.util.FileUtils;
 54 import jdk.httpclient.test.lib.common.HttpServerAdapters;
 55 import jdk.httpclient.test.lib.common.TestServerConfigurator;
 56 import jdk.httpclient.test.lib.http2.Http2TestServer;
 57 import jdk.httpclient.test.lib.http2.Http2TestExchange;
 58 import jdk.httpclient.test.lib.http2.Http2Handler;
 59 import org.testng.annotations.AfterTest;
 60 import org.testng.annotations.BeforeTest;
 61 import org.testng.annotations.DataProvider;
 62 import org.testng.annotations.Test;
 63 import static java.lang.System.out;
 64 import static java.net.http.HttpResponse.BodyHandlers.ofFileDownload;
 65 import static java.nio.charset.StandardCharsets.UTF_8;
 66 import static java.nio.file.StandardOpenOption.*;
 67 import static org.testng.Assert.assertEquals;
 68 import static org.testng.Assert.assertTrue;
 69 import static org.testng.Assert.fail;
 70 
 71 /*
 72  * @test
 73  * @summary Basic test for ofFileDownload
 74  * @bug 8196965 8302475
 75  * @library /test/lib /test/jdk/java/net/httpclient/lib
 76  * @build jdk.httpclient.test.lib.http2.Http2TestServer jdk.test.lib.net.SimpleSSLContext
 77  *        jdk.test.lib.Platform jdk.test.lib.util.FileUtils
 78  *        jdk.httpclient.test.lib.common.TestServerConfigurator
 79  * @run testng/othervm AsFileDownloadTest
 80  * @run testng/othervm/java.security.policy=AsFileDownloadTest.policy AsFileDownloadTest
 81  */
 82 public class AsFileDownloadTest {
 83 
 84     SSLContext sslContext;
 85     HttpServer httpTestServer;         // HTTP/1.1    [ 4 servers ]
 86     HttpsServer httpsTestServer;       // HTTPS/1.1
 87     Http2TestServer http2TestServer;   // HTTP/2 ( h2c )
 88     Http2TestServer https2TestServer;  // HTTP/2 ( h2  )
 89     String httpURI;
 90     String httpsURI;
 91     String http2URI;
 92     String https2URI;
 93     final ReferenceTracker TRACKER = ReferenceTracker.INSTANCE;
 94 
 95     Path tempDir;
 96 
 97     static final String[][] contentDispositionValues = new String[][] {
 98           // URI query     Content-Type header value         Expected filename

311         Files.createDirectories(tempDir.resolve("http2/afdt/"));
312         Files.createDirectories(tempDir.resolve("https2/afdt/"));
313 
314         // HTTP/1.1 server logging in case of security exceptions ( uncomment if needed )
315         //Logger logger = Logger.getLogger("com.sun.net.httpserver");
316         //ConsoleHandler ch = new ConsoleHandler();
317         //logger.setLevel(Level.ALL);
318         //ch.setLevel(Level.ALL);
319         //logger.addHandler(ch);
320 
321         sslContext = new SimpleSSLContext().get();
322         if (sslContext == null)
323             throw new AssertionError("Unexpected null sslContext");
324 
325         InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
326         httpTestServer = HttpServer.create(sa, 0);
327         httpTestServer.createContext("/http1/afdt", new Http1FileDispoHandler());
328         httpURI = "http://" + serverAuthority(httpTestServer) + "/http1/afdt";
329 
330         httpsTestServer = HttpsServer.create(sa, 0);
331         httpsTestServer.setHttpsConfigurator(new TestServerConfigurator(sa.getAddress(), sslContext));
332         httpsTestServer.createContext("/https1/afdt", new Http1FileDispoHandler());
333         httpsURI = "https://" + serverAuthority(httpsTestServer) + "/https1/afdt";
334 
335         http2TestServer = new Http2TestServer("localhost", false, 0);
336         http2TestServer.addHandler(new Http2FileDispoHandler(), "/http2/afdt");
337         http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/afdt";
338 
339         https2TestServer = new Http2TestServer("localhost", true, sslContext);
340         https2TestServer.addHandler(new Http2FileDispoHandler(), "/https2/afdt");
341         https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/afdt";
342 
343         httpTestServer.start();
344         httpsTestServer.start();
345         http2TestServer.start();
346         https2TestServer.start();
347     }
348 
349     @AfterTest
350     public void teardown() throws Exception {
351         httpTestServer.stop(0);
< prev index next >