author | chegar |
Wed, 07 Feb 2018 14:17:24 +0000 | |
branch | http-client-branch |
changeset 56089 | 42208b2f224e |
parent 56082 | 1da51fab3032 |
child 56167 | 96fa4f49a9ff |
permissions | -rw-r--r-- |
36131 | 1 |
/* |
45286
cd809e28c082
8180887: move FileUtils to top level testlibrary
iignatyev
parents:
44854
diff
changeset
|
2 |
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
36131 | 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 |
||
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
24 |
/* |
48083 | 25 |
* @test |
26 |
* @bug 8087112 |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
27 |
* @modules java.net.http |
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
28 |
* java.logging |
38883 | 29 |
* jdk.httpserver |
45286
cd809e28c082
8180887: move FileUtils to top level testlibrary
iignatyev
parents:
44854
diff
changeset
|
30 |
* @library /lib/testlibrary/ /test/lib |
36131 | 31 |
* @compile ../../../com/sun/net/httpserver/LogFilter.java |
45535 | 32 |
* @compile ../../../com/sun/net/httpserver/EchoHandler.java |
36131 | 33 |
* @compile ../../../com/sun/net/httpserver/FileServerHandler.java |
45467
99c87a16a8e4
8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents:
45286
diff
changeset
|
34 |
* @build jdk.test.lib.Platform |
99c87a16a8e4
8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents:
45286
diff
changeset
|
35 |
* @build jdk.test.lib.util.FileUtils |
37720 | 36 |
* @build LightWeightHttpServer |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
37 |
* @build jdk.testlibrary.SimpleSSLContext |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
38 |
* @run testng/othervm RequestBodyTest |
36131 | 39 |
*/ |
40 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
41 |
import java.io.*; |
36131 | 42 |
import java.net.URI; |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
43 |
import java.net.http.HttpClient; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
44 |
import java.net.http.HttpRequest; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
45 |
import java.net.http.HttpResponse; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
46 |
import java.net.http.HttpResponse.BodyHandler; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
47 |
import java.nio.charset.Charset; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
48 |
import java.nio.charset.StandardCharsets; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
49 |
import java.nio.file.Files; |
36131 | 50 |
import java.nio.file.Path; |
51 |
import java.nio.file.Paths; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
52 |
import java.util.ArrayList; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
53 |
import java.util.Arrays; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
54 |
import java.util.List; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
55 |
import java.util.Optional; |
48083 | 56 |
import java.util.concurrent.ConcurrentHashMap; |
57 |
import java.util.function.Consumer; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
58 |
import java.util.function.Supplier; |
36131 | 59 |
import javax.net.ssl.SSLContext; |
45286
cd809e28c082
8180887: move FileUtils to top level testlibrary
iignatyev
parents:
44854
diff
changeset
|
60 |
import jdk.test.lib.util.FileUtils; |
48083 | 61 |
import static java.lang.System.out; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
62 |
import static java.nio.charset.StandardCharsets.*; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
63 |
import static java.nio.file.StandardOpenOption.*; |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
64 |
import static java.net.http.HttpRequest.BodyPublisher.*; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
65 |
import static java.net.http.HttpResponse.BodyHandler.*; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
66 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
67 |
import org.testng.annotations.AfterTest; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
68 |
import org.testng.annotations.BeforeTest; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
69 |
import org.testng.annotations.DataProvider; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
70 |
import org.testng.annotations.Test; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
71 |
import static org.testng.Assert.*; |
36131 | 72 |
|
73 |
public class RequestBodyTest { |
|
74 |
||
48083 | 75 |
static final String fileroot = System.getProperty("test.src", ".") + "/docs"; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
76 |
static final String midSizedFilename = "/files/notsobigfile.txt"; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
77 |
static final String smallFilename = "/files/smallfile.txt"; |
48083 | 78 |
final ConcurrentHashMap<String,Throwable> failures = new ConcurrentHashMap<>(); |
36131 | 79 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
80 |
HttpClient client; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
81 |
String httpURI; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
82 |
String httpsURI; |
36131 | 83 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
84 |
enum RequestBody { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
85 |
BYTE_ARRAY, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
86 |
BYTE_ARRAY_OFFSET, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
87 |
BYTE_ARRAYS, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
88 |
FILE, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
89 |
INPUTSTREAM, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
90 |
STRING, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
91 |
STRING_WITH_CHARSET |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
92 |
} |
36131 | 93 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
94 |
enum ResponseBody { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
95 |
BYTE_ARRAY, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
96 |
BYTE_ARRAY_CONSUMER, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
97 |
DISCARD, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
98 |
FILE, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
99 |
FILE_WITH_OPTION, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
100 |
STRING, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
101 |
STRING_WITH_CHARSET, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
102 |
} |
36131 | 103 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
104 |
@BeforeTest |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
105 |
public void setup() throws Exception { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
106 |
LightWeightHttpServer.initServer(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
107 |
httpURI = LightWeightHttpServer.httproot + "echo/foo"; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
108 |
httpsURI = LightWeightHttpServer.httpsroot + "echo/foo"; |
36131 | 109 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
110 |
SSLContext ctx = LightWeightHttpServer.ctx; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
111 |
client = HttpClient.newBuilder() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
112 |
.sslContext(ctx) |
44854
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
42460
diff
changeset
|
113 |
.version(HttpClient.Version.HTTP_1_1) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
114 |
.followRedirects(HttpClient.Redirect.ALWAYS) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
115 |
.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
116 |
} |
36131 | 117 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
118 |
@AfterTest |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
119 |
public void teardown() throws Exception { |
48083 | 120 |
try { |
121 |
LightWeightHttpServer.stop(); |
|
122 |
} finally { |
|
123 |
System.out.println("RequestBodyTest: " + failures.size() + " failures"); |
|
124 |
int i = 0; |
|
125 |
for (String key: failures.keySet()) { |
|
126 |
System.out.println("test" + key + " failed: " + failures.get(key)); |
|
127 |
failures.get(key).printStackTrace(System.out); |
|
128 |
if (i++ > 3) { |
|
129 |
System.out.println("..... other failures not printed ...."); |
|
130 |
break; |
|
131 |
} |
|
132 |
} |
|
133 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
134 |
} |
36131 | 135 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
136 |
@DataProvider |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
137 |
public Object[][] exchanges() throws Exception { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
138 |
List<Object[]> values = new ArrayList<>(); |
36131 | 139 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
140 |
for (boolean async : new boolean[] { false, true }) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
141 |
for (String uri : new String[] { httpURI, httpsURI }) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
142 |
for (String file : new String[] { smallFilename, midSizedFilename }) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
143 |
for (RequestBody requestBodyType : RequestBody.values()) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
144 |
for (ResponseBody responseBodyType : ResponseBody.values()) |
48083 | 145 |
for (boolean bufferResponseBody : new boolean[] { false, true }) |
146 |
values.add(new Object[] |
|
147 |
{uri, requestBodyType, responseBodyType, file, async, bufferResponseBody}); |
|
36131 | 148 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
149 |
return values.stream().toArray(Object[][]::new); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
150 |
} |
36131 | 151 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
152 |
@Test(dataProvider = "exchanges") |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
153 |
void exchange(String target, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
154 |
RequestBody requestBodyType, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
155 |
ResponseBody responseBodyType, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
156 |
String file, |
48083 | 157 |
boolean async, |
158 |
boolean bufferResponseBody) |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
159 |
throws Exception |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
160 |
{ |
48083 | 161 |
try { |
162 |
Path filePath = Paths.get(fileroot + file); |
|
163 |
URI uri = new URI(target); |
|
164 |
||
165 |
HttpRequest request = createRequest(uri, requestBodyType, filePath); |
|
36131 | 166 |
|
48083 | 167 |
checkResponse(client, request, requestBodyType, responseBodyType, filePath, async, bufferResponseBody); |
168 |
} catch (Exception | Error x) { |
|
169 |
Object[] params = new Object[] { |
|
170 |
target, requestBodyType, responseBodyType, |
|
171 |
file, "async=" + async, "buffer=" + bufferResponseBody |
|
172 |
}; |
|
173 |
failures.put(java.util.Arrays.toString(params), x); |
|
174 |
throw x; |
|
175 |
} |
|
36131 | 176 |
} |
177 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
178 |
static final int DEFAULT_OFFSET = 10; |
48083 | 179 |
static final int DEFAULT_LENGTH_FACTOR = 4/5; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
180 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
181 |
HttpRequest createRequest(URI uri, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
182 |
RequestBody requestBodyType, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
183 |
Path file) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
184 |
throws IOException |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
185 |
{ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
186 |
HttpRequest.Builder rb = HttpRequest.newBuilder(uri); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
187 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
188 |
String filename = file.toFile().getAbsolutePath(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
189 |
byte[] fileAsBytes = getFileBytes(filename); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
190 |
String fileAsString = new String(fileAsBytes, UTF_8); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
191 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
192 |
switch (requestBodyType) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
193 |
case BYTE_ARRAY: |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
194 |
rb.POST(fromByteArray(fileAsBytes)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
195 |
break; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
196 |
case BYTE_ARRAY_OFFSET: |
48083 | 197 |
rb.POST(fromByteArray(fileAsBytes, |
198 |
DEFAULT_OFFSET, |
|
199 |
fileAsBytes.length * DEFAULT_LENGTH_FACTOR)); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
200 |
break; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
201 |
case BYTE_ARRAYS: |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
202 |
Iterable<byte[]> iterable = Arrays.asList(fileAsBytes); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
203 |
rb.POST(fromByteArrays(iterable)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
204 |
break; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
205 |
case FILE: |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
206 |
rb.POST(fromFile(file)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
207 |
break; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
208 |
case INPUTSTREAM: |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
209 |
rb.POST(fromInputStream(fileInputStreamSupplier(file))); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
210 |
break; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
211 |
case STRING: |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
212 |
rb.POST(fromString(fileAsString)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
213 |
break; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
214 |
case STRING_WITH_CHARSET: |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
215 |
rb.POST(fromString(new String(fileAsBytes), Charset.defaultCharset())); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
216 |
break; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
217 |
default: |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
218 |
throw new AssertionError("Unknown request body:" + requestBodyType); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
219 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
220 |
return rb.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
221 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
222 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
223 |
void checkResponse(HttpClient client, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
224 |
HttpRequest request, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
225 |
RequestBody requestBodyType, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
226 |
ResponseBody responseBodyType, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
227 |
Path file, |
48083 | 228 |
boolean async, |
229 |
boolean bufferResponseBody) |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
230 |
throws InterruptedException, IOException |
36131 | 231 |
{ |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
232 |
String filename = file.toFile().getAbsolutePath(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
233 |
byte[] fileAsBytes = getFileBytes(filename); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
234 |
if (requestBodyType == RequestBody.BYTE_ARRAY_OFFSET) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
235 |
// Truncate the expected response body, if only a portion was sent |
48083 | 236 |
int length = DEFAULT_OFFSET + (fileAsBytes.length * DEFAULT_LENGTH_FACTOR); |
237 |
fileAsBytes = Arrays.copyOfRange(fileAsBytes, DEFAULT_OFFSET, length); |
|
36131 | 238 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
239 |
String fileAsString = new String(fileAsBytes, UTF_8); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
240 |
Path tempFile = Paths.get("RequestBodyTest.tmp"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
241 |
FileUtils.deleteFileIfExistsWithRetry(tempFile); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
242 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
243 |
switch (responseBodyType) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
244 |
case BYTE_ARRAY: |
48083 | 245 |
BodyHandler<byte[]> bh = asByteArray(); |
246 |
if (bufferResponseBody) bh = buffering(bh, 50); |
|
247 |
HttpResponse<byte[]> bar = getResponse(client, request, bh, async); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
248 |
assertEquals(bar.statusCode(), 200); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
249 |
assertEquals(bar.body(), fileAsBytes); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
250 |
break; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
251 |
case BYTE_ARRAY_CONSUMER: |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
252 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
48083 | 253 |
Consumer<Optional<byte[]>> consumer = o -> consumerBytes(o, baos); |
254 |
BodyHandler<Void> bh1 = asByteArrayConsumer(consumer); |
|
255 |
if (bufferResponseBody) bh1 = buffering(bh1, 49); |
|
256 |
HttpResponse<Void> v = getResponse(client, request, bh1, async); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
257 |
byte[] ba = baos.toByteArray(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
258 |
assertEquals(v.statusCode(), 200); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
259 |
assertEquals(ba, fileAsBytes); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
260 |
break; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
261 |
case DISCARD: |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
262 |
Object o = new Object(); |
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
55973
diff
changeset
|
263 |
BodyHandler<Object> bh2 = replace(o); |
48083 | 264 |
if (bufferResponseBody) bh2 = buffering(bh2, 51); |
265 |
HttpResponse<Object> or = getResponse(client, request, bh2, async); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
266 |
assertEquals(or.statusCode(), 200); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
267 |
assertSame(or.body(), o); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
268 |
break; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
269 |
case FILE: |
48083 | 270 |
BodyHandler<Path> bh3 = asFile(tempFile); |
271 |
if (bufferResponseBody) bh3 = buffering(bh3, 48); |
|
272 |
HttpResponse<Path> fr = getResponse(client, request, bh3, async); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
273 |
assertEquals(fr.statusCode(), 200); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
274 |
assertEquals(Files.size(tempFile), fileAsString.length()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
275 |
assertEquals(Files.readAllBytes(tempFile), fileAsBytes); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
276 |
break; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
277 |
case FILE_WITH_OPTION: |
48083 | 278 |
BodyHandler<Path> bh4 = asFile(tempFile, CREATE_NEW, WRITE); |
279 |
if (bufferResponseBody) bh4 = buffering(bh4, 52); |
|
280 |
fr = getResponse(client, request, bh4, async); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
281 |
assertEquals(fr.statusCode(), 200); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
282 |
assertEquals(Files.size(tempFile), fileAsString.length()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
283 |
assertEquals(Files.readAllBytes(tempFile), fileAsBytes); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
284 |
break; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
285 |
case STRING: |
48083 | 286 |
BodyHandler<String> bh5 = asString(); |
287 |
if(bufferResponseBody) bh5 = buffering(bh5, 47); |
|
288 |
HttpResponse<String> sr = getResponse(client, request, bh5, async); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
289 |
assertEquals(sr.statusCode(), 200); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
290 |
assertEquals(sr.body(), fileAsString); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
291 |
break; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
292 |
case STRING_WITH_CHARSET: |
48083 | 293 |
BodyHandler<String> bh6 = asString(StandardCharsets.UTF_8); |
294 |
if (bufferResponseBody) bh6 = buffering(bh6, 53); |
|
295 |
HttpResponse<String> r = getResponse(client, request, bh6, async); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
296 |
assertEquals(r.statusCode(), 200); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
297 |
assertEquals(r.body(), fileAsString); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
298 |
break; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
299 |
default: |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
300 |
throw new AssertionError("Unknown response body:" + responseBodyType); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
301 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
302 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
303 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
304 |
static <T> HttpResponse<T> getResponse(HttpClient client, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
305 |
HttpRequest request, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
306 |
HttpResponse.BodyHandler<T> handler, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
307 |
boolean async) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
308 |
throws InterruptedException, IOException |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
309 |
{ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
310 |
if (!async) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
311 |
return client.send(request, handler); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
312 |
else |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
313 |
return client.sendAsync(request, handler).join(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
314 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
315 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
316 |
static byte[] getFileBytes(String path) throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
317 |
try (FileInputStream fis = new FileInputStream(path); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
318 |
BufferedInputStream bis = new BufferedInputStream(fis); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
319 |
ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
320 |
bis.transferTo(baos); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
321 |
return baos.toByteArray(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
322 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
323 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
324 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
325 |
static Supplier<FileInputStream> fileInputStreamSupplier(Path f) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
326 |
return new Supplier<>() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
327 |
Path file = f; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
328 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
329 |
public FileInputStream get() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
330 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
331 |
return new FileInputStream(file.toFile()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
332 |
} catch (FileNotFoundException x) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
333 |
throw new UncheckedIOException(x); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
334 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
335 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
336 |
}; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
337 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
338 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
339 |
static void consumerBytes(Optional<byte[]> bytes, ByteArrayOutputStream baos) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
340 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
341 |
if (bytes.isPresent()) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
342 |
baos.write(bytes.get()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
343 |
} catch (IOException x) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
344 |
throw new UncheckedIOException(x); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
345 |
} |
36131 | 346 |
} |
48083 | 347 |
|
348 |
// --- |
|
349 |
||
350 |
/* Main entry point for standalone testing of the main functional test. */ |
|
351 |
public static void main(String... args) throws Exception { |
|
352 |
RequestBodyTest t = new RequestBodyTest(); |
|
353 |
t.setup(); |
|
354 |
int count = 0; |
|
355 |
try { |
|
356 |
for (Object[] objs : t.exchanges()) { |
|
357 |
count++; |
|
358 |
out.printf("********* iteration: %d %s %s %s %s %s %s *********%n", |
|
359 |
count, objs[0], objs[1], objs[2], objs[3], objs[4], objs[5]); |
|
360 |
t.exchange((String) objs[0], |
|
361 |
(RequestBody) objs[1], |
|
362 |
(ResponseBody) objs[2], |
|
363 |
(String) objs[3], |
|
364 |
(boolean) objs[4], |
|
365 |
(boolean) objs[5]); |
|
366 |
} |
|
367 |
} finally { |
|
368 |
t.teardown(); |
|
369 |
} |
|
370 |
} |
|
36131 | 371 |
} |