View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Test for {@link DiskFileUpload}. Remove when deprecated class is removed.
36   *
37   * @since 1.4
38   */
39  @SuppressWarnings({"deprecation"}) // unit tests for deprecated class
40  public class DiskFileUploadTest {
41  
42      private DiskFileUpload upload;
43  
44      @Before
45      public void setUp() {
46          upload = new DiskFileUpload();
47      }
48  
49      /** Proposed test for FILEUPLOAD-293. As of yet, doesn't reproduce the problem.
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  }