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