    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.fileupload2.jakarta.servlet6;
    018
    019import static org.junit.jupiter.api.Assertions.assertEquals;
    020import static org.junit.jupiter.api.Assertions.assertTrue;
    021
    022import java.nio.charset.StandardCharsets;
    023import java.util.List;
    024
    025import org.apache.commons.fileupload2.core.AbstractFileUploadTest;
    026import org.apache.commons.fileupload2.core.Constants;
    027import org.apache.commons.fileupload2.core.DiskFileItem;
    028import org.apache.commons.fileupload2.core.DiskFileItemFactory;
    029import org.apache.commons.fileupload2.core.FileUploadException;
    030import org.junit.jupiter.api.Test;
    031
    032import jakarta.servlet.http.HttpServletRequest;
    033
    034/**
    035 * Tests {@link JakartaServletFileUpload}.
    036 *
    037 * @see AbstractFileUploadTest
    038 */
    039public class JakartaServletFileUploadTest
    040        extends AbstractFileUploadTest<JakartaServletFileUpload<DiskFileItem, DiskFileItemFactory>, HttpServletRequest, DiskFileItem, DiskFileItemFactory> {
    041
    042    public JakartaServletFileUploadTest() {
    043        super(new JakartaServletFileUpload<>(DiskFileItemFactory.builder().get()));
    044    }
    045
    046    @Override
    047    public List<DiskFileItem> parseUpload(final JakartaServletFileUpload<DiskFileItem, DiskFileItemFactory> upload, final byte[] bytes,
    048            final String contentType) throws FileUploadException {
    049        final HttpServletRequest request = new JakartaMockHttpServletRequest(bytes, contentType);
    050        return upload.parseRequest(new JakartaServletRequestContext(request));
    051    }
    052
    053    @Test
    054    public void testParseImpliedUtf8() throws Exception {
    055        // utf8 encoded form-data without explicit content-type encoding
    056        // @formatter:off
    057        final var text = "-----1234\r\n" +
    058                "Content-Disposition: form-data; name=\"utf8Html\"\r\n" +
    059                "\r\n" +
    060                "Thís ís the coñteñt of the fíle\n" +
    061                "\r\n" +
    062                "-----1234--\r\n";
    063        // @formatter:on
    064
    065        final var bytes = text.getBytes(StandardCharsets.UTF_8);
    066        final HttpServletRequest request = new JakartaMockServletHttpRequest(bytes, Constants.CONTENT_TYPE);
    067        // @formatter:off
    068        final var fileItemFactory = DiskFileItemFactory.builder()
    069                .setCharset(StandardCharsets.UTF_8)
    070                .get();
    071        // @formatter:on
    072        final var upload = new JakartaServletFileUpload<>(fileItemFactory);
    073        final var fileItems = upload.parseRequest(request);
    074        final var fileItem = fileItems.get(0);
    075        assertTrue(fileItem.getString().contains("coñteñt"), fileItem.getString());
    076    }
    077
    078    /*
    079     * Test case for <a href="https://issues.apache.org/jira/browse/FILEUPLOAD-210">
    080     */
    081    @Test
    082    public void testParseParameterMap() throws Exception {
    083        // @formatter:off
    084        final var text = "-----1234\r\n" +
    085                      "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" +
    086                      "Content-Type: text/whatever\r\n" +
    087                      "\r\n" +
    088                      "This is the content of the file\n" +
    089                      "\r\n" +
    090                      "-----1234\r\n" +
    091                      "Content-Disposition: form-data; name=\"field\"\r\n" +
    092                      "\r\n" +
    093                      "fieldValue\r\n" +
    094                      "-----1234\r\n" +
    095                      "Content-Disposition: form-data; name=\"multi\"\r\n" +
    096                      "\r\n" +
    097                      "value1\r\n" +
    098                      "-----1234\r\n" +
    099                      "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}