DomainValidateFilter implements Filter { @VisibleForTesting protected boolean shouldCheck() { if(!shouldCheckSetting.get() || !shouldCheckConfig.getAsBoolean()) { logger.debug("[domain check stopped] {}, {}", shouldCheckSetting.get(), shouldCheckConfig.getAsBoolean()); return false; } return true; } DomainValidateFilter(BooleanSupplier shouldCheckConfig, Supplier<String> expectedDomainName); @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain); @Override void destroy(); static final String STOP_CHECK_URI; static final String START_CHECK_URI; static final String HTTP_REQUEST_HEADER_HOST; static final int BAD_REQUEST_CODE; static final String BAD_REQUEST_MESSAGE_TEMPLATE; }
@Test public void testShouldCheck() throws Exception { filter = spy(filter); shouldCheck = false; Assert.assertFalse(filter.shouldCheck()); domain = "localhost"; MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addHeader(HTTP_REQUEST_HEADER_HOST, "error.domain"); filter.doFilter(request, response, filterChain); Assert.assertEquals(200, response.getStatus()); verify(filter, never()).removePortIfNeeded(domain); verify(filter, never()).removeProtocolIfNeeded(domain); verify(filter, never()).removePortIfNeeded("error.domain"); }