@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 multipartRequestWithServletParts() throws Exception { byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8); MockPart filePart = new MockPart("file", "orig", fileContent); byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8); MockPart jsonPart = new MockPart("json", "json", json); jsonPart.getHeaders().setContentType(MediaType.APPLICATION_JSON); standaloneSetup(new MultipartController()).build() .perform(multipart("/multipartfile").part(filePart).part(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 public void multipartRequestWithSingleFileNotPresent() throws Exception { standaloneSetup(new MultipartController()).build() .perform(multipart("/multipartfile")) .andExpect(status().isFound()); }
@Test public void multipartRequestWithFileListNotPresent() throws Exception { standaloneSetup(new MultipartController()).build() .perform(multipart("/multipartfilelist")) .andExpect(status().isFound()); }
/** * Create a {@link MockHttpServletRequestBuilder} for a multipart request. * @param uri the URL * @return the builder for the file upload request */ public static MockMultipartHttpServletRequestBuilder fileUpload(URI uri) { return MockMvcRequestBuilders.multipart(uri); }
@Test public void multipartRequestWithFileArrayNotPresent() throws Exception { standaloneSetup(new MultipartController()).build() .perform(multipart("/multipartfilearray")) .andExpect(status().isFound()); }
@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"))); }
@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"))); }
@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 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"))); }
/** * Create a {@link MockHttpServletRequestBuilder} for a multipart request. The url * template will be captured and made available for documentation. * @param urlTemplate a URL template; the resulting URL will be encoded * @param urlVariables zero or more URL variables * @return the builder for the file upload request */ public static MockMultipartHttpServletRequestBuilder fileUpload(String urlTemplate, Object... urlVariables) { return (MockMultipartHttpServletRequestBuilder) MockMvcRequestBuilders .multipart(urlTemplate, urlVariables) .requestAttr(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate); }
@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 }); }
public void upload() throws Exception { // tag::request-parts[] this.mockMvc.perform(multipart("/upload").file("file", "example".getBytes())) // <1> .andExpect(status().isOk()) .andDo(document("upload", requestParts( // <2> partWithName("file").description("The file to upload")) // <3> )); // end::request-parts[] }
@Test public void requestPartsSnippet() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context) .apply(documentationConfiguration(this.restDocumentation)).build(); mockMvc.perform(multipart("/upload").file("foo", "bar".getBytes())) .andExpect(status().isOk()).andDo(document("request-parts", requestParts( partWithName("foo").description("The description")))); assertExpectedSnippetFilesExist( new File("build/generated-snippets/request-parts"), "http-request.adoc", "http-response.adoc", "curl-request.adoc", "request-parts.adoc"); }
@Test public void multiPart() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context) .apply(documentationConfiguration(this.restDocumentation)).build(); mockMvc.perform(multipart("/upload").file("test", "content".getBytes())) .andExpect(status().isOk()).andDo(document("upload", requestParts(partWithName("test").description("Foo")))); }