21
21
import java .io .OutputStream ;
22
22
import java .io .PipedInputStream ;
23
23
import java .io .PipedOutputStream ;
24
+ import java .nio .charset .StandardCharsets ;
24
25
import java .util .concurrent .ExecutionException ;
25
26
import java .util .concurrent .Future ;
26
27
import java .util .concurrent .TimeoutException ;
29
30
import javax .servlet .http .HttpServletRequest ;
30
31
import javax .servlet .http .HttpServletResponse ;
31
32
33
+ import org .apache .commons .io .IOUtils ;
32
34
import org .asynchttpclient .AbstractBasicTest ;
33
35
import org .asynchttpclient .AsyncHttpClient ;
34
36
import org .asynchttpclient .AsyncHttpClientConfig ;
35
37
import org .asynchttpclient .BoundRequestBuilder ;
38
+ import org .asynchttpclient .ListenableFuture ;
36
39
import org .asynchttpclient .Response ;
37
40
import org .asynchttpclient .handler .BodyDeferringAsyncHandler .BodyDeferringInputStream ;
38
41
import org .eclipse .jetty .server .Request ;
@@ -112,7 +115,7 @@ public AsyncHttpClientConfig getAsyncHttpClientConfig() {
112
115
@ Test (groups = "standalone" )
113
116
public void deferredSimple () throws IOException , ExecutionException , TimeoutException , InterruptedException {
114
117
try (AsyncHttpClient client = asyncHttpClient (getAsyncHttpClientConfig ())) {
115
- BoundRequestBuilder r = client .prepareGet ("http://localhost/" + port1 + "/deferredSimple" );
118
+ BoundRequestBuilder r = client .prepareGet (getTargetUrl () );
116
119
117
120
CountingOutputStream cos = new CountingOutputStream ();
118
121
BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler (cos );
@@ -136,8 +139,7 @@ public void deferredSimple() throws IOException, ExecutionException, TimeoutExce
136
139
@ Test (groups = "standalone" , enabled = false )
137
140
public void deferredSimpleWithFailure () throws IOException , ExecutionException , TimeoutException , InterruptedException {
138
141
try (AsyncHttpClient client = asyncHttpClient (getAsyncHttpClientConfig ())) {
139
- BoundRequestBuilder r = client .prepareGet ("http://localhost:" + port1 + "/deferredSimpleWithFailure" ).addHeader ("X-FAIL-TRANSFER" ,
140
- Boolean .TRUE .toString ());
142
+ BoundRequestBuilder r = client .prepareGet (getTargetUrl ()).addHeader ("X-FAIL-TRANSFER" , Boolean .TRUE .toString ());
141
143
142
144
CountingOutputStream cos = new CountingOutputStream ();
143
145
BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler (cos );
@@ -166,7 +168,7 @@ public void deferredSimpleWithFailure() throws IOException, ExecutionException,
166
168
@ Test (groups = "standalone" )
167
169
public void deferredInputStreamTrick () throws IOException , ExecutionException , TimeoutException , InterruptedException {
168
170
try (AsyncHttpClient client = asyncHttpClient (getAsyncHttpClientConfig ())) {
169
- BoundRequestBuilder r = client .prepareGet ("http://localhost/" + port1 + "/deferredInputStreamTrick" );
171
+ BoundRequestBuilder r = client .prepareGet (getTargetUrl () );
170
172
171
173
PipedOutputStream pos = new PipedOutputStream ();
172
174
PipedInputStream pis = new PipedInputStream (pos );
@@ -241,4 +243,26 @@ public void testConnectionRefused() throws IOException, ExecutionException, Time
241
243
bdah .getResponse ();
242
244
}
243
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
+ }
244
268
}
0 commit comments