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.servlet5; 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; 024import java.util.concurrent.atomic.AtomicInteger; 025 026import org.apache.commons.fileupload2.core.AbstractFileUploadTest; 027import org.apache.commons.fileupload2.core.Constants; 028import org.apache.commons.fileupload2.core.DiskFileItem; 029import org.apache.commons.fileupload2.core.DiskFileItemFactory; 030import org.apache.commons.fileupload2.core.FileUploadException; 031import org.junit.jupiter.api.Test; 032 033import jakarta.servlet.http.HttpServletRequest; 034 035/** 036 * Tests {@link JakartaServletFileUpload}. 037 * 038 * @see AbstractFileUploadTest 039 */ 040public class JakartaServletFileUploadTest 041 extends AbstractFileUploadTest<JakartaServletFileUpload<DiskFileItem, DiskFileItemFactory>, HttpServletRequest, DiskFileItem, DiskFileItemFactory> { 042 043 public JakartaServletFileUploadTest() { 044 super(new JakartaServletFileUpload<>(DiskFileItemFactory.builder().get())); 045 } 046 047 @Override 048 public List<DiskFileItem> parseUpload(final JakartaServletFileUpload<DiskFileItem, DiskFileItemFactory> upload, final byte[] bytes, 049 final String contentType) throws FileUploadException { 050 final HttpServletRequest request = new JakartaMockHttpServletRequest(bytes, contentType); 051 return upload.parseRequest(new JakartaServletRequestContext(request)); 052 } 053 054 @Test 055 public void testParseImpliedUtf8() throws Exception { 056 // utf8 encoded form-data without explicit content-type encoding 057 // @formatter:off 058 final var text = "-----1234\r\n" + 059 "Content-Disposition: form-data; name=\"utf8Html\"\r\n" + 060 "\r\n" + 061 "Thís ís the coñteñt of the fíle\n" + 062 "\r\n" + 063 "-----1234--\r\n"; 064 // @formatter:on 065 066 final var bytes = text.getBytes(StandardCharsets.UTF_8); 067 final HttpServletRequest request = new JakartaMockServletHttpRequest(bytes, Constants.CONTENT_TYPE); 068 // @formatter:off 069 final var fileItemFactory = DiskFileItemFactory.builder() 070 .setCharset(StandardCharsets.UTF_8) 071 .get(); 072 // @formatter:on 073 final var upload = new JakartaServletFileUpload<>(fileItemFactory); 074 final var fileItems = upload.parseRequest(request); 075 final var fileItem = fileItems.get(0); 076 assertTrue(fileItem.getString().contains("coñteñt"), fileItem.getString()); 077 } 078 079 /* 080 * Test case for <a href="https://issues.apache.org/jira/browse/FILEUPLOAD-210"> 081 */ 082 @Test 083 public void testParseParameterMap() throws Exception { 084 // @formatter:off 085 final var text = "-----1234\r\n" + 086 "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" + 087 "Content-Type: text/whatever\r\n" + 088 "\r\n" + 089 "This is the content of the file\n" + 090 "\r\n" + 091 "-----1234\r\n" + 092 "Content-Disposition: form-data; name=\"field\"\r\n" + 093 "\r\n" + 094 "fieldValue\r\n" + 095 "-----1234\r\n" + 096 "Content-Disposition: form-data; name=\"multi\"\r\n" + 097 "\r\n" + 098 "value1\r\n" + 099 "-----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}