<commit_before>package de.holisticon.util.tracee.servlet;

import de.holisticon.util.tracee.Tracee;
import de.holisticon.util.tracee.TraceeConstants;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.eclipse.jetty.server.DispatcherType;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.EnumSet;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;


/**
 * @author <NAME> (Holisticon AG)
 */
public class TraceeFilterIT {

	private static final int JETTY_PORT = 4204;

	private Server server;
	private FilterHolder traceeFilterHolder;
	private static final String ENDPOINT_URL = "http://localhost:4204/";

	@Before
	public void startJetty() throws Exception {
		server = new Server(JETTY_PORT);
		ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SECURITY);
		context.setContextPath("/");
		context.addServlet(SillyServlet.class, "/*");
		traceeFilterHolder = context.addFilter(TraceeFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));

		server.setHandler(context);

	}

	@After
	public void stopJetty() throws Exception {
		if (server != null)
			server.stop();
	}

	@Test
	public void testCompleteRoundtrip() throws Exception {
		traceeFilterHolder.setInitParameter(TraceeFilter.RESPOND_WITH_CONTEXT_KEY, "true");
		server.start();

		final Header traceeResponseHeader = get(ENDPOINT_URL,"{ \"inClient\":\"yes\" }").getFirstHeader(TraceeConstants.HTTP_HEADER_NAME);

		assertThat(traceeResponseHeader, notNullValue());
		assertThat(traceeResponseHeader.getValue(), containsString("\"inServlet\":\"yes\""));
		assertThat(traceeResponseHeader.getValue(), containsString("\"inClient\":\"yes\""));
	}

	public static final class SillyServlet extends HttpServlet {
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			Assert.assertThat(req.getHeader(TraceeConstants.HTTP_HEADER_NAME), Matchers.containsString("\"inClient\":\"yes\""));
			Tracee.getBackend().put("inServlet", "yes");
		}
	}

	private HttpResponse get(String url, String traceeHeader) throws IOException {
		final HttpClient client = new DefaultHttpClient();
		final HttpGet httpGet = new HttpGet(ENDPOINT_URL);
		if (traceeHeader != null) {
			httpGet.setHeader(TraceeConstants.HTTP_HEADER_NAME, traceeHeader);
		}
		return client.execute(httpGet);<commit_msg>Remove unused method variable<commit_after>package de.holisticon.util.tracee.servlet;

import de.holisticon.util.tracee.Tracee;
import de.holisticon.util.tracee.TraceeConstants;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.eclipse.jetty.server.DispatcherType;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.EnumSet;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;


/**
 * @author <NAME> (Holisticon AG)
 */
public class TraceeFilterIT {

	private static final int JETTY_PORT = 4204;

	private Server server;
	private FilterHolder traceeFilterHolder;
	private static final String ENDPOINT_URL = "http://localhost:4204/";

	@Before
	public void startJetty() throws Exception {
		server = new Server(JETTY_PORT);
		ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SECURITY);
		context.setContextPath("/");
		context.addServlet(SillyServlet.class, "/*");
		traceeFilterHolder = context.addFilter(TraceeFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));

		server.setHandler(context);

	}

	@After
	public void stopJetty() throws Exception {
		if (server != null)
			server.stop();
	}

	@Test
	public void testCompleteRoundtrip() throws Exception {
		traceeFilterHolder.setInitParameter(TraceeFilter.RESPOND_WITH_CONTEXT_KEY, "true");
		server.start();

		final Header traceeResponseHeader = get("{ \"inClient\":\"yes\" }").getFirstHeader(TraceeConstants.HTTP_HEADER_NAME);

		assertThat(traceeResponseHeader, notNullValue());
		assertThat(traceeResponseHeader.getValue(), containsString("\"inServlet\":\"yes\""));
		assertThat(traceeResponseHeader.getValue(), containsString("\"inClient\":\"yes\""));
	}

	public static final class SillyServlet extends HttpServlet {
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			Assert.assertThat(req.getHeader(TraceeConstants.HTTP_HEADER_NAME), Matchers.containsString("\"inClient\":\"yes\""));
			Tracee.getBackend().put("inServlet", "yes");
		}
	}

	private HttpResponse get(String traceeHeader) throws IOException {
		final HttpClient client = new DefaultHttpClient();
		final HttpGet httpGet = new HttpGet(ENDPOINT_URL);
		if (traceeHeader != null) {
			httpGet.setHeader(TraceeConstants.HTTP_HEADER_NAME, traceeHeader);
		}
		return client.execute(httpGet);
{'fraction_non_alphanumeric': 0.11727886674353484, 'fraction_numerical': 0.0026354801515401085, 'mean_word_length': 6.126760563380282, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 5, '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': '4672534', 'n_tokens_mistral': 1898, 'n_tokens_neox': 1799, 'n_words': 352}