HostBasedUrlAccessSecurityFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (hostValidatorService.isRequestFromAdmin(request)) { chain.doFilter(request, response); } else { if (request instanceof HttpServletRequest) { logger.info(urlAccessLogMessageAssembler.assembleDenyMessage((HttpServletRequest) request)); } sendBackNoAccessResponse(response); } }  @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); @Override void destroy();  }
@Test public void testdoFilterShouldPassRequestResponseToChainIfIsAdmin() throws IOException, ServletException { given(hostValidatorService.isRequestFromAdmin(servletRequest)).willReturn(true); underTest.doFilter(servletRequest, servletResponse, filterChain); verify(filterChain).doFilter(servletRequest, servletResponse); }
@Test public void testdoFilterShouldNotPassRequestResponseToChainIfNotAdmin() throws IOException, ServletException { given(hostValidatorService.isRequestFromAdmin(servletRequest)).willReturn(false); given(servletResponse.getWriter()).willReturn(printWriter); underTest.doFilter(servletRequest, servletResponse, filterChain); verify(filterChain, never()).doFilter(servletRequest, servletResponse); }
@Test public void testdoFilterShouldWriteErrorToResponseIfNotAdmin() throws IOException, ServletException { given(hostValidatorService.isRequestFromAdmin(servletRequest)).willReturn(false); given(servletResponse.getWriter()).willReturn(printWriter); underTest.doFilter(servletRequest, servletResponse, filterChain); verify(printWriter).write(Mockito.anyString()); }