12
12
*/
13
13
package org .asynchttpclient .handler ;
14
14
15
+ import static io .netty .handler .codec .http .HttpHeaderNames .CONTENT_LENGTH ;
15
16
import static org .apache .commons .io .IOUtils .copy ;
16
17
import static org .asynchttpclient .Dsl .*;
17
18
import static org .asynchttpclient .test .TestUtils .findFreePort ;
34
35
import org .asynchttpclient .AsyncHttpClientConfig ;
35
36
import org .asynchttpclient .BoundRequestBuilder ;
36
37
import org .asynchttpclient .Response ;
38
+ import org .asynchttpclient .exception .RemotelyClosedException ;
37
39
import org .asynchttpclient .handler .BodyDeferringAsyncHandler .BodyDeferringInputStream ;
38
40
import org .eclipse .jetty .server .Request ;
39
41
import org .eclipse .jetty .server .handler .AbstractHandler ;
40
42
import org .testng .annotations .Test ;
41
43
42
44
public class BodyDeferringAsyncHandlerTest extends AbstractBasicTest {
43
45
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 ;
46
47
47
48
public static class SlowAndBigHandler extends AbstractHandler {
48
49
49
50
public void handle (String pathInContext , Request request , HttpServletRequest httpRequest , HttpServletResponse httpResponse ) throws IOException , ServletException {
50
51
51
52
httpResponse .setStatus (200 );
52
- httpResponse .setContentLength (HALF_GIG );
53
+ httpResponse .setContentLength (CONTENT_LENGTH_VALUE );
53
54
httpResponse .setContentType ("application/octet-stream" );
54
55
55
56
httpResponse .flushBuffer ();
@@ -58,7 +59,7 @@ public void handle(String pathInContext, Request request, HttpServletRequest htt
58
59
final boolean wantSlow = httpRequest .getHeader ("X-SLOW" ) != null ;
59
60
60
61
OutputStream os = httpResponse .getOutputStream ();
61
- for (int i = 0 ; i < HALF_GIG ; i ++) {
62
+ for (int i = 0 ; i < CONTENT_LENGTH_VALUE ; i ++) {
62
63
os .write (i % 255 );
63
64
64
65
if (wantSlow ) {
@@ -70,9 +71,9 @@ public void handle(String pathInContext, Request request, HttpServletRequest htt
70
71
}
71
72
72
73
if (wantFailure ) {
73
- if (i > HALF_GIG / 2 ) {
74
+ if (i > CONTENT_LENGTH_VALUE / 2 ) {
74
75
// kaboom
75
- // yes, response is commited , but Jetty does aborts and
76
+ // yes, response is committed , but Jetty does aborts and
76
77
// drops connection
77
78
httpResponse .sendError (500 );
78
79
break ;
@@ -120,46 +121,45 @@ public void deferredSimple() throws IOException, ExecutionException, TimeoutExce
120
121
Response resp = bdah .getResponse ();
121
122
assertNotNull (resp );
122
123
assertEquals (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 ));
124
125
// we got headers only, it's probably not all yet here (we have BIG file
125
126
// downloading)
126
- assertTrue (cos .getByteCount () <= HALF_GIG );
127
+ assertTrue (cos .getByteCount () <= CONTENT_LENGTH_VALUE );
127
128
128
129
// now be polite and wait for body arrival too (otherwise we would be
129
130
// dropping the "line" on server)
130
131
f .get ();
131
132
// it all should be here now
132
- assertEquals (cos .getByteCount (), HALF_GIG );
133
+ assertEquals (cos .getByteCount (), CONTENT_LENGTH_VALUE );
133
134
}
134
135
}
135
136
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 {
138
139
try (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 ());
141
141
142
142
CountingOutputStream cos = new CountingOutputStream ();
143
143
BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler (cos );
144
144
Future <Response > f = r .execute (bdah );
145
145
Response resp = bdah .getResponse ();
146
146
assertNotNull (resp );
147
147
assertEquals (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 ));
149
149
// we got headers only, it's probably not all yet here (we have BIG file
150
150
// downloading)
151
- assertTrue (cos .getByteCount () <= HALF_GIG );
151
+ assertTrue (cos .getByteCount () <= CONTENT_LENGTH_VALUE );
152
152
153
153
// now be polite and wait for body arrival too (otherwise we would be
154
154
// dropping the "line" on server)
155
155
try {
156
156
f .get ();
157
- fail ("get() should fail with IOException!" );
158
- } catch (Exception e ) {
157
+ } catch (ExecutionException e ) {
159
158
// good
159
+ // it's incomplete, there was an error
160
+ assertNotEquals (cos .getByteCount (), CONTENT_LENGTH_VALUE );
161
+ throw e .getCause ();
160
162
}
161
- // it's incomplete, there was an error
162
- assertNotEquals (cos .getByteCount (), HALF_GIG );
163
163
}
164
164
}
165
165
@@ -179,7 +179,7 @@ public void deferredInputStreamTrick() throws IOException, ExecutionException, T
179
179
Response resp = is .getAsapResponse ();
180
180
assertNotNull (resp );
181
181
assertEquals (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 ));
183
183
// "consume" the body, but our code needs input stream
184
184
CountingOutputStream cos = new CountingOutputStream ();
185
185
try {
@@ -192,15 +192,14 @@ public void deferredInputStreamTrick() throws IOException, ExecutionException, T
192
192
// now we don't need to be polite, since consuming and closing
193
193
// BodyDeferringInputStream does all.
194
194
// it all should be here now
195
- assertEquals (cos .getByteCount (), HALF_GIG );
195
+ assertEquals (cos .getByteCount (), CONTENT_LENGTH_VALUE );
196
196
}
197
197
}
198
198
199
199
@ Test (groups = "standalone" )
200
200
public void deferredInputStreamTrickWithFailure () throws IOException , ExecutionException , TimeoutException , InterruptedException {
201
201
try (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 ());
204
203
PipedOutputStream pos = new PipedOutputStream ();
205
204
PipedInputStream pis = new PipedInputStream (pos );
206
205
BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler (pos );
@@ -212,7 +211,7 @@ public void deferredInputStreamTrickWithFailure() throws IOException, ExecutionE
212
211
Response resp = is .getAsapResponse ();
213
212
assertNotNull (resp );
214
213
assertEquals (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 ));
216
215
// "consume" the body, but our code needs input stream
217
216
CountingOutputStream cos = new CountingOutputStream ();
218
217
try {
0 commit comments