18
18
import com .ning .http .client .RequestBuilder ;
19
19
import com .ning .http .client .Response ;
20
20
import com .ning .http .client .generators .InputStreamBodyGenerator ;
21
+
21
22
import org .testng .annotations .Test ;
22
23
23
24
import java .io .BufferedInputStream ;
24
25
import java .io .ByteArrayOutputStream ;
25
26
import java .io .File ;
26
27
import java .io .FileInputStream ;
28
+ import java .io .IOException ;
27
29
import java .io .InputStream ;
30
+ import java .net .URISyntaxException ;
28
31
import java .net .URL ;
29
32
import java .util .Random ;
30
33
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 .*;
34
35
import static org .testng .FileAssert .fail ;
35
36
36
37
/**
@@ -69,64 +70,54 @@ abstract public class ChunkingTest extends AbstractBasicTest {
69
70
* Tests that the custom chunked stream result in success and content returned that is unchunked
70
71
*/
71
72
@ Test ()
72
- public void testCustomChunking () throws Throwable {
73
- doTest (true );
73
+ public void testBufferLargerThanFile () throws Throwable {
74
+ doTest (new BufferedInputStream ( new FileInputStream ( getTestFile ()), 400000 ) );
74
75
}
75
76
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
+ }
80
81
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
+ }
87
86
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 );
89
95
96
+ AsyncHttpClient client = getAsyncHttpClient (bc .build ());
97
+ try {
90
98
RequestBuilder builder = new RequestBuilder ("POST" );
91
99
builder .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" ));
95
110
} 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 );
121
112
}
122
113
} finally {
123
- if (c != null )
124
- c .close ();
114
+ if (client != null )
115
+ client .close ();
125
116
}
126
117
}
127
-
128
- private byte [] readInputStreamToBytes ( InputStream stream ) {
129
- byte [] data = new byte [ 0 ];
118
+
119
+
120
+ private byte [] readInputStreamToBytes ( InputStream stream ) throws IOException {
130
121
ByteArrayOutputStream buffer = new ByteArrayOutputStream ();
131
122
try {
132
123
int nRead ;
@@ -135,33 +126,21 @@ private byte[] readInputStreamToBytes(InputStream stream) {
135
126
while ((nRead = stream .read (tmp , 0 , tmp .length )) != -1 ) {
136
127
buffer .write (tmp , 0 , nRead );
137
128
}
129
+
138
130
buffer .flush ();
139
- data = buffer .toByteArray ();
140
- } catch (Exception e ) {
131
+ return buffer .toByteArray ();
141
132
142
133
} finally {
143
134
try {
144
135
stream .close ();
145
136
} catch (Exception e2 ) {
146
137
}
147
- return data ;
148
138
}
149
139
}
150
140
151
- private static File getTestFile () {
141
+ private static File getTestFile () throws URISyntaxException {
152
142
String 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 ());
165
145
}
166
-
167
- }
146
+ }
0 commit comments