1818import com .ning .http .client .RequestBuilder ;
1919import com .ning .http .client .Response ;
2020import com .ning .http .client .generators .InputStreamBodyGenerator ;
21+
2122import org .testng .annotations .Test ;
2223
2324import java .io .BufferedInputStream ;
2425import java .io .ByteArrayOutputStream ;
2526import java .io .File ;
2627import java .io .FileInputStream ;
28+ import java .io .IOException ;
2729import java .io .InputStream ;
30+ import java .net .URISyntaxException ;
2831import java .net .URL ;
2932import java .util .Random ;
3033
31- import static org .testng .Assert .assertNotNull ;
32- import static org .testng .AssertJUnit .assertEquals ;
33- import static org .testng .AssertJUnit .assertTrue ;
34+ import static org .testng .Assert .*;
3435import static org .testng .FileAssert .fail ;
3536
3637/**
@@ -69,64 +70,54 @@ abstract public class ChunkingTest extends AbstractBasicTest {
6970 * Tests that the custom chunked stream result in success and content returned that is unchunked
7071 */
7172@ Test ()
72- public void testCustomChunking ()throws Throwable {
73- doTest (true );
73+ public void testBufferLargerThanFile ()throws Throwable {
74+ doTest (new BufferedInputStream ( new FileInputStream ( getTestFile ()), 400000 ) );
7475 }
7576
76- private void doTest ( boolean customChunkedInputStream ) throws Exception {
77- AsyncHttpClient c = null ;
78- try {
79- AsyncHttpClientConfig . Builder bc = new AsyncHttpClientConfig . Builder ();
77+ @ Test ()
78+ public void testBufferSmallThanFile () throws Throwable {
79+ doTest ( new BufferedInputStream ( new FileInputStream ( getTestFile ())));
80+ }
8081
81- bc .setAllowPoolingConnection (true );
82- bc .setMaximumConnectionsPerHost (1 );
83- bc .setMaximumConnectionsTotal (1 );
84- bc .setConnectionTimeoutInMs (1000 );
85- bc .setRequestTimeoutInMs (1000 );
86- bc .setFollowRedirects (true );
82+ @ Test ()
83+ public void testDirectFile ()throws Throwable {
84+ doTest (new FileInputStream (getTestFile ()));
85+ }
8786
88- c =getAsyncHttpClient (bc .build ());
87+ public void doTest (InputStream is )throws Throwable {
88+ AsyncHttpClientConfig .Builder bc =new AsyncHttpClientConfig .Builder ()//
89+ .setAllowPoolingConnection (true )//
90+ .setMaximumConnectionsPerHost (1 )//
91+ .setMaximumConnectionsTotal (1 )//
92+ .setConnectionTimeoutInMs (1000 )//
93+ .setRequestTimeoutInMs (1000 )//
94+ .setFollowRedirects (true );
8995
96+ AsyncHttpClient client =getAsyncHttpClient (bc .build ());
97+ try {
9098RequestBuilder builder =new RequestBuilder ("POST" );
9199builder .setUrl (getTargetUrl ());
92- if (customChunkedInputStream ) {
93- // made buff in stream big enough to mark.
94- builder .setBody (new InputStreamBodyGenerator (new BufferedInputStream (new FileInputStream (getTestFile ()),400000 )));
100+ // made buff in stream big enough to mark.
101+ builder .setBody (new InputStreamBodyGenerator (is ));
102+
103+ ListenableFuture <Response >response =client .executeRequest (builder .build ());
104+ Response res =response .get ();
105+ assertNotNull (res .getResponseBodyAsStream ());
106+ if (500 ==res .getStatusCode ()) {
107+ assertEquals (res .getStatusCode (),500 ,"Should have 500 status code" );
108+ assertTrue (res .getHeader ("X-Exception" ).contains ("invalid.chunk.length" ),"Should have failed due to chunking" );
109+ fail ("HARD Failing the test due to provided InputStreamBodyGenerator, chunking incorrectly:" +res .getHeader ("X-Exception" ));
95110 }else {
96- // made buff in stream big enough to mark.
97- builder .setBody (new InputStreamBodyGenerator (new BufferedInputStream (new FileInputStream (getTestFile ()),400000 )));
98- }
99- com .ning .http .client .Request r =builder .build ();
100- Response res =null ;
101-
102- try {
103- ListenableFuture <Response >response =c .executeRequest (r );
104- res =response .get ();
105- assertNotNull (res .getResponseBodyAsStream ());
106- if (500 ==res .getStatusCode ()) {
107- System .out .println ("==============" );
108- System .out .println ("500 response from call" );
109- System .out .println ("Headers:" +res .getHeaders ());
110- System .out .println ("==============" );
111- System .out .flush ();
112- assertEquals ("Should have 500 status code" ,500 ,res .getStatusCode ());
113- assertTrue ("Should have failed due to chunking" ,res .getHeader ("X-Exception" ).contains ("invalid.chunk.length" ));
114- fail ("HARD Failing the test due to provided InputStreamBodyGenerator, chunking incorrectly:" +res .getHeader ("X-Exception" ));
115- }else {
116- assertEquals (LARGE_IMAGE_BYTES ,readInputStreamToBytes (res .getResponseBodyAsStream ()));
117- }
118- }catch (Exception e ) {
119-
120- fail ("Exception Thrown:" +e .getMessage ());
111+ assertEquals (readInputStreamToBytes (res .getResponseBodyAsStream ()),LARGE_IMAGE_BYTES );
121112 }
122113 }finally {
123- if (c !=null )
124- c .close ();
114+ if (client !=null )
115+ client .close ();
125116 }
126117 }
127-
128- private byte [] readInputStreamToBytes ( InputStream stream ) {
129- byte []data = new byte [ 0 ];
118+
119+
120+ private byte []readInputStreamToBytes ( InputStream stream ) throws IOException {
130121ByteArrayOutputStream buffer =new ByteArrayOutputStream ();
131122try {
132123int nRead ;
@@ -135,33 +126,21 @@ private byte[] readInputStreamToBytes(InputStream stream) {
135126while ((nRead =stream .read (tmp ,0 ,tmp .length )) != -1 ) {
136127buffer .write (tmp ,0 ,nRead );
137128 }
129+
138130buffer .flush ();
139- data =buffer .toByteArray ();
140- }catch (Exception e ) {
131+ return buffer .toByteArray ();
141132
142133 }finally {
143134try {
144135stream .close ();
145136 }catch (Exception e2 ) {
146137 }
147- return data ;
148138 }
149139 }
150140
151- private static File getTestFile () {
141+ private static File getTestFile ()throws URISyntaxException {
152142String testResource1 ="300k.png" ;
153-
154- File testResource1File =null ;
155- try {
156- ClassLoader cl =ChunkingTest .class .getClassLoader ();
157- URL url =cl .getResource (testResource1 );
158- testResource1File =new File (url .toURI ());
159- }catch (Throwable e ) {
160- // TODO Auto-generated catch block
161- fail ("unable to find " +testResource1 );
162- }
163-
164- return testResource1File ;
143+ URL url =ChunkingTest .class .getClassLoader ().getResource (testResource1 );
144+ return new File (url .toURI ());
165145 }
166-
167- }
146+ }