protected TestProcess testPost(String api) throws Exception { MockHttpServletRequestBuilder msrb = initDefaultSetting(post(api)); return test(msrb); }
@Test public void testExists() throws Exception { this.mockMvc.perform(post("/persons")) .andExpect(flash().attributeExists("one", "two", "three")); }
@Test public void testHasErrors() throws Exception { mockMvc.perform(post("/persons")).andExpect(model().attributeHasErrors("person")); }
@Test public void testEqualTo() throws Exception { this.mockMvc.perform(post("/persons")) .andExpect(flash().attribute("one", "1")) .andExpect(flash().attribute("two", 2.222)) .andExpect(flash().attribute("three", new URL("http://example.com"))) .andExpect(flash().attribute("one", equalTo("1"))) // Hamcrest... .andExpect(flash().attribute("two", equalTo(2.222))) .andExpect(flash().attribute("three", equalTo(new URL("http://example.com")))); }
@Test public void testMatchers() throws Exception { this.mockMvc.perform(post("/persons")) .andExpect(flash().attribute("one", containsString("1"))) .andExpect(flash().attribute("two", closeTo(2, 0.5))) .andExpect(flash().attribute("three", notNullValue())); }
@Test public void multipartRequestWithFileListNoMultipart() throws Exception { standaloneSetup(new MultipartController()).build() .perform(post("/multipartfilelist")) .andExpect(status().isFound()); }
@Test public void multipartRequestWithFileArrayNoMultipart() throws Exception { standaloneSetup(new MultipartController()).build() .perform(post("/multipartfilearray")) .andExpect(status().isFound()); }
private static RequestBuilder formLogin(MockHttpSession session) { return post("/login") .param("username", "user") .param("password", "password") .session(session) .with(csrf()); } }
@Test public void filterWithExactMapping() throws Exception { standaloneSetup(new PersonController()) .addFilter(new RedirectFilter(), "/p", "/persons").build() .perform(post("/persons").param("name", "Andy")) .andExpect(redirectedUrl("/login")); }
@Test public void filterMappedBySuffix() throws Exception { standaloneSetup(new PersonController()) .addFilter(new RedirectFilter(), "*.html").build() .perform(post("/persons.html").param("name", "Andy")) .andExpect(redirectedUrl("/login")); }
@Test public void filterWrapsRequestResponse() throws Exception { standaloneSetup(new PersonController()) .addFilters(new WrappingRequestResponseFilter()).build() .perform(post("/user")) .andExpect(model().attribute("principal", WrappingRequestResponseFilter.PRINCIPAL_NAME)); }
@Test public void filtersProcessRequest() throws Exception { standaloneSetup(new PersonController()) .addFilters(new ContinueFilter(), new RedirectFilter()).build() .perform(post("/persons").param("name", "Andy")) .andExpect(redirectedUrl("/login")); }
@Test public void postWhenCsrfDisabledThenRequestAllowed() throws Exception { this.spring.configLocations( this.xml("shared-controllers"), this.xml("CsrfDisabled") ).autowire(); this.mvc.perform(post("/ok")) .andExpect(status().isOk()); assertThat(getFilter(this.spring, CsrfFilter.class)).isNull(); }
@Test public void postWhenCsrfElementEnabledThenForbidden() throws Exception { this.spring.configLocations( this.xml("CsrfEnabled") ).autowire(); this.mvc.perform(post("/csrf")) .andExpect(status().isForbidden()) .andExpect(csrfCreated()); }
@Test public void saveWithErrors() throws Exception { this.mockMvc.perform(post("/persons")) .andExpect(status().isOk()) .andExpect(forwardedUrl("persons/add")) .andExpect(model().size(1)) .andExpect(model().attributeExists("person")) .andExpect(flash().attributeCount(0)); }
@Test public void saveSpecialWithErrors() throws Exception { this.mockMvc.perform(post("/people")) .andExpect(status().isOk()) .andExpect(forwardedUrl("persons/add")) .andExpect(model().size(1)) .andExpect(model().attributeExists("person")) .andExpect(flash().attributeCount(0)); }
@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!")); }