2323import java .io .OutputStream ;
2424import java .io .PipedInputStream ;
2525import java .io .PipedOutputStream ;
26+ import java .nio .charset .StandardCharsets ;
2627import java .util .concurrent .ExecutionException ;
2728import java .util .concurrent .Future ;
2829import java .util .concurrent .TimeoutException ;
3132import javax .servlet .http .HttpServletRequest ;
3233import javax .servlet .http .HttpServletResponse ;
3334
35+ import org .apache .commons .io .IOUtils ;
3436import org .asynchttpclient .AbstractBasicTest ;
3537import org .asynchttpclient .AsyncHttpClient ;
3638import org .asynchttpclient .AsyncHttpClientConfig ;
3739import org .asynchttpclient .BoundRequestBuilder ;
40+ import org .asynchttpclient .ListenableFuture ;
3841import org .asynchttpclient .Response ;
3942import org .asynchttpclient .exception .RemotelyClosedException ;
4043import org .asynchttpclient .handler .BodyDeferringAsyncHandler .BodyDeferringInputStream ;
@@ -114,7 +117,7 @@ public AsyncHttpClientConfig getAsyncHttpClientConfig() {
114117 @ Test (groups = "standalone" )
115118 public void deferredSimple () throws IOException , ExecutionException , TimeoutException , InterruptedException {
116119 try (AsyncHttpClient client = asyncHttpClient (getAsyncHttpClientConfig ())) {
117- BoundRequestBuilder r = client .prepareGet ("http://localhost:" + port1 + "/deferredSimple" );
120+ BoundRequestBuilder r = client .prepareGet (getTargetUrl () );
118121
119122 CountingOutputStream cos = new CountingOutputStream ();
120123 BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler (cos );
@@ -138,7 +141,7 @@ public void deferredSimple() throws IOException, ExecutionException, TimeoutExce
138141 @ Test (groups = "standalone" , expectedExceptions = RemotelyClosedException .class )
139142 public void deferredSimpleWithFailure () throws Throwable {
140143 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 ());
142145
143146 CountingOutputStream cos = new CountingOutputStream ();
144147 BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler (cos );
@@ -167,7 +170,7 @@ public void deferredSimpleWithFailure() throws Throwable {
167170 @ Test (groups = "standalone" )
168171 public void deferredInputStreamTrick () throws IOException , ExecutionException , TimeoutException , InterruptedException {
169172 try (AsyncHttpClient client = asyncHttpClient (getAsyncHttpClientConfig ())) {
170- BoundRequestBuilder r = client .prepareGet ("http://localhost:" + port1 + "/deferredInputStreamTrick" );
173+ BoundRequestBuilder r = client .prepareGet (getTargetUrl () );
171174
172175 PipedOutputStream pos = new PipedOutputStream ();
173176 PipedInputStream pis = new PipedInputStream (pos );
@@ -200,7 +203,7 @@ public void deferredInputStreamTrick() throws IOException, ExecutionException, T
200203 @ Test (groups = "standalone" , expectedExceptions = RemotelyClosedException .class )
201204 public void deferredInputStreamTrickWithFailure () throws Throwable {
202205 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 ());
204207 PipedOutputStream pos = new PipedOutputStream ();
205208 PipedInputStream pis = new PipedInputStream (pos );
206209 BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler (pos );
@@ -240,4 +243,26 @@ public void testConnectionRefused() throws IOException, ExecutionException, Time
240243 bdah .getResponse ();
241244 }
242245 }
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+ }
243268}
0 commit comments