48 import java.io.InputStream; |
48 import java.io.InputStream; |
49 import java.io.OutputStream; |
49 import java.io.OutputStream; |
50 import java.io.UncheckedIOException; |
50 import java.io.UncheckedIOException; |
51 import java.net.InetSocketAddress; |
51 import java.net.InetSocketAddress; |
52 import java.net.URI; |
52 import java.net.URI; |
|
53 import java.net.http.HttpClient; |
|
54 import java.net.http.HttpRequest; |
|
55 import java.net.http.HttpRequest.BodyPublishers; |
|
56 import java.net.http.HttpResponse; |
|
57 import java.net.http.HttpResponse.BodyHandler; |
53 import java.nio.file.Files; |
58 import java.nio.file.Files; |
54 import java.nio.file.Path; |
59 import java.nio.file.Path; |
55 import java.nio.file.Paths; |
60 import java.nio.file.Paths; |
56 import java.util.ArrayList; |
61 import java.util.ArrayList; |
57 import java.util.Arrays; |
62 import java.util.Arrays; |
58 import java.util.List; |
63 import java.util.List; |
59 import javax.net.ssl.SSLContext; |
64 import javax.net.ssl.SSLContext; |
60 import java.net.http.HttpClient; |
|
61 import java.net.http.HttpRequest; |
|
62 import java.net.http.HttpResponse; |
|
63 import java.net.http.HttpResponse.BodyHandler; |
|
64 import jdk.testlibrary.SimpleSSLContext; |
65 import jdk.testlibrary.SimpleSSLContext; |
65 import jdk.test.lib.util.FileUtils; |
66 import jdk.test.lib.util.FileUtils; |
66 import org.testng.annotations.AfterTest; |
67 import org.testng.annotations.AfterTest; |
67 import org.testng.annotations.BeforeTest; |
68 import org.testng.annotations.BeforeTest; |
68 import org.testng.annotations.DataProvider; |
69 import org.testng.annotations.DataProvider; |
69 import org.testng.annotations.Test; |
70 import org.testng.annotations.Test; |
70 import static java.lang.System.out; |
71 import static java.lang.System.out; |
71 import static java.net.http.HttpRequest.BodyPublisher.fromString; |
72 import static java.net.http.HttpResponse.BodyHandlers.ofFileDownload; |
72 import static java.net.http.HttpResponse.BodyHandler.asFileDownload; |
|
73 import static java.nio.charset.StandardCharsets.UTF_8; |
73 import static java.nio.charset.StandardCharsets.UTF_8; |
74 import static java.nio.file.StandardOpenOption.*; |
74 import static java.nio.file.StandardOpenOption.*; |
75 import static org.testng.Assert.assertEquals; |
75 import static org.testng.Assert.assertEquals; |
76 import static org.testng.Assert.assertTrue; |
76 import static org.testng.Assert.assertTrue; |
77 import static org.testng.Assert.fail; |
77 import static org.testng.Assert.fail; |
163 out.printf("test(%s, %s, %s): starting", uriString, contentDispositionValue, expectedFilename); |
163 out.printf("test(%s, %s, %s): starting", uriString, contentDispositionValue, expectedFilename); |
164 HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); |
164 HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); |
165 |
165 |
166 URI uri = URI.create(uriString); |
166 URI uri = URI.create(uriString); |
167 HttpRequest request = HttpRequest.newBuilder(uri) |
167 HttpRequest request = HttpRequest.newBuilder(uri) |
168 .POST(fromString("May the luck of the Irish be with you!")) |
168 .POST(BodyPublishers.ofString("May the luck of the Irish be with you!")) |
169 .build(); |
169 .build(); |
170 |
170 |
171 BodyHandler bh = asFileDownload(tempDir.resolve(uri.getPath().substring(1)), |
171 BodyHandler bh = ofFileDownload(tempDir.resolve(uri.getPath().substring(1)), |
172 CREATE, TRUNCATE_EXISTING, WRITE); |
172 CREATE, TRUNCATE_EXISTING, WRITE); |
173 HttpResponse<Path> response = client.send(request, bh); |
173 HttpResponse<Path> response = client.send(request, bh); |
174 |
174 |
175 out.println("Got response: " + response); |
175 out.println("Got response: " + response); |
176 out.println("Got body Path: " + response.body()); |
176 out.println("Got body Path: " + response.body()); |
232 out.printf("negativeTest(%s, %s): starting", uriString, contentDispositionValue); |
232 out.printf("negativeTest(%s, %s): starting", uriString, contentDispositionValue); |
233 HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); |
233 HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); |
234 |
234 |
235 URI uri = URI.create(uriString); |
235 URI uri = URI.create(uriString); |
236 HttpRequest request = HttpRequest.newBuilder(uri) |
236 HttpRequest request = HttpRequest.newBuilder(uri) |
237 .POST(fromString("Does not matter")) |
237 .POST(BodyPublishers.ofString("Does not matter")) |
238 .build(); |
238 .build(); |
239 |
239 |
240 BodyHandler bh = asFileDownload(tempDir, CREATE, TRUNCATE_EXISTING, WRITE); |
240 BodyHandler bh = ofFileDownload(tempDir, CREATE, TRUNCATE_EXISTING, WRITE); |
241 try { |
241 try { |
242 HttpResponse<Path> response = client.send(request, bh); |
242 HttpResponse<Path> response = client.send(request, bh); |
243 fail("UNEXPECTED response: " + response + ", path:" + response.body()); |
243 fail("UNEXPECTED response: " + response + ", path:" + response.body()); |
244 } catch (UncheckedIOException | IOException ioe) { |
244 } catch (UncheckedIOException | IOException ioe) { |
245 System.out.println("Caught expected: " + ioe); |
245 System.out.println("Caught expected: " + ioe); |