20
20
21
21
import java .io .BufferedInputStream ;
22
22
import java .io .FileInputStream ;
23
+ import java .io .IOException ;
23
24
import java .io .InputStream ;
25
+ import java .nio .ByteBuffer ;
24
26
25
27
import org .asynchttpclient .AbstractBasicTest ;
26
28
import org .asynchttpclient .AsyncHttpClient ;
27
29
import org .asynchttpclient .AsyncHttpClientConfig ;
30
+ import org .asynchttpclient .ListenableFuture ;
28
31
import org .asynchttpclient .Request ;
29
32
import org .asynchttpclient .RequestBuilder ;
30
33
import org .asynchttpclient .Response ;
34
+ import org .asynchttpclient .request .body .generator .FeedableBodyGenerator ;
31
35
import org .asynchttpclient .request .body .generator .InputStreamBodyGenerator ;
32
36
import org .testng .annotations .Test ;
33
37
@@ -40,28 +44,27 @@ abstract public class ChunkingTest extends AbstractBasicTest {
40
44
// So we can just test the returned data is the image,
41
45
// and doesn't contain the chunked delimeters.
42
46
@ Test ()
43
- public void testBufferLargerThanFile () throws Throwable {
44
- doTest (new BufferedInputStream (new FileInputStream (LARGE_IMAGE_FILE ), 400000 ));
47
+ public void testBufferLargerThanFileWithStreamBodyGenerator () throws Throwable {
48
+ doTestWithInputStreamBodyGenerator (new BufferedInputStream (new FileInputStream (LARGE_IMAGE_FILE ), 400000 ));
45
49
}
46
50
47
51
@ Test ()
48
- public void testBufferSmallThanFile () throws Throwable {
49
- doTest (new BufferedInputStream (new FileInputStream (LARGE_IMAGE_FILE )));
52
+ public void testBufferSmallThanFileWithStreamBodyGenerator () throws Throwable {
53
+ doTestWithInputStreamBodyGenerator (new BufferedInputStream (new FileInputStream (LARGE_IMAGE_FILE )));
50
54
}
51
55
52
56
@ Test ()
53
- public void testDirectFile () throws Throwable {
54
- doTest (new FileInputStream (LARGE_IMAGE_FILE ));
57
+ public void testDirectFileWithStreamBodyGenerator () throws Throwable {
58
+ doTestWithInputStreamBodyGenerator (new FileInputStream (LARGE_IMAGE_FILE ));
55
59
}
56
60
57
- public void doTest (InputStream is ) throws Throwable {
58
- AsyncHttpClientConfig .Builder bc = new AsyncHttpClientConfig .Builder ()//
59
- .setAllowPoolingConnections (true )//
60
- .setMaxConnectionsPerHost (1 )//
61
- .setMaxConnections (1 )//
62
- .setConnectTimeout (1000 )//
63
- .setRequestTimeout (1000 )
64
- .setFollowRedirect (true );
61
+ @ Test ()
62
+ public void testDirectFileWithFeedableBodyGenerator () throws Throwable {
63
+ doTestWithFeedableBodyGenerator (new FileInputStream (LARGE_IMAGE_FILE ));
64
+ }
65
+
66
+ public void doTestWithInputStreamBodyGenerator (InputStream is ) throws Throwable {
67
+ AsyncHttpClientConfig .Builder bc = httpClientBuilder ();
65
68
66
69
try (AsyncHttpClient c = getAsyncHttpClient (bc .build ())) {
67
70
@@ -71,20 +74,68 @@ public void doTest(InputStream is) throws Throwable {
71
74
72
75
Request r = builder .build ();
73
76
74
- Response response = c .executeRequest (r ).get ();
75
- if (500 == response .getStatusCode ()) {
76
- StringBuilder sb = new StringBuilder ();
77
- sb .append ("==============\n " );
78
- sb .append ("500 response from call\n " );
79
- sb .append ("Headers:" + response .getHeaders () + "\n " );
80
- sb .append ("==============\n " );
81
- logger .debug (sb .toString ());
82
- assertEquals (response .getStatusCode (), 500 , "Should have 500 status code" );
83
- assertTrue (response .getHeader ("X-Exception" ).contains ("invalid.chunk.length" ), "Should have failed due to chunking" );
84
- fail ("HARD Failing the test due to provided InputStreamBodyGenerator, chunking incorrectly:" + response .getHeader ("X-Exception" ));
85
- } else {
86
- assertEquals (response .getResponseBodyAsBytes (), LARGE_IMAGE_BYTES );
77
+ final ListenableFuture <Response > responseFuture = c .executeRequest (r );
78
+ waitForAndAssertResponse (responseFuture );
79
+ }
80
+ }
81
+
82
+ public void doTestWithFeedableBodyGenerator (InputStream is ) throws Throwable {
83
+ AsyncHttpClientConfig .Builder bc = httpClientBuilder ();
84
+
85
+ try (AsyncHttpClient c = getAsyncHttpClient (bc .build ())) {
86
+
87
+ RequestBuilder builder = new RequestBuilder ("POST" );
88
+ builder .setUrl (getTargetUrl ());
89
+ final FeedableBodyGenerator feedableBodyGenerator = new FeedableBodyGenerator ();
90
+ builder .setBody (feedableBodyGenerator );
91
+
92
+ Request r = builder .build ();
93
+
94
+ final ListenableFuture <Response > responseFuture = c .executeRequest (r );
95
+
96
+ feed (feedableBodyGenerator , is );
97
+
98
+ waitForAndAssertResponse (responseFuture );
99
+ }
100
+ }
101
+
102
+ private void feed (FeedableBodyGenerator feedableBodyGenerator , InputStream is ) throws IOException {
103
+ try (InputStream inputStream = is ) {
104
+ byte [] buffer = new byte [512 ];
105
+ for (int i =0 ; (i = inputStream .read (buffer )) > -1 ;) {
106
+ byte [] chunk = new byte [i ];
107
+ System .arraycopy (buffer , 0 , chunk , 0 , i );
108
+ feedableBodyGenerator .feed (ByteBuffer .wrap (chunk ), false );
87
109
}
88
110
}
111
+ feedableBodyGenerator .feed (ByteBuffer .allocate (0 ), true );
112
+
113
+ }
114
+
115
+ private AsyncHttpClientConfig .Builder httpClientBuilder () {
116
+ return new AsyncHttpClientConfig .Builder ()//
117
+ .setAllowPoolingConnections (true )//
118
+ .setMaxConnectionsPerHost (1 )//
119
+ .setMaxConnections (1 )//
120
+ .setConnectTimeout (1000 )//
121
+ .setRequestTimeout (1000 )
122
+ .setFollowRedirect (true );
123
+ }
124
+
125
+ private void waitForAndAssertResponse (ListenableFuture <Response > responseFuture ) throws InterruptedException , java .util .concurrent .ExecutionException , IOException {
126
+ Response response = responseFuture .get ();
127
+ if (500 == response .getStatusCode ()) {
128
+ StringBuilder sb = new StringBuilder ();
129
+ sb .append ("==============\n " );
130
+ sb .append ("500 response from call\n " );
131
+ sb .append ("Headers:" + response .getHeaders () + "\n " );
132
+ sb .append ("==============\n " );
133
+ logger .debug (sb .toString ());
134
+ assertEquals (response .getStatusCode (), 500 , "Should have 500 status code" );
135
+ assertTrue (response .getHeader ("X-Exception" ).contains ("invalid.chunk.length" ), "Should have failed due to chunking" );
136
+ fail ("HARD Failing the test due to provided InputStreamBodyGenerator, chunking incorrectly:" + response .getHeader ("X-Exception" ));
137
+ } else {
138
+ assertEquals (response .getResponseBodyAsBytes (), LARGE_IMAGE_BYTES );
139
+ }
89
140
}
90
141
}
0 commit comments