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