Skip to content

Commit 776f4a5

Browse files
HTTPUtilitiesTest Cleanup
Updating file handling in the tests for file upload to ensure that any file created in the test is deleted before exiting.
1 parent 1458a47 commit 776f4a5

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

src/test/java/org/owasp/esapi/reference/HTTPUtilitiesTest.java

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public void testChangeSessionIdentifier() throws EnterpriseSecurityException {
206206
* Test of formatHttpRequestForLog method, of class org.owasp.esapi.HTTPUtilities.
207207
* @throws IOException
208208
*/
209-
public void testGetFileUploads() throws IOException {
209+
public void testGetFileUploads() throws Exception {
210210
File home = null;
211211

212212
try
@@ -227,37 +227,24 @@ public void testGetFileUploads() throws IOException {
227227
MockHttpServletRequest request2 = new MockHttpServletRequest("/test", content.getBytes(response.getCharacterEncoding()));
228228
request2.setContentType( "multipart/form-data; boundary=ridiculous");
229229
ESAPI.httpUtilities().setCurrentHTTP(request2, response);
230+
List<File> response2 = new ArrayList<>();
230231
try {
231-
List<?> list = ESAPI.httpUtilities().getFileUploads(request2, home);
232-
assertTrue( list.size() > 0 );
233-
Iterator<?> i = list.iterator();
234-
while ( i.hasNext() ) {
235-
File f = (File)i.next();
236-
f.delete();
237-
}
238-
} catch (ValidationException e) {
239-
System.out.println("ERROR in testGetFileUploads() request2: " + e.toString());
240-
e.printStackTrace();
241-
fail();
232+
response2 = ESAPI.httpUtilities().getFileUploads(request2, home);
233+
assertTrue( response2.size() > 0 );
234+
} finally {
235+
response2.forEach(file -> file.delete());
242236
}
243237

244238
MockHttpServletRequest request4 = new MockHttpServletRequest("/test", content.getBytes(response.getCharacterEncoding()));
245239
request4.setContentType( "multipart/form-data; boundary=ridiculous");
246240
ESAPI.httpUtilities().setCurrentHTTP(request4, response);
247241
System.err.println("UPLOAD DIRECTORY: " + ESAPI.securityConfiguration().getUploadDirectory());
242+
List<File> response4 = new ArrayList<>();
248243
try {
249-
List<?> list = ESAPI.httpUtilities().getFileUploads(request4, home);
250-
assertTrue( list.size() > 0 );
251-
Iterator<?> i = list.iterator();
252-
while ( i.hasNext() ) {
253-
File f = (File)i.next();
254-
f.delete();
255-
}
256-
} catch (ValidationException e) {
257-
// TODO: This test cases if failing when we upgrade to commons-fileupload;commons-fileupload:1.4 because of a duplicate file error. Need to figure out why.
258-
System.out.println("ERROR in testGetFileUploads() request4: " + e.toString());
259-
e.printStackTrace();
260-
fail();
244+
response4 = ESAPI.httpUtilities().getFileUploads(request4, home);
245+
assertTrue( response4.size() > 0 );
246+
} finally {
247+
response4.forEach(file -> file.delete());
261248
}
262249

263250
MockHttpServletRequest request3 = new MockHttpServletRequest("/test", content.replaceAll("txt", "ridiculous").getBytes(response.getCharacterEncoding()));

0 commit comments

Comments
 (0)