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