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.fileupload2.jakarta.servlet5;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import java.nio.charset.StandardCharsets;
23  import java.util.List;
24  
25  import org.apache.commons.fileupload2.core.AbstractFileUploadTest;
26  import org.apache.commons.fileupload2.core.Constants;
27  import org.apache.commons.fileupload2.core.DiskFileItem;
28  import org.apache.commons.fileupload2.core.DiskFileItemFactory;
29  import org.apache.commons.fileupload2.core.FileUploadException;
30  import org.junit.jupiter.api.Test;
31  
32  import jakarta.servlet.http.HttpServletRequest;
33  
34  /**
35   * Tests {@link JakartaServletFileUpload} and
36   *
37   * @see AbstractFileUploadTest
38   */
39  public class JakartaServletFileUploadDiskTest
40          extends AbstractFileUploadTest<JakartaServletDiskFileUpload, HttpServletRequest, DiskFileItem, DiskFileItemFactory> {
41  
42      public JakartaServletFileUploadDiskTest() {
43          super(new JakartaServletDiskFileUpload());
44      }
45  
46      @Override
47      public List<DiskFileItem> parseUpload(final JakartaServletDiskFileUpload upload, final byte[] bytes, final String contentType) throws FileUploadException {
48          final HttpServletRequest request = new JakartaMockHttpServletRequest(bytes, contentType);
49          return upload.parseRequest(new JakartaServletRequestContext(request));
50      }
51  
52      @Test
53      public void testParseImpliedUtf8() throws Exception {
54          // utf8 encoded form-data without explicit content-type encoding
55          // @formatter:off
56          final var text = "-----1234\r\n" +
57                  "Content-Disposition: form-data; name=\"utf8Html\"\r\n" +
58                  "\r\n" +
59                  "Thís ís the coñteñt of the fíle\n" +
60                  "\r\n" +
61                  "-----1234--\r\n";
62          // @formatter:on
63  
64          final var bytes = text.getBytes(StandardCharsets.UTF_8);
65          final HttpServletRequest request = new JakartaMockServletHttpRequest(bytes, Constants.CONTENT_TYPE);
66          // @formatter:off
67          final var fileItemFactory = DiskFileItemFactory.builder()
68                  .setCharset(StandardCharsets.UTF_8)
69                  .get();
70          // @formatter:on
71          final var upload = new JakartaServletDiskFileUpload(fileItemFactory);
72          final var fileItems = upload.parseRequest(request);
73          final var fileItem = fileItems.get(0);
74          assertTrue(fileItem.getString().contains("coñteñt"), fileItem.getString());
75      }
76  
77      /*
78       * Test case for <a href="https://issues.apache.org/jira/browse/FILEUPLOAD-210">
79       */
80      @Test
81      public void testParseParameterMap() throws Exception {
82          // @formatter:off
83          final var text = "-----1234\r\n" +
84                        "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" +
85                        "Content-Type: text/whatever\r\n" +
86                        "\r\n" +
87                        "This is the content of the file\n" +
88                        "\r\n" +
89                        "-----1234\r\n" +
90                        "Content-Disposition: form-data; name=\"field\"\r\n" +
91                        "\r\n" +
92                        "fieldValue\r\n" +
93                        "-----1234\r\n" +
94                        "Content-Disposition: form-data; name=\"multi\"\r\n" +
95                        "\r\n" +
96                        "value1\r\n" +
97                        "-----1234\r\n" +
98                        "Content-Disposition: form-data; name=\"multi\"\r\n" +
99                        "\r\n" +
100                       "value2\r\n" +
101                       "-----1234--\r\n";
102         // @formatter:on
103         final var bytes = text.getBytes(StandardCharsets.US_ASCII);
104         final HttpServletRequest request = new JakartaMockServletHttpRequest(bytes, Constants.CONTENT_TYPE);
105 
106         final var upload = new JakartaServletDiskFileUpload();
107         final var mappedParameters = upload.parseParameterMap(request);
108         assertTrue(mappedParameters.containsKey("file"));
109         assertEquals(1, mappedParameters.get("file").size());
110 
111         assertTrue(mappedParameters.containsKey("field"));
112         assertEquals(1, mappedParameters.get("field").size());
113 
114         assertTrue(mappedParameters.containsKey("multi"));
115         assertEquals(2, mappedParameters.get("multi").size());
116     }
117 }