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} and
036 *
037 * @see AbstractFileUploadTest
038 */
039public class JakartaServletFileUploadDiskTest
040        extends AbstractFileUploadTest<JakartaServletDiskFileUpload, HttpServletRequest, DiskFileItem, DiskFileItemFactory> {
041
042    public JakartaServletFileUploadDiskTest() {
043        super(new JakartaServletDiskFileUpload());
044    }
045
046    @Override
047    public List<DiskFileItem> parseUpload(final JakartaServletDiskFileUpload upload, final byte[] bytes, final String contentType) throws FileUploadException {
048        final HttpServletRequest request = new JakartaMockHttpServletRequest(bytes, contentType);
049        return upload.parseRequest(new JakartaServletRequestContext(request));
050    }
051
052    @Test
053    public void testParseImpliedUtf8() throws Exception {
054        // utf8 encoded form-data without explicit content-type encoding
055        // @formatter:off
056        final var text = "-----1234\r\n" +
057                "Content-Disposition: form-data; name=\"utf8Html\"\r\n" +
058                "\r\n" +
059                "Thís ís the coñteñt of the fíle\n" +
060                "\r\n" +
061                "-----1234--\r\n";
062        // @formatter:on
063
064        final var bytes = text.getBytes(StandardCharsets.UTF_8);
065        final HttpServletRequest request = new JakartaMockServletHttpRequest(bytes, Constants.CONTENT_TYPE);
066        // @formatter:off
067        final var fileItemFactory = DiskFileItemFactory.builder()
068                .setCharset(StandardCharsets.UTF_8)
069                .get();
070        // @formatter:on
071        final var upload = new JakartaServletDiskFileUpload(fileItemFactory);
072        final var fileItems = upload.parseRequest(request);
073        final var fileItem = fileItems.get(0);
074        assertTrue(fileItem.getString().contains("coñteñt"), fileItem.getString());
075    }
076
077    /*
078     * Test case for <a href="https://issues.apache.org/jira/browse/FILEUPLOAD-210">
079     */
080    @Test
081    public void testParseParameterMap() throws Exception {
082        // @formatter:off
083        final var text = "-----1234\r\n" +
084                      "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" +
085                      "Content-Type: text/whatever\r\n" +
086                      "\r\n" +
087                      "This is the content of the file\n" +
088                      "\r\n" +
089                      "-----1234\r\n" +
090                      "Content-Disposition: form-data; name=\"field\"\r\n" +
091                      "\r\n" +
092                      "fieldValue\r\n" +
093                      "-----1234\r\n" +
094                      "Content-Disposition: form-data; name=\"multi\"\r\n" +
095                      "\r\n" +
096                      "value1\r\n" +
097                      "-----1234\r\n" +
098                      "Content-Disposition: form-data; name=\"multi\"\r\n" +
099                      "\r\n" +
100                      "value2\r\n" +
101                      "-----1234--\r\n";
102        // @formatter:on
103        final var bytes = text.getBytes(StandardCharsets.US_ASCII);
104        final HttpServletRequest request = new JakartaMockServletHttpRequest(bytes, Constants.CONTENT_TYPE);
105
106        final var upload = new JakartaServletDiskFileUpload();
107        final var mappedParameters = upload.parseParameterMap(request);
108        assertTrue(mappedParameters.containsKey("file"));
109        assertEquals(1, mappedParameters.get("file").size());
110
111        assertTrue(mappedParameters.containsKey("field"));
112        assertEquals(1, mappedParameters.get("field").size());
113
114        assertTrue(mappedParameters.containsKey("multi"));
115        assertEquals(2, mappedParameters.get("multi").size());
116    }
117}