Revisit MockPart constructors
Issue: SPR-15854
This commit is contained in:
@@ -27,13 +27,15 @@ import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
/**
|
||||
* Mock implementation of {@code javax.servlet.http.Part}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.0
|
||||
* @author Juergen Hoeller
|
||||
* @since 4.3.12
|
||||
* @see MockHttpServletRequest#addPart
|
||||
* @see MockMultipartFile
|
||||
*/
|
||||
public class MockPart implements Part {
|
||||
|
||||
@@ -55,19 +57,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, @Nullable 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, @Nullable String filename, @Nullable byte[] content) {
|
||||
public MockPart(String name, @Nullable String filename, @Nullable byte[] content) {
|
||||
Assert.hasLength(name, "Name must not be null");
|
||||
this.name = name;
|
||||
this.filename = filename;
|
||||
@@ -104,6 +98,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
|
||||
@Nullable
|
||||
public String getHeader(String name) {
|
||||
@@ -122,20 +126,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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.test.web.servlet.samples.standalone;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
@@ -44,10 +43,9 @@ import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
|
||||
|
||||
/**
|
||||
* @author Rossen Stoyanchev
|
||||
@@ -73,10 +71,10 @@ public class MultipartControllerTests {
|
||||
@Test
|
||||
public void multipartRequestWithServletParts() throws Exception {
|
||||
byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
|
||||
MockPart filePart = new MockPart("file", "orig", new ByteArrayInputStream(fileContent));
|
||||
MockPart filePart = new MockPart("file", "orig", fileContent);
|
||||
|
||||
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
|
||||
MockPart jsonPart = new MockPart("json", "json", new ByteArrayInputStream(json));
|
||||
MockPart jsonPart = new MockPart("json", "json", json);
|
||||
jsonPart.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
||||
|
||||
standaloneSetup(new MultipartController()).build()
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user