17
17
import static org .asynchttpclient .test .TestUtils .*;
18
18
import static org .testng .Assert .*;
19
19
20
- import java .io .ByteArrayOutputStream ;
21
- import java .io .File ;
22
- import java .io .FileInputStream ;
23
- import java .io .FileNotFoundException ;
24
- import java .io .FileOutputStream ;
25
- import java .io .IOException ;
26
- import java .io .InputStream ;
27
- import java .io .OutputStream ;
28
- import java .io .Writer ;
20
+ import java .io .*;
29
21
import java .util .ArrayList ;
30
22
import java .util .Arrays ;
31
23
import java .util .List ;
47
39
import org .asynchttpclient .AbstractBasicTest ;
48
40
import org .asynchttpclient .AsyncHttpClient ;
49
41
import org .asynchttpclient .Request ;
50
- import org .asynchttpclient .RequestBuilder ;
51
42
import org .asynchttpclient .Response ;
52
43
import org .eclipse .jetty .server .Server ;
53
44
import org .eclipse .jetty .server .ServerConnector ;
62
53
* @author dominict
63
54
*/
64
55
public class MultipartUploadTest extends AbstractBasicTest {
65
- public static byte GZIPTEXT [] = new byte [] { 31 , -117 , 8 , 8 , 11 , 43 , 79 , 75 , 0 , 3 , 104 , 101 , 108 , 108 , 111 , 46 , 116 , 120 , 116 , 0 , - 53 , 72 , - 51 , - 55 , - 55 , - 25 , 2 , 0 , 32 , 48 ,
66
- 58 , 54 , 6 , 0 , 0 , 0 };
56
+ public static byte GZIPTEXT [] = new byte [] { 31 , -117 , 8 , 8 , 11 , 43 , 79 , 75 , 0 , 3 , 104 , 101 , 108 , 108 , 111 , 46 , 116 ,
57
+ 120 , 116 , 0 , - 53 , 72 , - 51 , - 55 , - 55 , - 25 , 2 , 0 , 32 , 48 , 58 , 54 , 6 , 0 , 0 , 0 };
67
58
68
59
@ BeforeClass
69
60
public void setUp () throws Exception {
70
61
server = new Server ();
71
62
ServerConnector connector = addHttpConnector (server );
72
63
ServletContextHandler context = new ServletContextHandler (ServletContextHandler .SESSIONS );
73
- context .addServlet (new ServletHolder (new MockMultipartUploadServlet ()), "/upload/* " );
64
+ context .addServlet (new ServletHolder (new MockMultipartUploadServlet ()), "/upload" );
74
65
server .setHandler (context );
75
66
server .start ();
76
67
port1 = connector .getLocalPort ();
77
68
}
78
69
79
70
/**
80
71
* Tests that the streaming of a file works.
81
- * @throws IOException
72
+ *
73
+ * @throws IOException
82
74
*/
83
75
@ Test (groups = "standalone" )
84
- public void testSendingSmallFilesAndByteArray () throws IOException {
76
+ public void testSendingSmallFilesAndByteArray () throws Exception {
85
77
String expectedContents = "filecontent: hello" ;
86
78
String expectedContents2 = "gzipcontent: hello" ;
87
79
String expectedContents3 = "filecontent: hello2" ;
88
80
String testResource1 = "textfile.txt" ;
89
81
String testResource2 = "gzip.txt.gz" ;
90
82
String testResource3 = "textfile2.txt" ;
91
83
92
- File testResource1File = null ;
93
- try {
94
- testResource1File = getClasspathFile (testResource1 );
95
- } catch (FileNotFoundException e ) {
96
- // TODO Auto-generated catch block
97
- fail ("unable to find " + testResource1 );
98
- }
99
-
100
- File testResource2File = null ;
101
- try {
102
- testResource2File = getClasspathFile (testResource2 );
103
- } catch (FileNotFoundException e ) {
104
- // TODO Auto-generated catch block
105
- fail ("unable to find " + testResource2 );
106
- }
107
-
108
- File testResource3File = null ;
109
- try {
110
- testResource3File = getClasspathFile (testResource3 );
111
- } catch (FileNotFoundException e ) {
112
- // TODO Auto-generated catch block
113
- fail ("unable to find " + testResource3 );
114
- }
84
+ File testResource1File = getClasspathFile (testResource1 );
85
+ File testResource2File = getClasspathFile (testResource2 );
86
+ File testResource3File = getClasspathFile (testResource3 );
115
87
116
88
List <File > testFiles = new ArrayList <>();
117
89
testFiles .add (testResource1File );
@@ -137,48 +109,52 @@ public void testSendingSmallFilesAndByteArray() throws IOException {
137
109
testFiles .add (tmpFile );
138
110
expected .add (expectedContents );
139
111
gzipped .add (false );
140
-
141
- } catch (FileNotFoundException e1 ) {
142
- // TODO Auto-generated catch block
143
- e1 .printStackTrace ();
144
- } catch (IOException e1 ) {
145
- // TODO Auto-generated catch block
146
- e1 .printStackTrace ();
147
112
}
148
113
149
114
if (!tmpFileCreated ) {
150
115
fail ("Unable to test ByteArrayMultiPart, as unable to write to filesystem the tmp test content" );
151
116
}
152
117
153
- try (AsyncHttpClient c = asyncHttpClient (config ().setFollowRedirect (true ))) {
154
-
155
- RequestBuilder builder = post ("http://localhost" + ":" + port1 + "/upload/bob" );
156
- builder .addBodyPart (new FilePart ("file1" , testResource1File , "text/plain" , UTF_8 ));
157
- builder .addBodyPart (new FilePart ("file2" , testResource2File , "application/x-gzip" , null ));
158
- builder .addBodyPart (new StringPart ("Name" , "Dominic" ));
159
- builder .addBodyPart (new FilePart ("file3" , testResource3File , "text/plain" , UTF_8 ));
160
- builder .addBodyPart (new StringPart ("Age" , "3" ));
161
- builder .addBodyPart (new StringPart ("Height" , "shrimplike" ));
162
- builder .addBodyPart (new StringPart ("Hair" , "ridiculous" ));
163
-
164
- builder .addBodyPart (new ByteArrayPart ("file4" , expectedContents .getBytes (UTF_8 ), "text/plain" , UTF_8 , "bytearray.txt" ));
165
-
166
- Request r = builder .build ();
118
+ try (AsyncHttpClient c = asyncHttpClient (config ())) {
119
+ Request r = post ("http://localhost" + ":" + port1 + "/upload" )
120
+ .addBodyPart (new FilePart ("file1" , testResource1File , "text/plain" , UTF_8 ))
121
+ .addBodyPart (new FilePart ("file2" , testResource2File , "application/x-gzip" , null ))
122
+ .addBodyPart (new StringPart ("Name" , "Dominic" ))
123
+ .addBodyPart (new FilePart ("file3" , testResource3File , "text/plain" , UTF_8 ))
124
+ .addBodyPart (new StringPart ("Age" , "3" )).addBodyPart (new StringPart ("Height" , "shrimplike" ))
125
+ .addBodyPart (new StringPart ("Hair" , "ridiculous" )).addBodyPart (new ByteArrayPart ("file4" ,
126
+ expectedContents .getBytes (UTF_8 ), "text/plain" , UTF_8 , "bytearray.txt" ))
127
+ .build ();
167
128
168
129
Response res = c .executeRequest (r ).get ();
169
130
170
131
assertEquals (res .getStatusCode (), 200 );
171
132
172
133
testSentFile (expected , testFiles , res , gzipped );
134
+ }
135
+ }
173
136
174
- } catch (Exception e ) {
175
- e .printStackTrace ();
176
- fail ("Download Exception" );
177
- } finally {
178
- FileUtils .deleteQuietly (tmpFile );
137
+ private void sendEmptyFile0 (boolean disableZeroCopy ) throws Exception {
138
+ File file = getClasspathFile ("empty.txt" );
139
+ try (AsyncHttpClient c = asyncHttpClient (config ().setDisableZeroCopy (disableZeroCopy ))) {
140
+ Request r = post ("http://localhost" + ":" + port1 + "/upload/bob" )
141
+ .addBodyPart (new FilePart ("file" , file , "text/plain" , UTF_8 )).build ();
142
+
143
+ Response res = c .executeRequest (r ).get ();
144
+ assertEquals (res .getStatusCode (), 200 );
179
145
}
180
146
}
181
147
148
+ @ Test (groups = "standalone" )
149
+ public void sendEmptyFile () throws Exception {
150
+ sendEmptyFile0 (true );
151
+ }
152
+
153
+ @ Test (groups = "standalone" )
154
+ public void sendEmptyFileZeroCopy () throws Exception {
155
+ sendEmptyFile0 (false );
156
+ }
157
+
182
158
/**
183
159
* Test that the files were sent, based on the response from the servlet
184
160
*
@@ -187,7 +163,8 @@ public void testSendingSmallFilesAndByteArray() throws IOException {
187
163
* @param r
188
164
* @param deflate
189
165
*/
190
- private void testSentFile (List <String > expectedContents , List <File > sourceFiles , Response r , List <Boolean > deflate ) {
166
+ private void testSentFile (List <String > expectedContents , List <File > sourceFiles , Response r ,
167
+ List <Boolean > deflate ) {
191
168
String content = r .getResponseBody ();
192
169
assertNotNull ("===>" + content );
193
170
logger .debug (content );
@@ -248,7 +225,6 @@ private void testSentFile(List<String> expectedContents, List<File> sourceFiles,
248
225
assertEquals (bytes , sourceBytes );
249
226
}
250
227
251
-
252
228
if (!deflate .get (i )) {
253
229
String helloString = new String (bytes );
254
230
assertEquals (helloString , expectedContents .get (i ));
@@ -265,9 +241,9 @@ private void testSentFile(List<String> expectedContents, List<File> sourceFiles,
265
241
} finally {
266
242
deflater .close ();
267
243
}
268
-
244
+
269
245
String helloString = new String (baos3 .toByteArray ());
270
-
246
+
271
247
assertEquals (expectedContents .get (i ), helloString );
272
248
}
273
249
}
@@ -326,7 +302,8 @@ public int getStringsProcessed() {
326
302
}
327
303
328
304
@ Override
329
- public void service (HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException {
305
+ public void service (HttpServletRequest request , HttpServletResponse response )
306
+ throws ServletException , IOException {
330
307
// Check that we have a file upload request
331
308
boolean isMultipart = ServletFileUpload .isMultipartContent (request );
332
309
if (isMultipart ) {
@@ -342,12 +319,14 @@ public void service(HttpServletRequest request, HttpServletResponse response) th
342
319
try (InputStream stream = item .openStream ()) {
343
320
344
321
if (item .isFormField ()) {
345
- LOGGER .debug ("Form field " + name + " with value " + Streams .asString (stream ) + " detected." );
322
+ LOGGER .debug ("Form field " + name + " with value " + Streams .asString (stream )
323
+ + " detected." );
346
324
incrementStringsProcessed ();
347
325
} else {
348
326
LOGGER .debug ("File field " + name + " with file name " + item .getName () + " detected." );
349
327
// Process the input stream
350
- File tmpFile = File .createTempFile (UUID .randomUUID ().toString () + "_MockUploadServlet" , ".tmp" );
328
+ File tmpFile = File .createTempFile (UUID .randomUUID ().toString () + "_MockUploadServlet" ,
329
+ ".tmp" );
351
330
tmpFile .deleteOnExit ();
352
331
try (OutputStream os = new FileOutputStream (tmpFile )) {
353
332
byte [] buffer = new byte [4096 ];
0 commit comments