1212 */
1313package org .asynchttpclient .handler ;
1414
15+ import static io .netty .handler .codec .http .HttpHeaderNames .CONTENT_LENGTH ;
1516import static org .apache .commons .io .IOUtils .copy ;
1617import static org .asynchttpclient .Dsl .*;
1718import static org .asynchttpclient .test .TestUtils .findFreePort ;
3435import org .asynchttpclient .AsyncHttpClientConfig ;
3536import org .asynchttpclient .BoundRequestBuilder ;
3637import org .asynchttpclient .Response ;
38+ import org .asynchttpclient .exception .RemotelyClosedException ;
3739import org .asynchttpclient .handler .BodyDeferringAsyncHandler .BodyDeferringInputStream ;
3840import org .eclipse .jetty .server .Request ;
3941import org .eclipse .jetty .server .handler .AbstractHandler ;
4042import org .testng .annotations .Test ;
4143
4244public class BodyDeferringAsyncHandlerTest extends AbstractBasicTest {
4345
44- // not a half gig ;) for test shorter run's sake
45- protected static final int HALF_GIG =100000 ;
46+ protected static final int CONTENT_LENGTH_VALUE =100000 ;
4647
4748public static class SlowAndBigHandler extends AbstractHandler {
4849
4950public void handle (String pathInContext ,Request request ,HttpServletRequest httpRequest ,HttpServletResponse httpResponse )throws IOException ,ServletException {
5051
5152httpResponse .setStatus (200 );
52- httpResponse .setContentLength (HALF_GIG );
53+ httpResponse .setContentLength (CONTENT_LENGTH_VALUE );
5354httpResponse .setContentType ("application/octet-stream" );
5455
5556httpResponse .flushBuffer ();
@@ -58,7 +59,7 @@ public void handle(String pathInContext, Request request, HttpServletRequest htt
5859final boolean wantSlow =httpRequest .getHeader ("X-SLOW" ) !=null ;
5960
6061OutputStream os =httpResponse .getOutputStream ();
61- for (int i =0 ;i <HALF_GIG ;i ++) {
62+ for (int i =0 ;i <CONTENT_LENGTH_VALUE ;i ++) {
6263os .write (i %255 );
6364
6465if (wantSlow ) {
@@ -70,9 +71,9 @@ public void handle(String pathInContext, Request request, HttpServletRequest htt
7071 }
7172
7273if (wantFailure ) {
73- if (i >HALF_GIG /2 ) {
74+ if (i >CONTENT_LENGTH_VALUE /2 ) {
7475// kaboom
75- // yes, response iscommited , but Jetty does aborts and
76+ // yes, response iscommitted , but Jetty does aborts and
7677// drops connection
7778httpResponse .sendError (500 );
7879break ;
@@ -120,46 +121,45 @@ public void deferredSimple() throws IOException, ExecutionException, TimeoutExce
120121Response resp =bdah .getResponse ();
121122assertNotNull (resp );
122123assertEquals (resp .getStatusCode (),HttpServletResponse .SC_OK );
123- assertEquals (resp .getHeader ("content-length" ),String .valueOf (HALF_GIG ));
124+ assertEquals (resp .getHeader ("content-length" ),String .valueOf (CONTENT_LENGTH_VALUE ));
124125// we got headers only, it's probably not all yet here (we have BIG file
125126// downloading)
126- assertTrue (cos .getByteCount () <=HALF_GIG );
127+ assertTrue (cos .getByteCount () <=CONTENT_LENGTH_VALUE );
127128
128129// now be polite and wait for body arrival too (otherwise we would be
129130// dropping the "line" on server)
130131f .get ();
131132// it all should be here now
132- assertEquals (cos .getByteCount (),HALF_GIG );
133+ assertEquals (cos .getByteCount (),CONTENT_LENGTH_VALUE );
133134 }
134135 }
135136
136- @ Test (groups ="standalone" ,enabled =false )
137- public void deferredSimpleWithFailure ()throws IOException , ExecutionException , TimeoutException , InterruptedException {
137+ @ Test (groups ="standalone" ,expectedExceptions =RemotelyClosedException . class )
138+ public void deferredSimpleWithFailure ()throws Throwable {
138139try (AsyncHttpClient client =asyncHttpClient (getAsyncHttpClientConfig ())) {
139- BoundRequestBuilder r =client .prepareGet ("http://localhost:" +port1 +"/deferredSimpleWithFailure" ).addHeader ("X-FAIL-TRANSFER" ,
140- Boolean .TRUE .toString ());
140+ BoundRequestBuilder r =client .prepareGet ("http://localhost:" +port1 +"/deferredSimpleWithFailure" ).addHeader ("X-FAIL-TRANSFER" ,Boolean .TRUE .toString ());
141141
142142CountingOutputStream cos =new CountingOutputStream ();
143143BodyDeferringAsyncHandler bdah =new BodyDeferringAsyncHandler (cos );
144144Future <Response >f =r .execute (bdah );
145145Response resp =bdah .getResponse ();
146146assertNotNull (resp );
147147assertEquals (resp .getStatusCode (),HttpServletResponse .SC_OK );
148- assertEquals (resp .getHeader ("content-length" ),String .valueOf (HALF_GIG ));
148+ assertEquals (resp .getHeader (CONTENT_LENGTH ),String .valueOf (CONTENT_LENGTH_VALUE ));
149149// we got headers only, it's probably not all yet here (we have BIG file
150150// downloading)
151- assertTrue (cos .getByteCount () <=HALF_GIG );
151+ assertTrue (cos .getByteCount () <=CONTENT_LENGTH_VALUE );
152152
153153// now be polite and wait for body arrival too (otherwise we would be
154154// dropping the "line" on server)
155155try {
156156f .get ();
157- fail ("get() should fail with IOException!" );
158- }catch (Exception e ) {
157+ }catch (ExecutionException e ) {
159158// good
159+ // it's incomplete, there was an error
160+ assertNotEquals (cos .getByteCount (),CONTENT_LENGTH_VALUE );
161+ throw e .getCause ();
160162 }
161- // it's incomplete, there was an error
162- assertNotEquals (cos .getByteCount (),HALF_GIG );
163163 }
164164 }
165165
@@ -179,7 +179,7 @@ public void deferredInputStreamTrick() throws IOException, ExecutionException, T
179179Response resp =is .getAsapResponse ();
180180assertNotNull (resp );
181181assertEquals (resp .getStatusCode (),HttpServletResponse .SC_OK );
182- assertEquals (resp .getHeader ("content-length" ),String .valueOf (HALF_GIG ));
182+ assertEquals (resp .getHeader ("content-length" ),String .valueOf (CONTENT_LENGTH_VALUE ));
183183// "consume" the body, but our code needs input stream
184184CountingOutputStream cos =new CountingOutputStream ();
185185try {
@@ -192,15 +192,14 @@ public void deferredInputStreamTrick() throws IOException, ExecutionException, T
192192// now we don't need to be polite, since consuming and closing
193193// BodyDeferringInputStream does all.
194194// it all should be here now
195- assertEquals (cos .getByteCount (),HALF_GIG );
195+ assertEquals (cos .getByteCount (),CONTENT_LENGTH_VALUE );
196196 }
197197 }
198198
199199@ Test (groups ="standalone" )
200200public void deferredInputStreamTrickWithFailure ()throws IOException ,ExecutionException ,TimeoutException ,InterruptedException {
201201try (AsyncHttpClient client =asyncHttpClient (getAsyncHttpClientConfig ())) {
202- BoundRequestBuilder r =client .prepareGet ("http://localhost:" +port1 +"/deferredInputStreamTrickWithFailure" ).addHeader ("X-FAIL-TRANSFER" ,
203- Boolean .TRUE .toString ());
202+ BoundRequestBuilder r =client .prepareGet ("http://localhost:" +port1 +"/deferredInputStreamTrickWithFailure" ).addHeader ("X-FAIL-TRANSFER" ,Boolean .TRUE .toString ());
204203PipedOutputStream pos =new PipedOutputStream ();
205204PipedInputStream pis =new PipedInputStream (pos );
206205BodyDeferringAsyncHandler bdah =new BodyDeferringAsyncHandler (pos );
@@ -212,7 +211,7 @@ public void deferredInputStreamTrickWithFailure() throws IOException, ExecutionE
212211Response resp =is .getAsapResponse ();
213212assertNotNull (resp );
214213assertEquals (resp .getStatusCode (),HttpServletResponse .SC_OK );
215- assertEquals (resp .getHeader ("content-length" ),String .valueOf (HALF_GIG ));
214+ assertEquals (resp .getHeader ("content-length" ),String .valueOf (CONTENT_LENGTH_VALUE ));
216215// "consume" the body, but our code needs input stream
217216CountingOutputStream cos =new CountingOutputStream ();
218217try {