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
@@ -67,11 +70,21 @@ abstract public class ChunkingTest extends AbstractBasicTest {
67
70
* Tests that the custom chunked stream result in success and content returned that is unchunked
68
71
*/
69
72
@ Test ()
70
- public void testCustomChunking () throws Throwable {
71
- doTest (true );
73
+ public void testBufferLargerThanFile () throws Throwable {
74
+ doTest (new BufferedInputStream ( new FileInputStream ( getTestFile ()), 400000 ) );
72
75
}
73
76
74
- private void doTest (boolean customChunkedInputStream ) throws Exception {
77
+ @ Test ()
78
+ public void testBufferSmallThanFile () throws Throwable {
79
+ doTest (new BufferedInputStream (new FileInputStream (getTestFile ())));
80
+ }
81
+
82
+ @ Test ()
83
+ public void testDirectFile () throws Throwable {
84
+ doTest (new FileInputStream (getTestFile ()));
85
+ }
86
+
87
+ public void doTest (InputStream is ) throws Throwable {
75
88
AsyncHttpClientConfig .Builder bc = new AsyncHttpClientConfig .Builder ()//
76
89
.setAllowPoolingConnections (true )//
77
90
.setMaxConnectionsPerHost (1 )//
@@ -84,44 +97,27 @@ private void doTest(boolean customChunkedInputStream) throws Exception {
84
97
try {
85
98
RequestBuilder builder = new RequestBuilder ("POST" );
86
99
builder .setUrl (getTargetUrl ());
87
- if (customChunkedInputStream ) {
88
- // made buff in stream big enough to mark.
89
- 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" ));
90
110
} else {
91
- // made buff in stream big enough to mark.
92
- builder .setBody (new InputStreamBodyGenerator (new BufferedInputStream (new FileInputStream (getTestFile ()), 400000 )));
93
- }
94
- com .ning .http .client .Request r = builder .build ();
95
- Response res = null ;
96
-
97
- try {
98
- ListenableFuture <Response > response = client .executeRequest (r );
99
- res = response .get ();
100
- assertNotNull (res .getResponseBodyAsStream ());
101
- if (500 == res .getStatusCode ()) {
102
- System .out .println ("==============" );
103
- System .out .println ("500 response from call" );
104
- System .out .println ("Headers:" + res .getHeaders ());
105
- System .out .println ("==============" );
106
- System .out .flush ();
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" ));
110
- } else {
111
- assertEquals (readInputStreamToBytes (res .getResponseBodyAsStream ()), LARGE_IMAGE_BYTES );
112
- }
113
- } catch (Exception e ) {
114
-
115
- fail ("Exception Thrown:" + e .getMessage ());
111
+ assertEquals (readInputStreamToBytes (res .getResponseBodyAsStream ()), LARGE_IMAGE_BYTES );
116
112
}
117
113
} finally {
118
114
if (client != null )
119
115
client .close ();
120
116
}
121
117
}
122
-
123
- private byte [] readInputStreamToBytes ( InputStream stream ) {
124
- byte [] data = new byte [ 0 ];
118
+
119
+
120
+ private byte [] readInputStreamToBytes ( InputStream stream ) throws IOException {
125
121
ByteArrayOutputStream buffer = new ByteArrayOutputStream ();
126
122
try {
127
123
int nRead ;
@@ -130,32 +126,21 @@ private byte[] readInputStreamToBytes(InputStream stream) {
130
126
while ((nRead = stream .read (tmp , 0 , tmp .length )) != -1 ) {
131
127
buffer .write (tmp , 0 , nRead );
132
128
}
129
+
133
130
buffer .flush ();
134
- data = buffer .toByteArray ();
135
- } catch (Exception e ) {
131
+ return buffer .toByteArray ();
136
132
137
133
} finally {
138
134
try {
139
135
stream .close ();
140
136
} catch (Exception e2 ) {
141
137
}
142
- return data ;
143
138
}
144
139
}
145
140
146
- private static File getTestFile () {
141
+ private static File getTestFile () throws URISyntaxException {
147
142
String testResource1 = "300k.png" ;
148
-
149
- File testResource1File = null ;
150
- try {
151
- ClassLoader cl = ChunkingTest .class .getClassLoader ();
152
- URL url = cl .getResource (testResource1 );
153
- testResource1File = new File (url .toURI ());
154
- } catch (Throwable e ) {
155
- // TODO Auto-generated catch block
156
- fail ("unable to find " + testResource1 );
157
- }
158
-
159
- return testResource1File ;
143
+ URL url = ChunkingTest .class .getClassLoader ().getResource (testResource1 );
144
+ return new File (url .toURI ());
160
145
}
161
146
}
0 commit comments