23
23
import java .io .OutputStream ;
24
24
import java .io .PipedInputStream ;
25
25
import java .io .PipedOutputStream ;
26
+ import java .nio .charset .StandardCharsets ;
26
27
import java .util .concurrent .ExecutionException ;
27
28
import java .util .concurrent .Future ;
28
29
import java .util .concurrent .TimeoutException ;
31
32
import javax .servlet .http .HttpServletRequest ;
32
33
import javax .servlet .http .HttpServletResponse ;
33
34
35
+ import org .apache .commons .io .IOUtils ;
34
36
import org .asynchttpclient .AbstractBasicTest ;
35
37
import org .asynchttpclient .AsyncHttpClient ;
36
38
import org .asynchttpclient .AsyncHttpClientConfig ;
37
39
import org .asynchttpclient .BoundRequestBuilder ;
40
+ import org .asynchttpclient .ListenableFuture ;
38
41
import org .asynchttpclient .Response ;
39
42
import org .asynchttpclient .exception .RemotelyClosedException ;
40
43
import org .asynchttpclient .handler .BodyDeferringAsyncHandler .BodyDeferringInputStream ;
@@ -114,7 +117,7 @@ public AsyncHttpClientConfig getAsyncHttpClientConfig() {
114
117
@ Test (groups = "standalone" )
115
118
public void deferredSimple () throws IOException , ExecutionException , TimeoutException , InterruptedException {
116
119
try (AsyncHttpClient client = asyncHttpClient (getAsyncHttpClientConfig ())) {
117
- BoundRequestBuilder r = client .prepareGet ("http://localhost/" + port1 + "/deferredSimple" );
120
+ BoundRequestBuilder r = client .prepareGet (getTargetUrl () );
118
121
119
122
CountingOutputStream cos = new CountingOutputStream ();
120
123
BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler (cos );
@@ -138,7 +141,7 @@ public void deferredSimple() throws IOException, ExecutionException, TimeoutExce
138
141
@ Test (groups = "standalone" , expectedExceptions = RemotelyClosedException .class )
139
142
public void deferredSimpleWithFailure () throws Throwable {
140
143
try (AsyncHttpClient client = asyncHttpClient (getAsyncHttpClientConfig ())) {
141
- BoundRequestBuilder r = client .prepareGet ("http://localhost/" + port1 + "/deferredSimpleWithFailure" ).addHeader ("X-FAIL-TRANSFER" , Boolean .TRUE .toString ());
144
+ BoundRequestBuilder r = client .prepareGet (getTargetUrl () ).addHeader ("X-FAIL-TRANSFER" , Boolean .TRUE .toString ());
142
145
143
146
CountingOutputStream cos = new CountingOutputStream ();
144
147
BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler (cos );
@@ -167,7 +170,7 @@ public void deferredSimpleWithFailure() throws Throwable {
167
170
@ Test (groups = "standalone" )
168
171
public void deferredInputStreamTrick () throws IOException , ExecutionException , TimeoutException , InterruptedException {
169
172
try (AsyncHttpClient client = asyncHttpClient (getAsyncHttpClientConfig ())) {
170
- BoundRequestBuilder r = client .prepareGet ("http://localhost/" + port1 + "/deferredInputStreamTrick" );
173
+ BoundRequestBuilder r = client .prepareGet (getTargetUrl () );
171
174
172
175
PipedOutputStream pos = new PipedOutputStream ();
173
176
PipedInputStream pis = new PipedInputStream (pos );
@@ -200,7 +203,7 @@ public void deferredInputStreamTrick() throws IOException, ExecutionException, T
200
203
@ Test (groups = "standalone" , expectedExceptions = RemotelyClosedException .class )
201
204
public void deferredInputStreamTrickWithFailure () throws Throwable {
202
205
try (AsyncHttpClient client = asyncHttpClient (getAsyncHttpClientConfig ())) {
203
- BoundRequestBuilder r = client .prepareGet ("http://localhost/" + port1 + "/deferredInputStreamTrickWithFailure" ).addHeader ("X-FAIL-TRANSFER" , Boolean .TRUE .toString ());
206
+ BoundRequestBuilder r = client .prepareGet (getTargetUrl () ).addHeader ("X-FAIL-TRANSFER" , Boolean .TRUE .toString ());
204
207
PipedOutputStream pos = new PipedOutputStream ();
205
208
PipedInputStream pis = new PipedInputStream (pos );
206
209
BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler (pos );
@@ -240,4 +243,26 @@ public void testConnectionRefused() throws IOException, ExecutionException, Time
240
243
bdah .getResponse ();
241
244
}
242
245
}
246
+
247
+ @ Test (groups = "standalone" )
248
+ public void testPipedStreams () throws Exception {
249
+ try (AsyncHttpClient client = asyncHttpClient (getAsyncHttpClientConfig ())) {
250
+ PipedOutputStream pout = new PipedOutputStream ();
251
+ try (PipedInputStream pin = new PipedInputStream (pout )) {
252
+ BodyDeferringAsyncHandler handler = new BodyDeferringAsyncHandler (pout );
253
+ ListenableFuture <Response > respFut = client .prepareGet (getTargetUrl ()).execute (handler );
254
+
255
+ Response resp = handler .getResponse ();
256
+
257
+ if (resp .getStatusCode () == 200 ) {
258
+ try (BodyDeferringInputStream is = new BodyDeferringInputStream (respFut , handler , pin )) {
259
+ String body = IOUtils .toString (is , StandardCharsets .UTF_8 );
260
+ assertTrue (body .contains ("ABCDEF" ));
261
+ }
262
+ } else {
263
+ throw new IOException ("HTTP error " + resp .getStatusCode ());
264
+ }
265
+ }
266
+ }
267
+ }
243
268
}
0 commit comments