Path: blob/master/test/jdk/java/net/httpclient/FilePublisher/FilePublisherTest.java
41153 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 823545926* @summary Confirm that HttpRequest.BodyPublishers#ofFile(Path)27* assumes the default file system28* @modules java.base/sun.net.www.http29* java.net.http/jdk.internal.net.http.common30* java.net.http/jdk.internal.net.http.frame31* java.net.http/jdk.internal.net.http.hpack32* jdk.httpserver33* @library /test/lib ../http2/server34* @compile ../HttpServerAdapters.java35* @build jdk.test.lib.net.SimpleSSLContext36* @run testng/othervm FilePublisherTest37* @run testng/othervm/java.security.policy=FilePublisherTest.policy FilePublisherTest38*/3940import com.sun.net.httpserver.HttpServer;41import com.sun.net.httpserver.HttpsConfigurator;42import com.sun.net.httpserver.HttpsServer;43import jdk.test.lib.net.SimpleSSLContext;44import org.testng.annotations.AfterTest;45import org.testng.annotations.BeforeTest;46import org.testng.annotations.DataProvider;47import org.testng.annotations.Test;4849import javax.net.ssl.SSLContext;50import java.io.IOException;51import java.io.InputStream;52import java.io.OutputStream;53import java.net.InetAddress;54import java.net.InetSocketAddress;55import java.net.URI;56import java.net.http.HttpClient;57import java.net.http.HttpRequest;58import java.net.http.HttpRequest.BodyPublishers;59import java.net.http.HttpResponse;60import java.nio.file.FileSystem;61import java.nio.file.FileSystems;62import java.nio.file.Files;63import java.nio.file.Path;64import java.util.Map;6566import static java.lang.System.out;67import static java.net.http.HttpClient.Builder.NO_PROXY;68import static org.testng.Assert.assertEquals;6970public class FilePublisherTest implements HttpServerAdapters {71SSLContext sslContext;72HttpServerAdapters.HttpTestServer httpTestServer; // HTTP/1.1 [ 4 servers ]73HttpServerAdapters.HttpTestServer httpsTestServer; // HTTPS/1.174HttpServerAdapters.HttpTestServer http2TestServer; // HTTP/2 ( h2c )75HttpServerAdapters.HttpTestServer https2TestServer; // HTTP/2 ( h2 )76String httpURI;77String httpsURI;78String http2URI;79String https2URI;8081FileSystem zipFs;82Path defaultFsPath;83Path zipFsPath;8485// Default file system set up86static final String DEFAULT_FS_MSG = "default fs";8788static Path defaultFsFile() throws Exception {89var file = Path.of("defaultFile.txt");90if (Files.notExists(file)) {91Files.createFile(file);92Files.writeString(file, DEFAULT_FS_MSG);93}94assertEquals(Files.readString(file), DEFAULT_FS_MSG);95return file;96}9798@DataProvider(name = "defaultFsData")99public Object[][] defaultFsData() {100return new Object[][]{101{ httpURI, defaultFsPath, DEFAULT_FS_MSG, true },102{ httpsURI, defaultFsPath, DEFAULT_FS_MSG, true },103{ http2URI, defaultFsPath, DEFAULT_FS_MSG, true },104{ https2URI, defaultFsPath, DEFAULT_FS_MSG, true },105{ httpURI, defaultFsPath, DEFAULT_FS_MSG, false },106{ httpsURI, defaultFsPath, DEFAULT_FS_MSG, false },107{ http2URI, defaultFsPath, DEFAULT_FS_MSG, false },108{ https2URI, defaultFsPath, DEFAULT_FS_MSG, false },109};110}111112@Test(dataProvider = "defaultFsData")113public void testDefaultFs(String uriString,114Path path,115String expectedMsg,116boolean sameClient) throws Exception {117out.printf("\n\n--- testDefaultFs(%s, %s, \"%s\", %b): starting\n",118uriString, path, expectedMsg, sameClient);119send(uriString, path, expectedMsg, sameClient);120}121122// Zip file system set up123static final String ZIP_FS_MSG = "zip fs";124125static FileSystem newZipFs() throws Exception {126Path zipFile = Path.of("file.zip");127return FileSystems.newFileSystem(zipFile, Map.of("create", "true"));128}129130static Path zipFsFile(FileSystem fs) throws Exception {131var file = fs.getPath("fileInZip.txt");132if (Files.notExists(file)) {133Files.createFile(file);134Files.writeString(file, ZIP_FS_MSG);135}136assertEquals(Files.readString(file), ZIP_FS_MSG);137return file;138}139140@DataProvider(name = "zipFsData")141public Object[][] zipFsData() {142return new Object[][]{143{ httpURI, zipFsPath, ZIP_FS_MSG, true },144{ httpsURI, zipFsPath, ZIP_FS_MSG, true },145{ http2URI, zipFsPath, ZIP_FS_MSG, true },146{ https2URI, zipFsPath, ZIP_FS_MSG, true },147{ httpURI, zipFsPath, ZIP_FS_MSG, false },148{ httpsURI, zipFsPath, ZIP_FS_MSG, false },149{ http2URI, zipFsPath, ZIP_FS_MSG, false },150{ https2URI, zipFsPath, ZIP_FS_MSG, false },151};152}153154@Test(dataProvider = "zipFsData")155public void testZipFs(String uriString,156Path path,157String expectedMsg,158boolean sameClient) throws Exception {159out.printf("\n\n--- testZipFs(%s, %s, \"%s\", %b): starting\n",160uriString, path, expectedMsg, sameClient);161send(uriString, path, expectedMsg, sameClient);162}163164private static final int ITERATION_COUNT = 3;165166private void send(String uriString,167Path path,168String expectedMsg,169boolean sameClient)170throws Exception {171HttpClient client = null;172173for (int i = 0; i < ITERATION_COUNT; i++) {174if (!sameClient || client == null) {175client = HttpClient.newBuilder()176.proxy(NO_PROXY)177.sslContext(sslContext)178.build();179}180var req = HttpRequest.newBuilder(URI.create(uriString))181.POST(BodyPublishers.ofFile(path))182.build();183var resp = client.send(req, HttpResponse.BodyHandlers.ofString());184out.println("Got response: " + resp);185out.println("Got body: " + resp.body());186assertEquals(resp.statusCode(), 200);187assertEquals(resp.body(), expectedMsg);188}189}190191@BeforeTest192public void setup() throws Exception {193sslContext = new SimpleSSLContext().get();194if (sslContext == null)195throw new AssertionError("Unexpected null sslContext");196197defaultFsPath = defaultFsFile();198zipFs = newZipFs();199zipFsPath = zipFsFile(zipFs);200201InetSocketAddress sa =202new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);203204httpTestServer = HttpServerAdapters.HttpTestServer.of(HttpServer.create(sa, 0));205httpTestServer.addHandler(new HttpEchoHandler(), "/http1/echo");206httpURI = "http://" + httpTestServer.serverAuthority() + "/http1/echo";207208HttpsServer httpsServer = HttpsServer.create(sa, 0);209httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));210httpsTestServer = HttpServerAdapters.HttpTestServer.of(httpsServer);211httpsTestServer.addHandler(new HttpEchoHandler(), "/https1/echo");212httpsURI = "https://" + httpsTestServer.serverAuthority() + "/https1/echo";213214http2TestServer = HttpServerAdapters.HttpTestServer.of(215new Http2TestServer("localhost", false, 0));216http2TestServer.addHandler(new HttpEchoHandler(), "/http2/echo");217http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/echo";218219https2TestServer = HttpServerAdapters.HttpTestServer.of(220new Http2TestServer("localhost", true, sslContext));221https2TestServer.addHandler(new HttpEchoHandler(), "/https2/echo");222https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/echo";223224httpTestServer.start();225httpsTestServer.start();226http2TestServer.start();227https2TestServer.start();228}229230@AfterTest231public void teardown() throws Exception {232httpTestServer.stop();233httpsTestServer.stop();234http2TestServer.stop();235https2TestServer.stop();236zipFs.close();237}238239static class HttpEchoHandler implements HttpServerAdapters.HttpTestHandler {240@Override241public void handle(HttpServerAdapters.HttpTestExchange t) throws IOException {242try (InputStream is = t.getRequestBody();243OutputStream os = t.getResponseBody()) {244byte[] bytes = is.readAllBytes();245t.sendResponseHeaders(200, bytes.length);246os.write(bytes);247}248}249}250}251252253