@Test public void doFilter_should_not_explode_if_request_and_response_are_HttpServletRequests_and_HttpServletResponses( ) throws IOException, ServletException { // expect getBasicFilter().doFilter( mock(HttpServletRequest.class), mock(HttpServletResponse.class), mock(FilterChain.class) ); // No explosion no problem }
@Test public void doFilter_should_call_doFilterInternal_and_set_ALREADY_FILTERED_ATTRIBUTE_KEY_if_not_already_filtered_and_skipDispatch_returns_false() throws IOException, ServletException { // given: filter that returns false for skipDispatch and request that returns null for already-filtered attribute RequestTracingFilter spyFilter = spy(getBasicFilter()); given(requestMock.getAttribute( RequestTracingFilter.FILTER_HAS_ALREADY_EXECUTED_ATTRIBUTE)).willReturn(null); // when: doFilter() is called spyFilter.doFilter(requestMock, responseMock, filterChainMock); // then: doFilterInternal should be called and ALREADY_FILTERED_ATTRIBUTE_KEY should be set on the request verify(spyFilter).doFilterInternal(requestMock, responseMock, filterChainMock); verify(requestMock).setAttribute(RequestTracingFilter.FILTER_HAS_ALREADY_EXECUTED_ATTRIBUTE, Boolean.TRUE); }
@Test public void doFilter_should_not_call_doFilterInternal_if_already_filtered() throws IOException, ServletException { // given: filter that returns false for skipDispatch but request that returns non-null for already-filtered attribute RequestTracingFilter spyFilter = spy(getBasicFilter()); given(requestMock.getAttribute( RequestTracingFilter.FILTER_HAS_ALREADY_EXECUTED_ATTRIBUTE)).willReturn(Boolean.TRUE); // when: doFilter() is called spyFilter.doFilter(requestMock, responseMock, filterChainMock); // then: doFilterInternal should not be called verify(spyFilter, times(0)).doFilterInternal(requestMock, responseMock, filterChainMock); }
@Test(expected = ServletException.class) public void doFilter_should_explode_if_request_is_not_HttpServletRequest() throws IOException, ServletException { // expect getBasicFilter().doFilter(mock(ServletRequest.class), mock(HttpServletResponse.class), mock(FilterChain.class)); fail("Expected ServletException but no exception was thrown"); }
@Test public void doFilter_should_not_call_doFilterInternal_if_not_already_filtered_but_skipDispatch_returns_true( ) throws IOException, ServletException { // given: request that returns null for already-filtered attribute but filter that returns true for skipDispatch RequestTracingFilter spyFilter = spy(getBasicFilter()); doReturn(true).when(spyFilter).skipDispatch(any(HttpServletRequest.class)); given(requestMock.getAttribute(RequestTracingFilter.FILTER_HAS_ALREADY_EXECUTED_ATTRIBUTE)).willReturn(null); // when: doFilter() is called spyFilter.doFilter(requestMock, responseMock, filterChainMock); // then: doFilterInternal should not be called verify(spyFilter, times(0)).doFilterInternal(requestMock, responseMock, filterChainMock); verify(spyFilter).skipDispatch(requestMock); }
@Test(expected = ServletException.class) public void doFilter_should_explode_if_response_is_not_HttpServletResponse() throws IOException, ServletException { // expect getBasicFilter().doFilter(mock(HttpServletRequest.class), mock(ServletResponse.class), mock(FilterChain.class)); fail("Expected ServletException but no exception was thrown"); }
spyFilter.doFilter(requestMock, responseMock, smartFilterChain);
@Test public void doFilter_should_not_unset_ALREADY_FILTERED_ATTRIBUTE_KEY_after_running_doFilterInternal( ) throws IOException, ServletException { // given: filter that will run doFilterInternal and a FilterChain we can use to verify state when called final RequestTracingFilter spyFilter = spy(getBasicFilter()); given(requestMock.getAttribute( RequestTracingFilter.FILTER_HAS_ALREADY_EXECUTED_ATTRIBUTE)).willReturn(null); final List<Boolean> ifObjectAddedThenSmartFilterChainCalled = new ArrayList<>(); FilterChain smartFilterChain = new FilterChain() { @Override public void doFilter( ServletRequest request, ServletResponse response ) throws IOException, ServletException { // Verify that when the filter chain is called we're in doFilterInternal, and that the request has ALREADY_FILTERED_ATTRIBUTE_KEY set verify(spyFilter).doFilterInternal(requestMock, responseMock, this); verify(requestMock).setAttribute( RequestTracingFilter.FILTER_HAS_ALREADY_EXECUTED_ATTRIBUTE, Boolean.TRUE ); verify(requestMock, times(0)).removeAttribute( RequestTracingFilter.FILTER_HAS_ALREADY_EXECUTED_ATTRIBUTE ); ifObjectAddedThenSmartFilterChainCalled.add(true); } }; // when: doFilter() is called spyFilter.doFilter(requestMock, responseMock, smartFilterChain); // then: smartFilterChain's doFilter should have been called and ALREADY_FILTERED_ATTRIBUTE_KEY should still be set on the request assertThat(ifObjectAddedThenSmartFilterChainCalled).hasSize(1); verify(requestMock, never()).removeAttribute(RequestTracingFilter.FILTER_HAS_ALREADY_EXECUTED_ATTRIBUTE); }