@Test(expected = IllegalArgumentException.class) public void addFiltersFiltersNull() { StandaloneMockMvcBuilder builder = MockMvcBuilders.standaloneSetup(new PersonController()); builder.addFilters((Filter[]) null); }
@Test(expected = IllegalArgumentException.class) public void addFiltersFiltersContainsNull() { StandaloneMockMvcBuilder builder = MockMvcBuilders.standaloneSetup(new PersonController()); builder.addFilters(new ContinueFilter(), (Filter) null); }
private void loadConfig(String... configLocations) { for (int i=0; i<configLocations.length; i++) { configLocations[i] = getClass().getName().replaceAll("\\.", "/") + "-" + configLocations[i]; } XmlWebApplicationContext context = new XmlWebApplicationContext(); context.setConfigLocations(configLocations); context.setServletContext(new MockServletContext()); context.refresh(); this.context = context; context.getAutowireCapableBeanFactory().autowireBean(this); Filter springSecurityFilterChain = context.getBean("springSecurityFilterChain", Filter.class); mockMvc = MockMvcBuilders .standaloneSetup(new FooController()) .addFilters(springSecurityFilterChain) .build(); }
@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 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 // SPR-16067, SPR-16695 public void filterWrapsRequestResponseAndPerformsAsyncDispatch() throws Exception { MockMvc mockMvc = standaloneSetup(new PersonController()) .addFilters(new WrappingRequestResponseFilter(), new ShallowEtagHeaderFilter()) .build(); MvcResult mvcResult = mockMvc.perform(get("/persons/1").accept(MediaType.APPLICATION_JSON)) .andExpect(request().asyncStarted()) .andExpect(request().asyncResult(new Person("Lukas"))) .andReturn(); mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().isOk()) .andExpect(header().longValue("Content-Length", 53)) .andExpect(header().string("ETag", "\"0e37becb4f0c90709cb2e1efcc61eaa00\"")) .andExpect(content().string("{\"name\":\"Lukas\",\"someDouble\":0.0,\"someBoolean\":false}")); }