@Test public void saveAndLoad() { service.store(new MockMultipartFile("foo", "foo.txt", MediaType.TEXT_PLAIN_VALUE, "Hello World".getBytes())); assertThat(service.load("foo.txt")).exists();
@Test(expected = StorageException.class) public void saveNotPermitted() { service.store(new MockMultipartFile("foo", "../foo.txt", MediaType.TEXT_PLAIN_VALUE, "Hello World".getBytes()));
@Test public void savePermitted() { service.store(new MockMultipartFile("foo", "bar/../foo.txt", MediaType.TEXT_PLAIN_VALUE, "Hello World".getBytes()));
@Test public void shouldSaveUploadedFile() throws Exception { MockMultipartFile multipartFile = new MockMultipartFile("file", "test.txt", "text/plain", "Spring Framework".getBytes()); this.mvc.perform(fileUpload("/").file(multipartFile)) .andExpect(status().isFound()) .andExpect(header().string("Location", "/")); then(this.storageService).should().store(multipartFile);
/**
* Create a new MockMultipartFile with the given content.
* @param name the name of the file
* @param content the content of the file
public MockMultipartHttpServletRequestBuilder file(String name, byte[] content) {
this.files.add(new MockMultipartFile(name, content));
return this;
@Test public void testUpload() throws Exception { String endpoint = BASE_URL + "/upload/photo"; FileInputStream fis = new FileInputStream("/home/me/Desktop/someDir/image.jpg"); MockMultipartFile multipartFile = new MockMultipartFile("file", fis); HashMap<String, String> contentTypeParams = new HashMap<String, String>(); contentTypeParams.put("boundary", "265001916915724"); MediaType mediaType = new MediaType("multipart", "form-data", contentTypeParams); mockMvc.perform( post(endpoint) .content(multipartFile.getBytes()) .contentType(mediaType)) .andExpect(status().isOk());
@WebAppConfiguration @ContextConfiguration(classes = WebConfig.class) @RunWith(SpringJUnit4ClassRunner.class) public class Example { @Autowired private WebApplicationContext webApplicationContext; @Test public void test() throws Exception { MockMultipartFile firstFile = new MockMultipartFile("data", "filename.txt", "text/plain", "some xml".getBytes()); MockMultipartFile secondFile = new MockMultipartFile("data", "other-file-name.data", "text/plain", "some other type".getBytes()); MockMultipartFile jsonFile = new MockMultipartFile("json", "", "application/json", "{\"json\": \"someValue\"}".getBytes()); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); mockMvc.perform(MockMvcRequestBuilders.fileUpload("/upload") .file(firstFile) .file(secondFile).file(jsonFile) .param("some-random", "4")) .andExpect(status().is(200)) .andExpect(content().string("success"));
@Test public void mockMultipartHttpServletRequestWithInputStream() throws IOException { MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(); request.addFile(new MockMultipartFile("file1", new ByteArrayInputStream("myContent1".getBytes()))); request.addFile(new MockMultipartFile("file2", "myOrigFilename", "text/plain", new ByteArrayInputStream( "myContent2".getBytes()))); doTestMultipartHttpServletRequest(request);
@Test public void mockMultipartHttpServletRequestWithByteArray() throws IOException { MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(); assertFalse(request.getFileNames().hasNext()); assertNull(request.getFile("file1")); assertNull(request.getFile("file2")); assertTrue(request.getFileMap().isEmpty()); request.addFile(new MockMultipartFile("file1", "myContent1".getBytes())); request.addFile(new MockMultipartFile("file2", "myOrigFilename", "text/plain", "myContent2".getBytes())); doTestMultipartHttpServletRequest(request);
@Test public void multipartRequestWithFileList() throws Exception { byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8); MockMultipartFile filePart1 = new MockMultipartFile("file", "orig", null, fileContent); MockMultipartFile filePart2 = new MockMultipartFile("file", "orig", null, fileContent); byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8); MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json); standaloneSetup(new MultipartController()).build() .perform(multipart("/multipartfilelist").file(filePart1).file(filePart2).file(jsonPart)) .andExpect(status().isFound()) .andExpect(model().attribute("fileContent", fileContent)) .andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
@Test public void multipartRequestWithOptionalFileArray() throws Exception { byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8); MockMultipartFile filePart1 = new MockMultipartFile("file", "orig", null, fileContent); MockMultipartFile filePart2 = new MockMultipartFile("file", "orig", null, fileContent); byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8); MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json); standaloneSetup(new MultipartController()).build() .perform(multipart("/optionalfilearray").file(filePart1).file(filePart2).file(jsonPart)) .andExpect(status().isFound()) .andExpect(model().attribute("fileContent", fileContent)) .andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
@Test public void multipartRequestWithFileArray() throws Exception { byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8); MockMultipartFile filePart1 = new MockMultipartFile("file", "orig", null, fileContent); MockMultipartFile filePart2 = new MockMultipartFile("file", "orig", null, fileContent); byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8); MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json); standaloneSetup(new MultipartController()).build() .perform(multipart("/multipartfilearray").file(filePart1).file(filePart2).file(jsonPart)) .andExpect(status().isFound()) .andExpect(model().attribute("fileContent", fileContent)) .andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
@Test public void multipartRequestWithOptionalFileList() throws Exception { byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8); MockMultipartFile filePart1 = new MockMultipartFile("file", "orig", null, fileContent); MockMultipartFile filePart2 = new MockMultipartFile("file", "orig", null, fileContent); byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8); MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json); standaloneSetup(new MultipartController()).build() .perform(multipart("/optionalfilelist").file(filePart1).file(filePart2).file(jsonPart)) .andExpect(status().isFound()) .andExpect(model().attribute("fileContent", fileContent)) .andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
@Test public void multipartRequestWithOptionalFile() throws Exception { byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8); MockMultipartFile filePart = new MockMultipartFile("file", "orig", null, fileContent); byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8); MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json); standaloneSetup(new MultipartController()).build() .perform(multipart("/optionalfile").file(filePart).file(jsonPart)) .andExpect(status().isFound()) .andExpect(model().attribute("fileContent", fileContent)) .andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
@Test public void multipartRequestWithSingleFile() throws Exception { byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8); MockMultipartFile filePart = new MockMultipartFile("file", "orig", null, fileContent); byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8); MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json); standaloneSetup(new MultipartController()).build() .perform(multipart("/multipartfile").file(filePart).file(jsonPart)) .andExpect(status().isFound()) .andExpect(model().attribute("fileContent", fileContent)) .andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
@Test // SPR-13317 public void multipartRequestWrapped() throws Exception { byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8); MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json); Filter filter = new RequestWrappingFilter(); MockMvc mockMvc = standaloneSetup(new MultipartController()).addFilter(filter).build(); Map<String, String> jsonMap = Collections.singletonMap("name", "yeeeah"); mockMvc.perform(multipart("/json").file(jsonPart)).andExpect(model().attribute("json", jsonMap));
@Test public void multipartRequestWithOptionalFileArrayNotPresent() throws Exception { byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8); MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json); standaloneSetup(new MultipartController()).build() .perform(multipart("/optionalfilearray").file(jsonPart)) .andExpect(status().isFound()) .andExpect(model().attributeDoesNotExist("fileContent")) .andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
String mimeType = multiPart.getMimeType();
if (multiPart.isByteArray()) {
multipartFile = new MockMultipartFile(controlName, fileName, mimeType, (byte[]) multiPart.getContent());
} else if (multiPart.isFile() || multiPart.isInputStream()) {
InputStream inputStream;
multipartFile = new MockMultipartFile(controlName, fileName, mimeType, inputStream);
} catch (IOException e) {
return SafeExceptionRethrower.safeRethrow(e);
multipartFile = new MockMultipartFile(controlName, fileName, mimeType, ((String) multiPart.getContent()).getBytes());
@Test public void multipartRequestWithOptionalFileNotPresent() throws Exception { byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8); MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json); standaloneSetup(new MultipartController()).build() .perform(multipart("/optionalfile").file(jsonPart)) .andExpect(status().isFound()) .andExpect(model().attributeDoesNotExist("fileContent")) .andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
@Test public void multipartRequestWithOptionalFileListNotPresent() throws Exception { byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8); MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json); standaloneSetup(new MultipartController()).build() .perform(multipart("/optionalfilelist").file(jsonPart)) .andExpect(status().isFound())