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 */ 017 018package org.apache.commons.fileupload2.javax; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertTrue; 022 023import java.io.ByteArrayOutputStream; 024import java.io.IOException; 025import java.nio.charset.StandardCharsets; 026import java.util.List; 027 028import javax.servlet.http.HttpServletRequest; 029 030import org.apache.commons.fileupload2.core.AbstractFileUploadTest; 031import org.apache.commons.fileupload2.core.Constants; 032import org.apache.commons.fileupload2.core.DiskFileItem; 033import org.apache.commons.fileupload2.core.DiskFileItemFactory; 034import org.apache.commons.fileupload2.core.FileUploadException; 035import org.junit.jupiter.api.Test; 036 037/** 038 * Tests {@link JavaxServletFileUpload}. 039 * 040 * @see AbstractFileUploadTest 041 */ 042public class JavaxServletFileUploadDiskTest extends AbstractFileUploadTest<JavaxServletDiskFileUpload, HttpServletRequest, DiskFileItem, DiskFileItemFactory> { 043 044 public JavaxServletFileUploadDiskTest() { 045 super(new JavaxServletDiskFileUpload()); 046 } 047 048 @Override 049 public List<DiskFileItem> parseUpload(final JavaxServletDiskFileUpload upload, final byte[] bytes, final String contentType) throws FileUploadException { 050 final HttpServletRequest request = new JavaxMockHttpServletRequest(bytes, contentType); 051 return upload.parseRequest(new JavaxServletRequestContext(request)); 052 } 053 054 /** 055 * Runs a test with varying file sizes. 056 */ 057 @Override 058 @Test 059 public void testFileUpload() throws IOException, FileUploadException { 060 final var baos = new ByteArrayOutputStream(); 061 var add = 16; 062 var num = 0; 063 for (var i = 0; i < 16384; i += add) { 064 if (++add == 32) { 065 add = 16; 066 } 067 final var header = "-----1234\r\n" + "Content-Disposition: form-data; name=\"field" + num++ + "\"\r\n" + "\r\n"; 068 baos.write(header.getBytes(StandardCharsets.US_ASCII)); 069 for (var j = 0; j < i; j++) { 070 baos.write((byte) j); 071 } 072 baos.write("\r\n".getBytes(StandardCharsets.US_ASCII)); 073 } 074 baos.write("-----1234--\r\n".getBytes(StandardCharsets.US_ASCII)); 075 076 final var fileItems = parseUpload(new JavaxServletDiskFileUpload(), baos.toByteArray()); 077 final var fileIter = fileItems.iterator(); 078 add = 16; 079 num = 0; 080 for (var i = 0; i < 16384; i += add) { 081 if (++add == 32) { 082 add = 16; 083 } 084 final var item = fileIter.next(); 085 assertEquals("field" + num++, item.getFieldName()); 086 final var bytes = item.get(); 087 assertEquals(i, bytes.length); 088 for (var j = 0; j < i; j++) { 089 assertEquals((byte) j, bytes[j]); 090 } 091 } 092 assertTrue(!fileIter.hasNext()); 093 } 094 095 @Test 096 public void testParseImpliedUtf8() throws Exception { 097 // utf8 encoded form-data without explicit content-type encoding 098 // @formatter:off 099 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}