@Override public <T> T redirectTo(Class<T> controller) { return use(logic()).redirectTo(controller); }
@Override public <T> T onErrorRedirectTo(Class<T> controller) { return onErrorUse(logic()).redirectTo(controller); }
@Test public void whenRefererDontMatchAControllerShouldRedirectToPage() throws Exception { PageResult page = mock(PageResult.class); when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller"); when(request.getContextPath()).thenReturn("/vraptor"); when(router.parse("/no-controller", HttpMethod.GET, request)).thenThrow(new ControllerNotFoundException()); doReturn(page).when(result).use(page()); refererResult.redirect(); verify(page).redirectTo("/no-controller"); }
@Test public void shouldDelegateToLogicResultOnRedirectToLogicWithInstance() throws Exception { LogicResult logicResult = mockResult(LogicResult.class); result.redirectTo(new RandomController()); verify(logicResult).redirectTo(RandomController.class); }
@Test public void shouldNotPutParametersOnFlashScopeOnRedirectIfThereAreNoParameters() throws Exception { logicResult.redirectTo(MyComponent.class).base(); verify(flash, never()).includeParameters(any(ControllerMethod.class), any(Object[].class)); }
@Test public void shouldPutParametersOnFlashScopeOnRedirect() throws Exception { logicResult.redirectTo(MyComponent.class).withParameter("a"); verify(flash).includeParameters(any(ControllerMethod.class), eq(new Object[] {"a"})); }
@Test public void clientRedirectingWillRedirectToTranslatedUrl() throws NoSuchMethodException, IOException { final String url = "custom_url"; when(request.getContextPath()).thenReturn("/context"); when(router.urlFor(MyComponent.class, MyComponent.class.getDeclaredMethod("base"))).thenReturn(url); logicResult.redirectTo(MyComponent.class).base(); verify(response).sendRedirect("/context" + url); }
@Test public void canRedirectWhenLogicMethodIsNotAnnotatedWithHttpMethods() throws Exception { logicResult.redirectTo(MyComponent.class).base(); verify(response).sendRedirect(any(String.class)); }
@Test public void canRedirectWhenLogicMethodIsAnnotatedWithHttpGetMethod() throws Exception { logicResult.redirectTo(MyComponent.class).annotatedWithGet(); verify(response).sendRedirect(any(String.class)); }
@Test public void cannotRedirectWhenLogicMethodIsAnnotatedWithAnyHttpMethodButGet() throws Exception { try { logicResult.redirectTo(MyComponent.class).annotated(); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException e) { verify(response, never()).sendRedirect(any(String.class)); } }
@Override public void redirect() throws IllegalStateException { String referer = getReferer(); try { ControllerMethod method = router.parse(referer, HttpMethod.GET, request); executeMethod(method, result.use(logic()).redirectTo(method.getController().getType())); } catch (ControllerNotFoundException | MethodNotAllowedException e) { logger.warn("Could not find or doesn't allowed to get controller method", e); result.use(page()).redirectTo(referer); } }