RequestLoggingFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (HttpServletRequest.class.isAssignableFrom(request.getClass()) && HttpServletResponse.class.isAssignableFrom(response.getClass())) { doFilterRequest((HttpServletRequest) request, (HttpServletResponse) response, chain); } else { chain.doFilter(request, response); } } RequestLoggingFilter();   RequestLoggingFilter(RequestRecordFactory requestRecordFactory); @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); @Override void destroy(); static final String LOG_PROVIDER; static final String WRAP_RESPONSE_INIT_PARAM; static final String WRAP_REQUEST_INIT_PARAM; }
@Test public void testSimple() throws IOException, ServletException { FilterChain mockFilterChain = mock(FilterChain.class); new RequestLoggingFilter().doFilter(mockReq, mockResp, mockFilterChain); assertThat(getField(Fields.REQUEST), is(Defaults.UNKNOWN)); assertThat(getField(Fields.CORRELATION_ID), not(isEmptyOrNullString())); assertThat(getField(Fields.REQUEST_ID), is(Defaults.UNKNOWN)); assertThat(getField(Fields.REMOTE_HOST), is(Defaults.UNKNOWN)); assertThat(getField(Fields.COMPONENT_ID), is(Defaults.UNKNOWN)); assertThat(getField(Fields.CONTAINER_ID), is(Defaults.UNKNOWN)); assertThat(getField(Fields.REQUEST_SIZE_B), is("-1")); }
@Test public void testInputStream() throws IOException, ServletException { ServletInputStream mockStream = mock(ServletInputStream.class); when(mockReq.getInputStream()).thenReturn(mockStream); when(mockStream.read()).thenReturn(1); FilterChain mockFilterChain = new FilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { request.getInputStream().read(); } }; new RequestLoggingFilter().doFilter(mockReq, mockResp, mockFilterChain); assertThat(getField(Fields.REQUEST), is(Defaults.UNKNOWN)); assertThat(getField(Fields.CORRELATION_ID), not(isEmptyOrNullString())); assertThat(getField(Fields.REQUEST_ID), is(Defaults.UNKNOWN)); assertThat(getField(Fields.REMOTE_HOST), is(Defaults.UNKNOWN)); assertThat(getField(Fields.COMPONENT_ID), is(Defaults.UNKNOWN)); assertThat(getField(Fields.CONTAINER_ID), is(Defaults.UNKNOWN)); assertThat(getField(Fields.REQUEST_SIZE_B), is("1")); }
@Test public void testReader() throws IOException, ServletException { BufferedReader reader = new BufferedReader(new StringReader("TEST")); when(mockReq.getReader()).thenReturn(reader); FilterChain mockFilterChain = new FilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { request.getReader().read(); } }; new RequestLoggingFilter().doFilter(mockReq, mockResp, mockFilterChain); assertThat(getField(Fields.REQUEST), is(Defaults.UNKNOWN)); assertThat(getField(Fields.CORRELATION_ID), not(isEmptyOrNullString())); assertThat(getField(Fields.REQUEST_ID), is(Defaults.UNKNOWN)); assertThat(getField(Fields.REMOTE_HOST), is(Defaults.UNKNOWN)); assertThat(getField(Fields.COMPONENT_ID), is(Defaults.UNKNOWN)); assertThat(getField(Fields.CONTAINER_ID), is(Defaults.UNKNOWN)); assertThat(getField(Fields.REQUEST_SIZE_B), is("4")); assertThat(getField(Fields.TENANT_ID), is(Defaults.UNKNOWN)); }
@Test public void testWithActivatedOptionalFields() throws IOException, ServletException { when(mockReq.getRequestURI()).thenReturn(REQUEST); when(mockReq.getQueryString()).thenReturn(QUERY_STRING); when(mockReq.getRemoteHost()).thenReturn(REMOTE_HOST); mockGetHeader(HttpHeaders.X_VCAP_REQUEST_ID, REQUEST_ID); mockGetHeader(HttpHeaders.REFERER, REFERER); FilterChain mockFilterChain = mock(FilterChain.class); LogOptionalFieldsSettings mockOptionalFieldsSettings = mock(LogOptionalFieldsSettings.class); when(mockOptionalFieldsSettings.isLogSensitiveConnectionData()).thenReturn(true); when(mockOptionalFieldsSettings.isLogRemoteUserField()).thenReturn(true); when(mockOptionalFieldsSettings.isLogRefererField()).thenReturn(true); RequestRecordFactory requestRecordFactory = new RequestRecordFactory(mockOptionalFieldsSettings); RequestLoggingFilter requestLoggingFilter = new RequestLoggingFilter(requestRecordFactory); requestLoggingFilter.doFilter(mockReq, mockResp, mockFilterChain); assertThat(getField(Fields.REQUEST), is(FULL_REQUEST)); assertThat(getField(Fields.CORRELATION_ID), is(REQUEST_ID)); assertThat(getField(Fields.REQUEST_ID), is(REQUEST_ID)); assertThat(getField(Fields.REMOTE_HOST), is(REMOTE_HOST)); assertThat(getField(Fields.COMPONENT_ID), is(Defaults.UNKNOWN)); assertThat(getField(Fields.CONTAINER_ID), is(Defaults.UNKNOWN)); assertThat(getField(Fields.REFERER), is(REFERER)); assertThat(getField(Fields.TENANT_ID), is(Defaults.UNKNOWN)); }
@Test public void testWithSuppressedOptionalFields() throws IOException, ServletException { when(mockReq.getRequestURI()).thenReturn(REQUEST); when(mockReq.getQueryString()).thenReturn(QUERY_STRING); when(mockReq.getRemoteHost()).thenReturn(REMOTE_HOST); mockGetHeader(HttpHeaders.X_VCAP_REQUEST_ID, REQUEST_ID); mockGetHeader(HttpHeaders.REFERER, REFERER); FilterChain mockFilterChain = mock(FilterChain.class); LogOptionalFieldsSettings mockLogOptionalFieldsSettings = mock(LogOptionalFieldsSettings.class); when(mockLogOptionalFieldsSettings.isLogSensitiveConnectionData()).thenReturn(false); when(mockLogOptionalFieldsSettings.isLogRemoteUserField()).thenReturn(false); when(mockLogOptionalFieldsSettings.isLogRefererField()).thenReturn(false); RequestRecordFactory requestRecordFactory = new RequestRecordFactory(mockLogOptionalFieldsSettings); RequestLoggingFilter requestLoggingFilter = new RequestLoggingFilter(requestRecordFactory); requestLoggingFilter.doFilter(mockReq, mockResp, mockFilterChain); assertThat(getField(Fields.REQUEST), is(FULL_REQUEST)); assertThat(getField(Fields.CORRELATION_ID), is(REQUEST_ID)); assertThat(getField(Fields.REQUEST_ID), is(REQUEST_ID)); assertThat(getField(Fields.REMOTE_IP), is(Defaults.UNKNOWN)); assertThat(getField(Fields.REMOTE_HOST), is(Defaults.REDACTED)); assertThat(getField(Fields.COMPONENT_ID), is(Defaults.UNKNOWN)); assertThat(getField(Fields.CONTAINER_ID), is(Defaults.UNKNOWN)); assertThat(getField(Fields.TENANT_ID), is(Defaults.UNKNOWN)); }
@Test public void testExplicitCorrelationId() throws IOException, ServletException { mockGetHeader(HttpHeaders.CORRELATION_ID, CORRELATION_ID); mockGetHeader(HttpHeaders.X_VCAP_REQUEST_ID, REQUEST_ID); FilterChain mockFilterChain = mock(FilterChain.class); new RequestLoggingFilter().doFilter(mockReq, mockResp, mockFilterChain); assertThat(getField(Fields.CORRELATION_ID), is(CORRELATION_ID)); assertThat(getField(Fields.CORRELATION_ID), not(REQUEST_ID)); assertThat(getField(Fields.TENANT_ID), is(Defaults.UNKNOWN)); }
@Test public void testExplicitTenantId() throws IOException, ServletException { mockGetHeader(HttpHeaders.TENANT_ID, TENANT_ID); mockGetHeader(HttpHeaders.X_VCAP_REQUEST_ID, REQUEST_ID); FilterChain mockFilterChain = mock(FilterChain.class); new RequestLoggingFilter().doFilter(mockReq, mockResp, mockFilterChain); assertThat(getField(Fields.TENANT_ID), is(TENANT_ID)); }