Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/httpclient/FilePublisher/FilePublisherTest.java
41153 views
1
/*
2
* Copyright (c) 2020, 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
* @bug 8235459
27
* @summary Confirm that HttpRequest.BodyPublishers#ofFile(Path)
28
* assumes the default file system
29
* @modules java.base/sun.net.www.http
30
* java.net.http/jdk.internal.net.http.common
31
* java.net.http/jdk.internal.net.http.frame
32
* java.net.http/jdk.internal.net.http.hpack
33
* jdk.httpserver
34
* @library /test/lib ../http2/server
35
* @compile ../HttpServerAdapters.java
36
* @build jdk.test.lib.net.SimpleSSLContext
37
* @run testng/othervm FilePublisherTest
38
* @run testng/othervm/java.security.policy=FilePublisherTest.policy FilePublisherTest
39
*/
40
41
import com.sun.net.httpserver.HttpServer;
42
import com.sun.net.httpserver.HttpsConfigurator;
43
import com.sun.net.httpserver.HttpsServer;
44
import jdk.test.lib.net.SimpleSSLContext;
45
import org.testng.annotations.AfterTest;
46
import org.testng.annotations.BeforeTest;
47
import org.testng.annotations.DataProvider;
48
import org.testng.annotations.Test;
49
50
import javax.net.ssl.SSLContext;
51
import java.io.IOException;
52
import java.io.InputStream;
53
import java.io.OutputStream;
54
import java.net.InetAddress;
55
import java.net.InetSocketAddress;
56
import java.net.URI;
57
import java.net.http.HttpClient;
58
import java.net.http.HttpRequest;
59
import java.net.http.HttpRequest.BodyPublishers;
60
import java.net.http.HttpResponse;
61
import java.nio.file.FileSystem;
62
import java.nio.file.FileSystems;
63
import java.nio.file.Files;
64
import java.nio.file.Path;
65
import java.util.Map;
66
67
import static java.lang.System.out;
68
import static java.net.http.HttpClient.Builder.NO_PROXY;
69
import static org.testng.Assert.assertEquals;
70
71
public class FilePublisherTest implements HttpServerAdapters {
72
SSLContext sslContext;
73
HttpServerAdapters.HttpTestServer httpTestServer; // HTTP/1.1 [ 4 servers ]
74
HttpServerAdapters.HttpTestServer httpsTestServer; // HTTPS/1.1
75
HttpServerAdapters.HttpTestServer http2TestServer; // HTTP/2 ( h2c )
76
HttpServerAdapters.HttpTestServer https2TestServer; // HTTP/2 ( h2 )
77
String httpURI;
78
String httpsURI;
79
String http2URI;
80
String https2URI;
81
82
FileSystem zipFs;
83
Path defaultFsPath;
84
Path zipFsPath;
85
86
// Default file system set up
87
static final String DEFAULT_FS_MSG = "default fs";
88
89
static Path defaultFsFile() throws Exception {
90
var file = Path.of("defaultFile.txt");
91
if (Files.notExists(file)) {
92
Files.createFile(file);
93
Files.writeString(file, DEFAULT_FS_MSG);
94
}
95
assertEquals(Files.readString(file), DEFAULT_FS_MSG);
96
return file;
97
}
98
99
@DataProvider(name = "defaultFsData")
100
public Object[][] defaultFsData() {
101
return new Object[][]{
102
{ httpURI, defaultFsPath, DEFAULT_FS_MSG, true },
103
{ httpsURI, defaultFsPath, DEFAULT_FS_MSG, true },
104
{ http2URI, defaultFsPath, DEFAULT_FS_MSG, true },
105
{ https2URI, defaultFsPath, DEFAULT_FS_MSG, true },
106
{ httpURI, defaultFsPath, DEFAULT_FS_MSG, false },
107
{ httpsURI, defaultFsPath, DEFAULT_FS_MSG, false },
108
{ http2URI, defaultFsPath, DEFAULT_FS_MSG, false },
109
{ https2URI, defaultFsPath, DEFAULT_FS_MSG, false },
110
};
111
}
112
113
@Test(dataProvider = "defaultFsData")
114
public void testDefaultFs(String uriString,
115
Path path,
116
String expectedMsg,
117
boolean sameClient) throws Exception {
118
out.printf("\n\n--- testDefaultFs(%s, %s, \"%s\", %b): starting\n",
119
uriString, path, expectedMsg, sameClient);
120
send(uriString, path, expectedMsg, sameClient);
121
}
122
123
// Zip file system set up
124
static final String ZIP_FS_MSG = "zip fs";
125
126
static FileSystem newZipFs() throws Exception {
127
Path zipFile = Path.of("file.zip");
128
return FileSystems.newFileSystem(zipFile, Map.of("create", "true"));
129
}
130
131
static Path zipFsFile(FileSystem fs) throws Exception {
132
var file = fs.getPath("fileInZip.txt");
133
if (Files.notExists(file)) {
134
Files.createFile(file);
135
Files.writeString(file, ZIP_FS_MSG);
136
}
137
assertEquals(Files.readString(file), ZIP_FS_MSG);
138
return file;
139
}
140
141
@DataProvider(name = "zipFsData")
142
public Object[][] zipFsData() {
143
return new Object[][]{
144
{ httpURI, zipFsPath, ZIP_FS_MSG, true },
145
{ httpsURI, zipFsPath, ZIP_FS_MSG, true },
146
{ http2URI, zipFsPath, ZIP_FS_MSG, true },
147
{ https2URI, zipFsPath, ZIP_FS_MSG, true },
148
{ httpURI, zipFsPath, ZIP_FS_MSG, false },
149
{ httpsURI, zipFsPath, ZIP_FS_MSG, false },
150
{ http2URI, zipFsPath, ZIP_FS_MSG, false },
151
{ https2URI, zipFsPath, ZIP_FS_MSG, false },
152
};
153
}
154
155
@Test(dataProvider = "zipFsData")
156
public void testZipFs(String uriString,
157
Path path,
158
String expectedMsg,
159
boolean sameClient) throws Exception {
160
out.printf("\n\n--- testZipFs(%s, %s, \"%s\", %b): starting\n",
161
uriString, path, expectedMsg, sameClient);
162
send(uriString, path, expectedMsg, sameClient);
163
}
164
165
private static final int ITERATION_COUNT = 3;
166
167
private void send(String uriString,
168
Path path,
169
String expectedMsg,
170
boolean sameClient)
171
throws Exception {
172
HttpClient client = null;
173
174
for (int i = 0; i < ITERATION_COUNT; i++) {
175
if (!sameClient || client == null) {
176
client = HttpClient.newBuilder()
177
.proxy(NO_PROXY)
178
.sslContext(sslContext)
179
.build();
180
}
181
var req = HttpRequest.newBuilder(URI.create(uriString))
182
.POST(BodyPublishers.ofFile(path))
183
.build();
184
var resp = client.send(req, HttpResponse.BodyHandlers.ofString());
185
out.println("Got response: " + resp);
186
out.println("Got body: " + resp.body());
187
assertEquals(resp.statusCode(), 200);
188
assertEquals(resp.body(), expectedMsg);
189
}
190
}
191
192
@BeforeTest
193
public void setup() throws Exception {
194
sslContext = new SimpleSSLContext().get();
195
if (sslContext == null)
196
throw new AssertionError("Unexpected null sslContext");
197
198
defaultFsPath = defaultFsFile();
199
zipFs = newZipFs();
200
zipFsPath = zipFsFile(zipFs);
201
202
InetSocketAddress sa =
203
new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
204
205
httpTestServer = HttpServerAdapters.HttpTestServer.of(HttpServer.create(sa, 0));
206
httpTestServer.addHandler(new HttpEchoHandler(), "/http1/echo");
207
httpURI = "http://" + httpTestServer.serverAuthority() + "/http1/echo";
208
209
HttpsServer httpsServer = HttpsServer.create(sa, 0);
210
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
211
httpsTestServer = HttpServerAdapters.HttpTestServer.of(httpsServer);
212
httpsTestServer.addHandler(new HttpEchoHandler(), "/https1/echo");
213
httpsURI = "https://" + httpsTestServer.serverAuthority() + "/https1/echo";
214
215
http2TestServer = HttpServerAdapters.HttpTestServer.of(
216
new Http2TestServer("localhost", false, 0));
217
http2TestServer.addHandler(new HttpEchoHandler(), "/http2/echo");
218
http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/echo";
219
220
https2TestServer = HttpServerAdapters.HttpTestServer.of(
221
new Http2TestServer("localhost", true, sslContext));
222
https2TestServer.addHandler(new HttpEchoHandler(), "/https2/echo");
223
https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/echo";
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();
234
httpsTestServer.stop();
235
http2TestServer.stop();
236
https2TestServer.stop();
237
zipFs.close();
238
}
239
240
static class HttpEchoHandler implements HttpServerAdapters.HttpTestHandler {
241
@Override
242
public void handle(HttpServerAdapters.HttpTestExchange t) throws IOException {
243
try (InputStream is = t.getRequestBody();
244
OutputStream os = t.getResponseBody()) {
245
byte[] bytes = is.readAllBytes();
246
t.sendResponseHeaders(200, bytes.length);
247
os.write(bytes);
248
}
249
}
250
}
251
}
252
253