<commit_before>package io.sniffy.servlet;

import io.sniffy.BaseTest;
import io.sniffy.configuration.SniffyConfiguration;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import ru.yandex.qatools.allure.annotations.Features;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import java.io.IOException;

import static io.sniffy.servlet.SniffyFilter.HEADER_NUMBER_OF_QUERIES;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class SniffyFilterConfigurationTest extends BaseTest {

    @Mock
    protected FilterChain filterChain;

    protected MockServletContext servletContext;
    protected MockHttpServletRequest httpServletRequest;
    protected MockHttpServletResponse httpServletResponse;

    @Before
    public void setupServletApiMocks() {
        httpServletResponse = new MockHttpServletResponse();
        servletContext = new MockServletContext("/petclinic");
        httpServletRequest = MockMvcRequestBuilders.get("/petclinic/foo/bar?baz").contextPath("/petclinic").buildRequest(servletContext);
    }


    @Test
    public void testDisabledFilterOneQuery() throws IOException, ServletException {

        doAnswer(invocation -> {executeStatement(); return null;}).
                when(filterChain).doFilter(any(), any());

        SniffyFilter filter = new SniffyFilter();
        filter.setEnabled(false);

        filter.doFilter(httpServletRequest, httpServletResponse, filterChain);

        assertFalse(httpServletResponse.containsHeader(HEADER_NUMBER_OF_QUERIES));

    }

    @Test
    public void testDisabledInConfigFilterOneQuery() throws IOException, ServletException {

        doAnswer(invocation -> {executeStatement(); return null;}).
                when(filterChain).doFilter(any(), any());

        FilterConfig filterConfig = mock(FilterConfig.class);
        when(filterConfig.getInitParameter("enabled")).thenReturn("false");
        when(filterConfig.getServletContext()).thenReturn(servletContext);

        SniffyFilter filter = new SniffyFilter();
        filter.init(filterConfig);

        filter.doFilter(httpServletRequest, httpServletResponse, filterChain);

        assertFalse(httpServletResponse.containsHeader(HEADER_NUMBER_OF_QUERIES));

    }

    @Test
    @Features("issues/288")
    public void testSystemInConfigFilterDisabledGloballyOneQuery() throws IOException, ServletException {

        doAnswer(invocation -> {executeStatement(); return null;}).
                when(filterChain).doFilter(any(), any());

        FilterConfig filterConfig = mock(FilterConfig.class);
        when(filterConfig.getInitParameter("enabled")).thenReturn("system");
        when(filterConfig.getServletContext()).thenReturn(servletContext);

        SniffyFilter filter = new SniffyFilter();
        filter.init(filterConfig);

        filter.doFilter(httpServletRequest, httpServletResponse, filterChain);

        assertFalse(httpServletResponse.containsHeader(HEADER_NUMBER_OF_QUERIES));

    }

    @Test
    @Features("issues/288")
    public void testSystemInConfigFilterEnabledGloballyOneQuery() throws IOException, ServletException {

        Boolean filterEnabledBackup = SniffyConfiguration.INSTANCE.getFilterEnabled();
        try {
            SniffyConfiguration.INSTANCE.setFilterEnabled(true);

            doAnswer(invocation -> {
                executeStatement();
                return null;
            }).
                    when(filterChain).doFilter(any(), any());

            FilterConfig filterConfig = mock(FilterConfig.class);
            when(filterConfig.getInitParameter("enabled")).thenReturn("system");
            when(filterConfig.getServletContext()).thenReturn(servletContext);

            SniffyFilter filter = new SniffyFilter();
            filter.init(filterConfig);

            filter.doFilter(httpServletRequest, httpServletResponse, filterChain);

            assertTrue(httpServletResponse.containsHeader(HEADER_NUMBER_OF_QUERIES));
        } finally {
            SniffyConfiguration.INSTANCE.setFilterEnabled(filterEnabledBackup);
        }

    }

    @Test
    public void testFilterEnabledByRequestParameter() throws IOException, ServletException {
        doAnswer(invocation -> {executeStatement(); return null;}).
                when(filterChain).doFilter(any(), any());
        SniffyFilter filter = new SniffyFilter();
        filter.setEnabled(false);
        httpServletRequest = MockMvcRequestBuilders.get("/petclinic/foo/bar?baz&sniffy=true").contextPath("/petclinic").buildRequest(servletContext);
        filter.doFilter(httpServletRequest, httpServletResponse, filterChain);
        assertTrue(httpServletResponse.containsHeader(HEADER_NUMBER_OF_QUERIES));
        assertEquals("Check cookie parameter specified", "true", httpServletResponse.getCookie("sniffy").getValue());
    }

    @Test
    public void testFilterNoCookies() throws IOException, ServletException {
        doAnswer(invocation -> {executeStatement(); return null;}).
                when(filterChain).doFilter(any(), any());
        SniffyFilter filter = new SniffyFilter();
        filter.setEnabled(false);
        httpServletRequest.setCookies((Cookie[]) null);
        filter.doFilter(httpServletRequest, httpServletResponse, filterChain);
        assertFalse(httpServletResponse.containsHeader(HEADER_NUMBER_OF_QUERIES));
    }

    @Test
    public void testFilterEnabledByCookie() throws IOException, ServletException {
        doAnswer(invocation -> {executeStatement(); return null;}).
                when(filterChain).doFilter(any(), any());
        SniffyFilter filter = new SniffyFilter();
        filter.setEnabled(false);
        httpServletRequest.setCookies(new Cookie("sniffy", "true"));
        filter.doFilter(httpServletRequest, httpServletResponse, filterChain);
        assertTrue(httpServletResponse.containsHeader(HEADER_NUMBER_OF_QUERIES));
    }

    @Test
    public void testFilterEnabledByHeader() throws IOException, ServletException {
        doAnswer(invocation -> {executeStatement(); return null;}).
                when(filterChain).doFilter(any(), any());
        SniffyFilter filter = new SniffyFilter();
        filter.setEnabled(false);
        httpServletRequest.addHeader("Sniffy-Enabled", "true");
        filter.doFilter(httpServletRequest, httpServletResponse, filterChain);
        assertTrue(httpServletResponse.containsHeader(HEADER_NUMBER_OF_QUERIES));
    }<commit_msg>Added unit test<commit_after>package io.sniffy.servlet;

import io.sniffy.BaseTest;
import io.sniffy.configuration.SniffyConfiguration;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import ru.yandex.qatools.allure.annotations.Features;
import ru.yandex.qatools.allure.annotations.Issue;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import static io.sniffy.servlet.SniffyFilter.HEADER_NUMBER_OF_QUERIES;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class SniffyFilterConfigurationTest extends BaseTest {

    @Mock
    protected FilterChain filterChain;

    protected MockServletContext servletContext;
    protected MockHttpServletRequest httpServletRequest;
    protected MockHttpServletResponse httpServletResponse;

    @Before
    public void setupServletApiMocks() {
        httpServletResponse = new MockHttpServletResponse();
        servletContext = new MockServletContext("/petclinic");
        httpServletRequest = MockMvcRequestBuilders.get("/petclinic/foo/bar?baz").contextPath("/petclinic").buildRequest(servletContext);
    }


    @Test
    public void testDisabledFilterOneQuery() throws IOException, ServletException {

        doAnswer(invocation -> {executeStatement(); return null;}).
                when(filterChain).doFilter(any(), any());

        SniffyFilter filter = new SniffyFilter();
        filter.setEnabled(false);

        filter.doFilter(httpServletRequest, httpServletResponse, filterChain);

        assertFalse(httpServletResponse.containsHeader(HEADER_NUMBER_OF_QUERIES));

    }

    @Test
    public void testDisabledInConfigFilterOneQuery() throws IOException, ServletException {

        doAnswer(invocation -> {executeStatement(); return null;}).
                when(filterChain).doFilter(any(), any());

        FilterConfig filterConfig = mock(FilterConfig.class);
        when(filterConfig.getInitParameter("enabled")).thenReturn("false");
        when(filterConfig.getServletContext()).thenReturn(servletContext);

        SniffyFilter filter = new SniffyFilter();
        filter.init(filterConfig);

        filter.doFilter(httpServletRequest, httpServletResponse, filterChain);

        assertFalse(httpServletResponse.containsHeader(HEADER_NUMBER_OF_QUERIES));

    }

    @Test
    @Features("issues/288")
    public void testSystemInConfigFilterDisabledGloballyOneQuery() throws IOException, ServletException {

        doAnswer(invocation -> {executeStatement(); return null;}).
                when(filterChain).doFilter(any(), any());

        FilterConfig filterConfig = mock(FilterConfig.class);
        when(filterConfig.getInitParameter("enabled")).thenReturn("system");
        when(filterConfig.getServletContext()).thenReturn(servletContext);

        SniffyFilter filter = new SniffyFilter();
        filter.init(filterConfig);

        filter.doFilter(httpServletRequest, httpServletResponse, filterChain);

        assertFalse(httpServletResponse.containsHeader(HEADER_NUMBER_OF_QUERIES));

    }

    @Test
    @Features("issues/288")
    public void testSystemInConfigFilterEnabledGloballyOneQuery() throws IOException, ServletException {

        Boolean filterEnabledBackup = SniffyConfiguration.INSTANCE.getFilterEnabled();
        try {
            SniffyConfiguration.INSTANCE.setFilterEnabled(true);

            doAnswer(invocation -> {
                executeStatement();
                return null;
            }).
                    when(filterChain).doFilter(any(), any());

            FilterConfig filterConfig = mock(FilterConfig.class);
            when(filterConfig.getInitParameter("enabled")).thenReturn("system");
            when(filterConfig.getServletContext()).thenReturn(servletContext);

            SniffyFilter filter = new SniffyFilter();
            filter.init(filterConfig);

            filter.doFilter(httpServletRequest, httpServletResponse, filterChain);

            assertTrue(httpServletResponse.containsHeader(HEADER_NUMBER_OF_QUERIES));
        } finally {
            SniffyConfiguration.INSTANCE.setFilterEnabled(filterEnabledBackup);
        }

    }

    @Test
    public void testFilterEnabledByRequestParameter() throws IOException, ServletException {
        doAnswer(invocation -> {executeStatement(); return null;}).
                when(filterChain).doFilter(any(), any());
        SniffyFilter filter = new SniffyFilter();
        filter.setEnabled(false);
        httpServletRequest = MockMvcRequestBuilders.get("/petclinic/foo/bar?baz&sniffy=true").contextPath("/petclinic").buildRequest(servletContext);
        filter.doFilter(httpServletRequest, httpServletResponse, filterChain);
        assertTrue(httpServletResponse.containsHeader(HEADER_NUMBER_OF_QUERIES));
        assertEquals("Check cookie parameter specified", "true", httpServletResponse.getCookie("sniffy").getValue());
    }

    @Test
    @Issue("issues/295")
    public void testFilterGetParameterIsNotCalled() throws IOException, ServletException {
        doAnswer(invocation -> {executeStatement(); return null;}).
                when(filterChain).doFilter(any(), any());
        SniffyFilter filter = new SniffyFilter();
        filter.setEnabled(false);
        httpServletRequest = MockMvcRequestBuilders.get("/petclinic/foo/bar?baz&sniffy=true").contextPath("/petclinic").buildRequest(servletContext);

        AtomicBoolean parametersAccessed = new AtomicBoolean(false);

        HttpServletRequest request = new HttpServletRequestWrapper(httpServletRequest) {

            @Override
            public String getParameter(String name) {
                parametersAccessed.set(true);
                return super.getParameter(name);
            }

            @Override
            public Map<String, String[]> getParameterMap() {
                parametersAccessed.set(true);
                return super.getParameterMap();
            }

            @Override
            public Enumeration<String> getParameterNames() {
                parametersAccessed.set(true);
                return super.getParameterNames();
            }

            @Override
            public String[] getParameterValues(String name) {
                parametersAccessed.set(true);
                return super.getParameterValues(name);
            }
        };

        filter.doFilter(request, httpServletResponse, filterChain);
        assertTrue(httpServletResponse.containsHeader(HEADER_NUMBER_OF_QUERIES));
        assertFalse(parametersAccessed.get());
    }

    @Test
    public void testFilterNoCookies() throws IOException, ServletException {
        doAnswer(invocation -> {executeStatement(); return null;}).
                when(filterChain).doFilter(any(), any());
        SniffyFilter filter = new SniffyFilter();
        filter.setEnabled(false);
        httpServletRequest.setCookies((Cookie[]) null);
        filter.doFilter(httpServletRequest, httpServletResponse, filterChain);
        assertFalse(httpServletResponse.containsHeader(HEADER_NUMBER_OF_QUERIES));
    }

    @Test
    public void testFilterEnabledByCookie() throws IOException, ServletException {
        doAnswer(invocation -> {executeStatement(); return null;}).
                when(filterChain).doFilter(any(), any());
        SniffyFilter filter = new SniffyFilter();
        filter.setEnabled(false);
        httpServletRequest.setCookies(new Cookie("sniffy", "true"));
        filter.doFilter(httpServletRequest, httpServletResponse, filterChain);
        assertTrue(httpServletResponse.containsHeader(HEADER_NUMBER_OF_QUERIES));
    }

    @Test
    public void testFilterEnabledByHeader() throws IOException, ServletException {
        doAnswer(invocation -> {executeStatement(); return null;}).
                when(filterChain).doFilter(any(), any());
        SniffyFilter filter = new SniffyFilter();
        filter.setEnabled(false);
        httpServletRequest.addHeader("Sniffy-Enabled", "true");
        filter.doFilter(httpServletRequest, httpServletResponse, filterChain);
        assertTrue(httpServletResponse.containsHeader(HEADER_NUMBER_OF_QUERIES));
    }
{'fraction_non_alphanumeric': 0.1004051658647759, 'fraction_numerical': 0.0009496074955684983, 'mean_word_length': 3.7509774436090226, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2656440', 'n_tokens_mistral': 4087, 'n_tokens_neox': 3957, 'n_words': 667}