author | chegar |
Wed, 07 Feb 2018 21:45:37 +0000 | |
branch | http-client-branch |
changeset 56092 | fd85b2bf2b0d |
parent 56089 | 42208b2f224e |
child 56093 | 22d94c4a3641 |
permissions | -rw-r--r-- |
48083 | 1 |
/* |
2 |
* Copyright (c) 2015, 2017, 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 8161157 |
|
27 |
* @summary Test response body handlers/subscribers when there is no body |
|
28 |
* @library /lib/testlibrary http2/server |
|
29 |
* @build jdk.testlibrary.SimpleSSLContext |
|
30 |
* @modules java.base/sun.net.www.http |
|
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
31 |
* java.net.http/jdk.internal.net.http.common |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
32 |
* java.net.http/jdk.internal.net.http.frame |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
33 |
* java.net.http/jdk.internal.net.http.hpack |
48083 | 34 |
* @run testng/othervm -Djdk.internal.httpclient.debug=true -Djdk.httpclient.HttpClient.log=all NoBodyPartTwo |
35 |
*/ |
|
36 |
||
37 |
import java.io.InputStream; |
|
38 |
import java.net.URI; |
|
39 |
import java.util.Optional; |
|
40 |
import java.util.function.Consumer; |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
41 |
import java.net.http.HttpClient; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
42 |
import java.net.http.HttpRequest; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
43 |
import java.net.http.HttpResponse; |
48083 | 44 |
import org.testng.annotations.Test; |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
45 |
import static java.net.http.HttpRequest.BodyPublisher.fromString; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
46 |
import static java.net.http.HttpResponse.BodyHandler.asByteArray; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
47 |
import static java.net.http.HttpResponse.BodyHandler.asByteArrayConsumer; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
48 |
import static java.net.http.HttpResponse.BodyHandler.asInputStream; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
49 |
import static java.net.http.HttpResponse.BodyHandler.buffering; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
50 |
import static java.net.http.HttpResponse.BodyHandler.replace; |
48083 | 51 |
import static org.testng.Assert.assertEquals; |
52 |
import static org.testng.Assert.assertTrue; |
|
53 |
import static org.testng.Assert.fail; |
|
54 |
||
55 |
public class NoBodyPartTwo extends AbstractNoBody { |
|
56 |
||
57 |
volatile boolean consumerHasBeenCalled; |
|
58 |
@Test(dataProvider = "variants") |
|
59 |
public void testAsByteArrayConsumer(String uri, boolean sameClient) throws Exception { |
|
60 |
printStamp(START, "testAsByteArrayConsumer(\"%s\", %s)", uri, sameClient); |
|
61 |
HttpClient client = null; |
|
62 |
for (int i=0; i< ITERATION_COUNT; i++) { |
|
63 |
if (!sameClient || client == null) |
|
64 |
client = newHttpClient(); |
|
65 |
||
66 |
HttpRequest req = HttpRequest.newBuilder(URI.create(uri)) |
|
67 |
.PUT(fromString(SIMPLE_STRING)) |
|
68 |
.build(); |
|
69 |
Consumer<Optional<byte[]>> consumer = oba -> { |
|
70 |
consumerHasBeenCalled = true; |
|
71 |
oba.ifPresent(ba -> fail("Unexpected non-empty optional:" + ba)); |
|
72 |
}; |
|
73 |
consumerHasBeenCalled = false; |
|
74 |
client.send(req, asByteArrayConsumer(consumer)); |
|
75 |
assertTrue(consumerHasBeenCalled); |
|
76 |
} |
|
77 |
// We have created many clients here. Try to speed up their release. |
|
78 |
if (!sameClient) System.gc(); |
|
79 |
} |
|
80 |
||
81 |
@Test(dataProvider = "variants") |
|
82 |
public void testAsInputStream(String uri, boolean sameClient) throws Exception { |
|
83 |
printStamp(START, "testAsInputStream(\"%s\", %s)", uri, sameClient); |
|
84 |
HttpClient client = null; |
|
85 |
for (int i=0; i< ITERATION_COUNT; i++) { |
|
86 |
if (!sameClient || client == null) |
|
87 |
client = newHttpClient(); |
|
88 |
||
89 |
HttpRequest req = HttpRequest.newBuilder(URI.create(uri)) |
|
90 |
.PUT(fromString(SIMPLE_STRING)) |
|
91 |
.build(); |
|
92 |
HttpResponse<InputStream> response = client.send(req, asInputStream()); |
|
93 |
byte[] body = response.body().readAllBytes(); |
|
94 |
assertEquals(body.length, 0); |
|
95 |
} |
|
96 |
// We have created many clients here. Try to speed up their release. |
|
97 |
if (!sameClient) System.gc(); |
|
98 |
} |
|
99 |
||
100 |
@Test(dataProvider = "variants") |
|
101 |
public void testBuffering(String uri, boolean sameClient) throws Exception { |
|
102 |
printStamp(START, "testBuffering(\"%s\", %s)", uri, sameClient); |
|
103 |
HttpClient client = null; |
|
104 |
for (int i=0; i< ITERATION_COUNT; i++) { |
|
105 |
if (!sameClient || client == null) |
|
106 |
client = newHttpClient(); |
|
107 |
||
108 |
HttpRequest req = HttpRequest.newBuilder(URI.create(uri)) |
|
109 |
.PUT(fromString(SIMPLE_STRING)) |
|
110 |
.build(); |
|
111 |
HttpResponse<byte[]> response = client.send(req, buffering(asByteArray(), 1024)); |
|
112 |
byte[] body = response.body(); |
|
113 |
assertEquals(body.length, 0); |
|
114 |
} |
|
115 |
// We have created many clients here. Try to speed up their release. |
|
116 |
if (!sameClient) System.gc(); |
|
117 |
} |
|
118 |
||
119 |
@Test(dataProvider = "variants") |
|
120 |
public void testDiscard(String uri, boolean sameClient) throws Exception { |
|
121 |
printStamp(START, "testDiscard(\"%s\", %s)", uri, sameClient); |
|
122 |
HttpClient client = null; |
|
123 |
for (int i=0; i< ITERATION_COUNT; i++) { |
|
124 |
if (!sameClient || client == null) |
|
125 |
client = newHttpClient(); |
|
126 |
||
127 |
HttpRequest req = HttpRequest.newBuilder(URI.create(uri)) |
|
128 |
.PUT(fromString(SIMPLE_STRING)) |
|
129 |
.build(); |
|
130 |
Object obj = new Object(); |
|
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
55973
diff
changeset
|
131 |
HttpResponse<Object> response = client.send(req, replace(obj)); |
48083 | 132 |
assertEquals(response.body(), obj); |
133 |
} |
|
134 |
// We have created many clients here. Try to speed up their release. |
|
135 |
if (!sameClient) System.gc(); |
|
136 |
} |
|
137 |
} |