/** * Asserts the request was redirected to the given URL template. * <p>This method accepts exact matches against the expanded and encoded URL template. * @param urlTemplate a URL template; the expanded URL will be encoded * @param uriVars zero or more URI variables to populate the template * @see UriComponentsBuilder#fromUriString(String) */ public static ResultMatcher redirectedUrlTemplate(String urlTemplate, Object... uriVars) { String uri = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(uriVars).encode().toUriString(); return redirectedUrl(uri); }
@Test public void redirect() throws Exception { redirectedUrl("/resource/1").match(getRedirectedUrlStubMvcResult("/resource/1")); }
@Test public void testRedirect() throws Exception { this.mockMvc.perform(get("/persons")).andExpect(redirectedUrl("/persons/1")); }
@Test public void requestWhenOpenIDAndFormLoginBothConfiguredThenRedirectsToGeneratedLoginPage() throws Exception { this.spring.configLocations(this.xml("WithFormLogin")).autowire(); this.mvc.perform(get("/")) .andExpect(status().isFound()) .andExpect(redirectedUrl("http://localhost/login")); assertThat(getFilter(DefaultLoginPageGeneratingFilter.class)).isNotNull(); }
@Test public void requestWhenPortMapperTwiceInvokedThenDoesNotOverride() throws Exception { this.spring.register(InvokeTwiceDoesNotOverride.class).autowire(); this.mockMvc.perform(get("http://localhost:543")) .andExpect(redirectedUrl("https://localhost:123")); }
@Test public void getWhenBookmarkedUrlIsFaviconIcoThenPostAuthenticationRedirectsToRoot() throws Exception { this.spring.register(RequestCacheDefaultsConfig.class, DefaultSecurityConfig.class).autowire(); MockHttpSession session = (MockHttpSession) this.mvc.perform(get("/favicon.ico")) .andExpect(redirectedUrl("http://localhost/login")) .andReturn().getRequest().getSession(); this.mvc.perform(formLogin(session)) .andExpect(redirectedUrl("/")); // ignores favicon.ico }
@Test public void getWhenBookmarkedUrlIsFaviconPngThenPostAuthenticationRedirectsToRoot() throws Exception { this.spring.register(RequestCacheDefaultsConfig.class, DefaultSecurityConfig.class).autowire(); MockHttpSession session = (MockHttpSession) this.mvc.perform(get("/favicon.png")) .andExpect(redirectedUrl("http://localhost/login")) .andReturn().getRequest().getSession(); this.mvc.perform(formLogin(session)) .andExpect(redirectedUrl("/")); // ignores favicon.png }
@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 getWhenBookmarkedRequestIsRequestedWithAndroidThenPostAuthenticationRemembers() throws Exception { this.spring.register(RequestCacheDefaultsConfig.class, DefaultSecurityConfig.class).autowire(); MockHttpSession session = (MockHttpSession) this.mvc.perform(get("/messages") .header("X-Requested-With", "com.android")) .andExpect(redirectedUrl("http://localhost/login")) .andReturn().getRequest().getSession(); this.mvc.perform(formLogin(session)) .andExpect(redirectedUrl("http://localhost/messages")); }
@Test public void getWhenBookmarkedRequestIsApplicationJsonThenPostAuthenticationRedirectsToRoot() throws Exception { this.spring.register(RequestCacheDefaultsConfig.class, DefaultSecurityConfig.class).autowire(); MockHttpSession session = (MockHttpSession) this.mvc.perform(get("/messages") .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)) .andExpect(redirectedUrl("http://localhost/login")) .andReturn().getRequest().getSession(); this.mvc.perform(formLogin(session)) .andExpect(redirectedUrl("/")); // ignores application/json // This is desirable since JSON requests are typically not invoked directly from the browser and we don't want the browser to replay them }
@Test public void getProtectedPageWhenFormLoginConfiguredThenRedirectsToDefaultLoginPage() throws Exception { this.spring.configLocations(this.xml("WithAntRequestMatcher")).autowire(); this.mvc.perform(get("/")) .andExpect(redirectedUrl("http://localhost/login")); }
@Test public void getWhenPortsMappedThenRedirectedAccordingly() throws Exception { this.spring.configLocations(xml("PortsMappedInterceptUrlMethodRequiresAny")).autowire(); this.mvc.perform(get("http://localhost:9080/protected")) .andExpect(redirectedUrl("https://localhost:9443/protected")); }
@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 logoutWhenSpecifyingSuccessHandlerRefThenResponseHandledAccordingly() throws Exception { this.spring.configLocations(xml("LogoutSuccessHandlerRef")).autowire(); this.mvc.perform(post("/logout").with(csrf())) .andExpect(redirectedUrl("/logoutSuccessEndpoint")); }
@Test public void servletAuthenticateWhenUsingDefaultConfigurationThenUsesSpringSecurity() throws Exception { this.spring.configLocations(this.xml("Simple")).autowire(); this.mvc.perform(get("/authenticate")) .andExpect(status().isFound()) .andExpect(redirectedUrl("http://localhost/login")); }
@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!")); }