DashboardRedirectionFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse resp = (HttpServletResponse) response; if ("GET".equals(req.getMethod()) && !projectPattern.matcher(req.getRequestURI()).matches()) { resp.sendRedirect("/dashboard/"); return; } chain.doFilter(request, response); }  @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); @Override void init(FilterConfig filterConfig); @Override void destroy();  }
@Test public void shouldSkipRequestToProject() throws Exception { when(request.getMethod()).thenReturn("GET"); when(request.getRequestURI()).thenReturn("/namespace/ws-id/project1"); when(request.getRequestURL()) .thenReturn(new StringBuffer("http: filter.doFilter(request, response, chain); verify(chain).doFilter(nullable(ServletRequest.class), nullable(ServletResponse.class)); }
@Test(dataProvider = "nonNamespacePathProvider") public void shouldRedirectIfRequestWithoutNamespace(String uri, String url) throws Exception { when(request.getMethod()).thenReturn("GET"); when(request.getRequestURI()).thenReturn(uri); when(request.getRequestURL()).thenReturn(new StringBuffer(url)); EnvironmentContext context = new EnvironmentContext(); context.setSubject(new SubjectImpl("id123", "name", "token123", false)); EnvironmentContext.setCurrent(context); filter.doFilter(request, response, chain); verify(response).sendRedirect(eq("/dashboard/")); }
@Test(dataProvider = "notGETMethodProvider") public void shouldSkipNotGETRequest(String method) throws Exception { when(request.getMethod()).thenReturn(method); when(request.getRequestURI()).thenReturn("/ws/ws-id/project1"); when(request.getRequestURL()) .thenReturn(new StringBuffer("http: filter.doFilter(request, response, chain); verify(chain).doFilter(nullable(ServletRequest.class), nullable(ServletResponse.class)); }