Revisit MockPart constructors

Issue: SPR-15854
This commit is contained in:
Juergen Hoeller
2017-09-20 10:55:06 +02:00
parent 4cbef27f90
commit ea01c4113a
4 changed files with 46 additions and 61 deletions

View File

@@ -26,13 +26,15 @@ import javax.servlet.http.Part;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
/**
* Mock implementation of {@code javax.servlet.http.Part}.
*
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @since 3.1
* @see MockHttpServletRequest#addPart
* @see MockMultipartFile
*/
public class MockPart implements Part {
@@ -53,19 +55,11 @@ public class MockPart implements Part {
this(name, null, content);
}
/**
* Constructor for a part with a filename and streamed content.
* @see #getHeaders()
*/
public MockPart(String name, String filename, InputStream content) throws IOException {
this(name, filename, FileCopyUtils.copyToByteArray(content));
}
/**
* Constructor for a part with a filename and byte[] content.
* @see #getHeaders()
*/
private MockPart(String name, String filename, byte[] content) {
public MockPart(String name, String filename, byte[] content) {
Assert.hasLength(name, "Name must not be null");
this.name = name;
this.filename = filename;
@@ -100,6 +94,16 @@ public class MockPart implements Part {
return new ByteArrayInputStream(this.content);
}
@Override
public void write(String fileName) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void delete() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public String getHeader(String name) {
return this.headers.getFirst(name);
@@ -117,20 +121,11 @@ public class MockPart implements Part {
}
/**
* Return the {@link HttpHeaders} backing header related accessor methods.
* Return the {@link HttpHeaders} backing header related accessor methods,
* allowing for populating selected header entries.
*/
public HttpHeaders getHeaders() {
public final HttpHeaders getHeaders() {
return this.headers;
}
@Override
public void write(String fileName) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void delete() throws IOException {
throw new UnsupportedOperationException();
}
}

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.multipart.support;
import org.junit.Test;
@@ -21,19 +22,17 @@ import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockPart;
import org.springframework.web.multipart.MultipartFile;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.*;
/**
* Unit tests for {@link StandardMultipartHttpServletRequest}.
*
* @author Rossen Stoyanchev
*/
public class StandardMultipartHttpServletRequestTests {
@Test
public void filename() throws Exception {
StandardMultipartHttpServletRequest request = getRequest(
"file", "form-data; name=\"file\"; filename=\"myFile.txt\"");
@@ -42,9 +41,8 @@ public class StandardMultipartHttpServletRequestTests {
assertEquals("myFile.txt", multipartFile.getOriginalFilename());
}
@Test // SPR-13319
@Test // SPR-13319
public void filenameRfc5987() throws Exception {
StandardMultipartHttpServletRequest request = getRequest(
"file", "form-data; name=\"file\"; filename*=\"UTF-8''foo-%c3%a4-%e2%82%ac.html\"");
@@ -53,9 +51,8 @@ public class StandardMultipartHttpServletRequestTests {
assertEquals("foo-ä-€.html", multipartFile.getOriginalFilename());
}
@Test // SPR-15205
@Test // SPR-15205
public void filenameRfc2047() throws Exception {
StandardMultipartHttpServletRequest request = getRequest(
"file", "form-data; name=\"file\"; filename=\"=?UTF-8?Q?Declara=C3=A7=C3=A3o.pdf?=\"");
@@ -67,7 +64,7 @@ public class StandardMultipartHttpServletRequestTests {
private StandardMultipartHttpServletRequest getRequest(String name, String dispositionValue) {
MockHttpServletRequest request = new MockHttpServletRequest();
MockPart part = new MockPart(name, new byte[0]);
MockPart part = new MockPart(name, null);
part.getHeaders().set("Content-Disposition", dispositionValue);
request.addPart(part);
return new StandardMultipartHttpServletRequest(request);