/** * Asserts the request was forwarded 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 forwardedUrlTemplate(String urlTemplate, Object... uriVars) { String uri = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(uriVars).encode().toUriString(); return forwardedUrl(uri); }
@Test public void forwardWithQueryString() throws Exception { forwardedUrl("/api/resource/1?arg=value").match(getForwardedUrlStubMvcResult("/api/resource/1?arg=value")); }
@Test public void forward() throws Exception { forwardedUrl("/api/resource/1").match(getForwardedUrlStubMvcResult("/api/resource/1")); }
@Test public void tilesDefinitions() throws Exception { this.mockMvc.perform(get("/")) .andExpect(forwardedUrl("/WEB-INF/layouts/standardLayout.jsp")); }
@Test public void testForward() throws Exception { this.mockMvc.perform(get("/")).andExpect(forwardedUrl("/home")); }
@Test public void tilesDefinitions() throws Exception { this.mockMvc.perform(get("/"))// .andExpect(status().isOk())// .andExpect(forwardedUrl("/WEB-INF/layouts/standardLayout.jsp")); }
@Test public void resourcesViaDefaultServlet() throws Exception { this.mockMvc.perform(get("/unknown/resource")) .andExpect(handler().handlerType(DefaultServletHttpRequestHandler.class)) .andExpect(forwardedUrl("default")); }
@Test public void tilesDefinitions() throws Exception { this.mockMvc.perform(get("/")) .andExpect(status().isOk()) .andExpect(forwardedUrl("/WEB-INF/layouts/standardLayout.jsp")); }
@Test public void defaultViewResolver() throws Exception { standaloneSetup(new PersonController()).build() .perform(get("/person/Corea")) .andExpect(model().attribute("person", hasProperty("name", equalTo("Corea")))) .andExpect(status().isOk()) .andExpect(forwardedUrl("person/show")); // InternalResourceViewResolver }
@Test public void testGlobalExceptionHandlerMethodUsingClassArgument() throws Exception { standaloneSetup(PersonController.class).setControllerAdvice(GlobalExceptionHandler.class).build() .perform(get("/person/Bonnie")) .andExpect(status().isOk()) .andExpect(forwardedUrl("globalErrorView")); }
@Test public void testExceptionHandlerMethod() throws Exception { standaloneSetup(new PersonController()).build() .perform(get("/person/Clyde")) .andExpect(status().isOk()) .andExpect(forwardedUrl("errorView")); }
@Test public void testGlobalExceptionHandlerMethod() throws Exception { standaloneSetup(new PersonController()).setControllerAdvice(new GlobalExceptionHandler()).build() .perform(get("/person/Bonnie")) .andExpect(status().isOk()) .andExpect(forwardedUrl("globalErrorView")); }
@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 testJspOnly() throws Exception { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver("/WEB-INF/", ".jsp"); standaloneSetup(new PersonController()).setViewResolvers(viewResolver).build() .perform(get("/person/Corea")) .andExpect(status().isOk()) .andExpect(model().size(1)) .andExpect(model().attributeExists("person")) .andExpect(forwardedUrl("/WEB-INF/person/show.jsp")); }
@Test // http@access-denied-page public void configureWhenAccessDeniedPageSetAndRequestForbiddenThenForwardedToAccessDeniedPage() throws Exception { this.spring.register(AccessDeniedPageConfig.class).autowire(); this.mockMvc.perform(get("/admin").with(user(PasswordEncodedUser.user()))) .andExpect(status().isForbidden()) .andExpect(forwardedUrl("/AccessDeniedPage")); }
@Test public void successfulLoginWhenConfiguredWithCustomAuthenticationSuccessThenForwardsAccordingly() throws Exception { this.spring.configLocations(this.xml("WithAuthenticationSuccessForwardUrl")).autowire(); this.mvc.perform(post("/login") .param("username", "user") .param("password", "password")) .andExpect(status().isOk()) .andExpect(forwardedUrl("/success_forward_url")); }
@Test public void getPerson() throws Exception { this.mockMvc.perform(get("/persons/Joe").flashAttr("message", "success!")) .andExpect(status().isOk()) .andExpect(forwardedUrl("persons/index")) .andExpect(model().size(2)) .andExpect(model().attribute("person", new Person("Joe"))) .andExpect(model().attribute("message", "success!")) .andExpect(flash().attributeCount(0)); }
@Test public void failedLoginWhenConfiguredWithCustomAuthenticationFailureThenForwardsAccordingly() throws Exception { this.spring.configLocations(this.xml("WithAuthenticationFailureForwardUrl")).autowire(); this.mvc.perform(post("/login") .param("username", "bob") .param("password", "invalidpassword")) .andExpect(status().isOk()) .andExpect(forwardedUrl("/failure_forward_url")) .andExpect(request().attribute(WebAttributes.AUTHENTICATION_EXCEPTION, not(nullValue()))); }
.andExpect(model().size(1)) .andExpect(model().attributeExists("person")) .andExpect(forwardedUrl("person/show"));