/** * 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; }
@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()) .andExpect(model().attributeDoesNotExist("fileContent")) .andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah"))); }
public void testAddContacts() throws Exception{ File f = new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Penguins.jpg"); System.out.println(f.isFile()+" "+f.getName()+f.exists()); FileInputStream fi1 = new FileInputStream(f); FileInputStream fi2 = new FileInputStream(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Tulips.jpg")); MockMultipartFile fstmp = new MockMultipartFile("upload", f.getName(), "multipart/form-data",fi1); MockMultipartFile secmp = new MockMultipartFile("upload", "Tulips.jpg","multipart/form-data",fi2); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); mockMvc.perform(MockMvcRequestBuilders.fileUpload("/AddContacts") .file(fstmp) .file(secmp) .param("name","abc").param("email","abc@gmail.com").param("phone", "1234567890")) .andExpect(status().isOk()); }
@Test public void mockMultipartFileUploadWithContentType() throws Exception { OperationRequest request = createOperationRequest( MockMvcRequestBuilders.multipart("/foo").file(new MockMultipartFile( "file", "original", "image/png", new byte[] { 1, 2, 3, 4 }))); assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo")); assertThat(request.getMethod()).isEqualTo(HttpMethod.POST); assertThat(request.getParts().size()).isEqualTo(1); OperationRequestPart part = request.getParts().iterator().next(); assertThat(part.getName()).isEqualTo("file"); assertThat(part.getSubmittedFileName()).isEqualTo("original"); assertThat(part.getHeaders().getContentType()).isEqualTo(MediaType.IMAGE_PNG); assertThat(part.getContent()).isEqualTo(new byte[] { 1, 2, 3, 4 }); }
@Test public void mockMultipartFileUpload() throws Exception { OperationRequest request = createOperationRequest( MockMvcRequestBuilders.multipart("/foo") .file(new MockMultipartFile("file", new byte[] { 1, 2, 3, 4 }))); assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo")); assertThat(request.getMethod()).isEqualTo(HttpMethod.POST); assertThat(request.getParts().size()).isEqualTo(1); OperationRequestPart part = request.getParts().iterator().next(); assertThat(part.getName()).isEqualTo("file"); assertThat(part.getSubmittedFileName()).isNull(); assertThat(part.getHeaders().size()).isEqualTo(1); assertThat(part.getHeaders().getContentLength()).isEqualTo(4L); assertThat(part.getContent()).isEqualTo(new byte[] { 1, 2, 3, 4 }); }
public void body() throws Exception { // tag::body[] MockMultipartFile image = new MockMultipartFile("image", "image.png", "image/png", "<<png data>>".getBytes()); MockMultipartFile metadata = new MockMultipartFile("metadata", "", "application/json", "{ \"version\": \"1.0\"}".getBytes()); this.mockMvc.perform(fileUpload("/images").file(image).file(metadata) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andDo(document("image-upload", requestPartBody("metadata"))); // <1> // end::body[] }
public void fields() throws Exception { // tag::fields[] MockMultipartFile image = new MockMultipartFile("image", "image.png", "image/png", "<<png data>>".getBytes()); MockMultipartFile metadata = new MockMultipartFile("metadata", "", "application/json", "{ \"version\": \"1.0\"}".getBytes()); this.mockMvc.perform(fileUpload("/images").file(image).file(metadata) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andDo(document("image-upload", requestPartFields("metadata", // <1> fieldWithPath("version").description("The version of the image")))); // <2> // end::fields[] }