### Question:
HttpBasicAuthenticationFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { LOG.trace("doFilter()"); HttpServletRequest request = (HttpServletRequest)servletRequest; if (enabled() && !isCorsPreflight(request)) { LOG.trace("Authentication is enabled"); HttpServletResponse response = (HttpServletResponse)servletResponse; Optional<String> authHeader = getAuthorizationHeader(request); if (authHeader.isPresent()) { Credentials credentials = Credentials.fromHeader(authHeader.get()); if (!isAuthorized(credentials)) { LOG.trace("Credentials do NOT match; Authorizationi failed"); sendUnauthorized(response); return; } LOG.trace("User {} is authorized", credentials.getUser()); } else { LOG.trace("Missing Authorization HTTP header; Authorization failed"); sendUnauthorized(response); return; } } else { LOG.trace("Authentication is NOT enabled (skipping...)"); } chain.doFilter(servletRequest, servletResponse); } HttpBasicAuthenticationFilter(NewtsConfig config); @Override void init(FilterConfig filterConfig); @Override void destroy(); @Override void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain);  }### Answer:
@Test public void testCorsPreflight() throws IOException, ServletException { when(m_request.getHeader("Authorization")).thenReturn(null); when(m_request.getMethod()).thenReturn("OPTIONS"); when(m_request.getHeader("Origin")).thenReturn("http: when(m_request.getHeader("Access-Control-Request-Method")).thenReturn("POST"); m_filter.doFilter(m_request, m_response, m_chain); verify(m_chain).doFilter(m_request, m_response); }
@Test public void testFilterEnabled() throws IOException, ServletException { m_filter.doFilter(m_request, m_response, m_chain); verify(m_chain).doFilter(m_request, m_response); when(m_request.getHeader("Authorization")).thenReturn(basicAuthHeader("peter", "sparkles")); m_filter.doFilter(m_request, m_response, m_chain); verify(m_response).sendError(eq(401), any(String.class)); }
@Test public void testFilterDisabled() throws IOException, ServletException { when(m_request.getHeader("Authorization")).thenReturn(basicAuthHeader("peter", "sparkles")); when(m_authConfig.isEnabled()).thenReturn(false); m_filter.doFilter(m_request, m_response, m_chain); verify(m_chain).doFilter(m_request, m_response); }