@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 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 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 multipartRequestWithSingleFileNotPresent() throws Exception { standaloneSetup(new MultipartController()).build() .perform(multipart("/multipartfile")) .andExpect(status().isFound()); }
@Test public void multipartRequestWithFileListNoMultipart() throws Exception { standaloneSetup(new MultipartController()).build() .perform(post("/multipartfilelist")) .andExpect(status().isFound()); }
@Test public void multipartRequestWithFileArrayNotPresent() throws Exception { standaloneSetup(new MultipartController()).build() .perform(multipart("/multipartfilearray")) .andExpect(status().isFound()); }
@Test public void multipartRequestWithFileArrayNoMultipart() throws Exception { standaloneSetup(new MultipartController()).build() .perform(post("/multipartfilearray")) .andExpect(status().isFound()); }
@Test public void multipartRequestWithFileListNotPresent() throws Exception { standaloneSetup(new MultipartController()).build() .perform(multipart("/multipartfilelist")) .andExpect(status().isFound()); }
@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 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 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 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 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 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"))); }
@Before public void setup() { this.mockMvc = standaloneSetup(new PersonController()) .alwaysExpect(status().isFound()) .alwaysExpect(flash().attributeCount(3)) .build(); }
@Test public void saveSpecial() throws Exception { this.mockMvc.perform(post("/people").param("name", "Andy")) .andExpect(status().isFound()) .andExpect(redirectedUrl("/persons/Joe")) .andExpect(model().size(1)) .andExpect(model().attributeExists("name")) .andExpect(flash().attributeCount(1)) .andExpect(flash().attribute("message", "success!")); }
@Test public void save() throws Exception { this.mockMvc.perform(post("/persons").param("name", "Andy")) .andExpect(status().isFound()) .andExpect(redirectedUrl("/persons/Joe")) .andExpect(model().size(1)) .andExpect(model().attributeExists("name")) .andExpect(flash().attributeCount(1)) .andExpect(flash().attribute("message", "success!")); }
@Test public void whenFiltersCompleteMvcProcessesRequest() throws Exception { standaloneSetup(new PersonController()) .addFilters(new ContinueFilter()).build() .perform(post("/persons").param("name", "Andy")) .andExpect(status().isFound()) .andExpect(redirectedUrl("/person/1")) .andExpect(model().size(1)) .andExpect(model().attributeExists("id")) .andExpect(flash().attributeCount(1)) .andExpect(flash().attribute("message", "success!")); }
@Test public void filterSkipped() throws Exception { standaloneSetup(new PersonController()) .addFilter(new RedirectFilter(), "/p", "/person").build() .perform(post("/persons").param("name", "Andy")) .andExpect(status().isFound()) .andExpect(redirectedUrl("/person/1")) .andExpect(model().size(1)) .andExpect(model().attributeExists("id")) .andExpect(flash().attributeCount(1)) .andExpect(flash().attribute("message", "success!")); }