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
    18  package org.apache.commons.fileupload2.javax;
    19
    20  import static org.junit.jupiter.api.Assertions.assertEquals;
    21  import static org.junit.jupiter.api.Assertions.assertTrue;
    22
    23  import java.io.ByteArrayOutputStream;
    24  import java.io.IOException;
    25  import java.nio.charset.StandardCharsets;
    26  import java.util.List;
    27
    28  import javax.servlet.http.HttpServletRequest;
    29
    30  import org.apache.commons.fileupload2.core.AbstractFileUploadTest;
    31  import org.apache.commons.fileupload2.core.Constants;
    32  import org.apache.commons.fileupload2.core.DiskFileItem;
    33  import org.apache.commons.fileupload2.core.DiskFileItemFactory;
    34  import org.apache.commons.fileupload2.core.FileUploadException;
    35  import org.junit.jupiter.api.Test;
    36
    37  /**
    38   * Tests {@link JavaxServletFileUpload}.
    39   *
    40   * @see AbstractFileUploadTest
    41   */
    42  public class JavaxServletFileUploadTest
    43          extends AbstractFileUploadTest<JavaxServletFileUpload<DiskFileItem, DiskFileItemFactory>, HttpServletRequest, DiskFileItem, DiskFileItemFactory> {
    44
    45      public JavaxServletFileUploadTest() {
    46          super(new JavaxServletFileUpload<>(DiskFileItemFactory.builder().get()));
    47      }
    48
    49      @Override
    50      public List<DiskFileItem> parseUpload(final JavaxServletFileUpload<DiskFileItem, DiskFileItemFactory> upload, final byte[] bytes, final String contentType)
    51              throws FileUploadException {
    52          final HttpServletRequest request = new JavaxMockHttpServletRequest(bytes, contentType);
    53          return upload.parseRequest(new JavaxServletRequestContext(request));
    54      }
    55
    56      /**
    57       * Runs a test with varying file sizes.
    58       */
    59      @Override
    60      @Test
    61      public void testFileUpload() throws IOException, FileUploadException {
    62          final var baos = new ByteArrayOutputStream();
    63          var add = 16;
    64          var num = 0;
    65          for (var i = 0; i < 16384; i += add) {
    66              if (++add == 32) {
    67                  add = 16;
    68              }
    69              final var header = "-----1234\r\n" + "Content-Disposition: form-data; name=\"field" + num++ + "\"\r\n" + "\r\n";
    70              baos.write(header.getBytes(StandardCharsets.US_ASCII));
    71              for (var j = 0; j < i; j++) {
    72                  baos.write((byte) j);
    73              }
    74              baos.write("\r\n".getBytes(StandardCharsets.US_ASCII));
    75          }
    76          baos.write("-----1234--\r\n".getBytes(StandardCharsets.US_ASCII));
    77
    78          final var fileItems = parseUpload(new JavaxServletFileUpload<>(DiskFileItemFactory.builder().get()), baos.toByteArray());
    79          final var fileIter = fileItems.iterator();
    80          add = 16;
    81          num = 0;
    82          for (var i = 0; i < 16384; i += add) {
    83              if (++add == 32) {
    84                  add = 16;
    85              }
    86              final var item = fileIter.next();
    87              assertEquals("field" + num++, item.getFieldName());
    88              final var bytes = item.get();
    89              assertEquals(i, bytes.length);
    90              for (var j = 0; j < i; j++) {
    91                  assertEquals((byte) j, bytes[j]);
    92              }
    93          }
    94          assertTrue(!fileIter.hasNext());
    95      }
    96
    97      @Test
    98      public void testParseImpliedUtf8() throws Exception {
    99          // utf8 encoded form-data without explicit content-type encoding
    100         // @formatter:off
    101         final var text = "-----1234\r\n" +
    102                 "Content-Disposition: form-data; name=\"utf8Html\"\r\n" +
    103                 "\r\n" +
    104                 "Thís ís the coñteñt of the fíle\n" +
    105                 "\r\n" +
    106                 "-----1234--\r\n";
    107         // @formatter:on
    108
    109         final var bytes = text.getBytes(StandardCharsets.UTF_8);
    110         final HttpServletRequest request = new JavaxMockHttpServletRequest(bytes, Constants.CONTENT_TYPE);
    111         // @formatter:off
    112         final var fileItemFactory = DiskFileItemFactory.builder()
    113                 .setCharset(StandardCharsets.UTF_8)
    114                 .get();
    115         // @formatter:on
    116         final var upload = new JavaxServletFileUpload<>(fileItemFactory);
    117         final var fileItems = upload.parseRequest(request);
    118         final var fileItem = fileItems.get(0);
    119         assertTrue(fileItem.getString().contains("coñteñt"), fileItem.getString());
    120     }
    121
    122     /*
    123      * Test case for <a href="https://issues.apache.org/jira/browse/FILEUPLOAD-210">
    124      */
    125     @Test
    126     public void testParseParameterMap() throws Exception {
    127         // @formatter:off
    128         final var text = "-----1234\r\n" +
    129                       "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" +
    130                       "Content-Type: text/whatever\r\n" +
    131                       "\r\n" +
    132                       "This is the content of the file\n" +
    133                       "\r\n" +
    134                       "-----1234\r\n" +
    135                       "Content-Disposition: form-data; name=\"field\"\r\n" +
    136                       "\r\n" +
    137                       "fieldValue\r\n" +
    138                       "-----1234\r\n" +
    139                       "Content-Disposition: form-data; name=\"multi\"\r\n" +
    140                       "\r\n" +
    141                       "value1\r\n" +
    142                       "-----1234\r\n" +
    143                       "Content-Disposition: form-data; name=\"multi\"\r\n" +
    144                       "\r\n" +
    145                       "value2\r\n" +
    146                       "-----1234--\r\n";
    147         // @formatter:on
    148         final var bytes = text.getBytes(StandardCharsets.US_ASCII);
    149         final HttpServletRequest request = new JavaxMockHttpServletRequest(bytes, Constants.CONTENT_TYPE);
    150
    151         final var upload = new JavaxServletFileUpload<>(DiskFileItemFactory.builder().get());
    152         final var mappedParameters = upload.parseParameterMap(request);
    153         assertTrue(mappedParameters.containsKey("file"));
    154         assertEquals(1, mappedParameters.get("file").size());
    155
    156         assertTrue(mappedParameters.containsKey("field"));
    157         assertEquals(1, mappedParameters.get("field").size());
    158
    159         assertTrue(mappedParameters.containsKey("multi"));
    160         assertEquals(2, mappedParameters.get("multi").size());
    161     }
    162
    163 }