1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.fileupload;
18
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.fail;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23 import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
24
25 import java.io.File;
26 import java.util.List;
27
28 import javax.servlet.http.HttpServletRequest;
29
30 import org.apache.commons.fileupload.disk.DiskFileItem;
31 import org.junit.Before;
32 import org.junit.Test;
33
34
35
36
37
38
39 @SuppressWarnings({"deprecation"})
40 public class DiskFileUploadTest {
41
42 private DiskFileUpload upload;
43
44 @Before
45 public void setUp() {
46 upload = new DiskFileUpload();
47 }
48
49
50
51 @Test
52 public void testMoveFile() throws Exception {
53 final DiskFileUpload myUpload = new DiskFileUpload();
54 myUpload.setSizeThreshold(0);
55 final String content =
56 "-----1234\r\n" +
57 "Content-Disposition: form-data; name=\"file\";" +
58 "filename=\"foo.tab\"\r\n" +
59 "Content-Type: text/whatever\r\n" +
60 "\r\n" +
61 "This is the content of the file\n" +
62 "\r\n" +
63 "-----1234--\r\n";
64 final byte[] contentBytes = content.getBytes("US-ASCII");
65 final HttpServletRequest request = new MockHttpServletRequest(contentBytes, Constants.CONTENT_TYPE);
66 final List<FileItem> items = myUpload.parseRequest(request);
67 assertNotNull(items);
68 assertFalse(items.isEmpty());
69 final DiskFileItem dfi = (DiskFileItem) items.get(0);
70 final File out = File.createTempFile("install", ".tmp");
71 dfi.write(out);
72 }
73
74 @Test
75 public void testWithInvalidRequest() {
76 final HttpServletRequest req = HttpServletRequestFactory.createInvalidHttpServletRequest();
77 assertThrows(FileUploadException.class, () -> upload.parseRequest(req));
78 }
79
80 @Test
81 public void testWithNullContentType() {
82 final HttpServletRequest req = HttpServletRequestFactory.createHttpServletRequestWithNullContentType();
83 assertThrowsExactly(DiskFileUpload.InvalidContentTypeException.class, () -> upload.parseRequest(req));
84 }
85 }