<commit_before>package com.atexpose;

import com.atexpose.dispatcher.PropertiesDispatcher;
import io.schinzel.basicutils.FunnyChars;
import io.schinzel.basicutils.Sandman;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.Socket;
import java.nio.charset.Charset;

import static org.junit.Assert.assertEquals;

/**
 * @author Schinzel
 */
public class WebServerTest {
    AtExpose mAtExpose;
    private static final String LOCAL_HOST_IP = "127.0.0.1";
    private static final String URL = "http://" + LOCAL_HOST_IP + ":5555/call/";


    @Before
    public void before() {
        Sandman.snoozeMillis(10);
        mAtExpose = AtExpose.create();
        mAtExpose.getWebServerBuilder()
                .numberOfThreads(5)
                .startWebServer();
    }


    @After
    public void after() {
        mAtExpose.shutdown();
        //Snooze required to get tests to work on Travis
        Sandman.snoozeMillis(10);
    }


    @Test
    public void test_WebServer_ping() throws IOException {
        Connection.Response response = Jsoup
                .connect(URL + "ping")
                .method(Connection.Method.GET)
                .execute();
        String result = response.body();
        assertEquals("pong", result);
    }


    @Test
    public void test_WebServer_noHeader() throws IOException {
        int port = 5555;
        Socket socket = new Socket(LOCAL_HOST_IP, port);
        String request = "no header call";
        byte[] baMessage = request.getBytes(Charset.forName("Utf-8"));
        SocketRWUtil.write(socket, baMessage);
        String expected = "HTTP/1.1 500 Internal Server Error\r\n"
                + "Server: " + PropertiesDispatcher.RESP_HEADER_SERVER_NAME + "\r\n"
                + "Content-Type: application/json; charset=UTF-8\r\n"
                + "Cache-Control: max-age=0\r\n"
                + "Content-Length: 132\r\n"
                + "\r\n"
                + "{\"message\":\"Error while reading from socket. Request not allowed. Request has to start with GET or POST. Request:' " + request + "'\"}\n\n";
        String response = SocketRWUtil.read(socket);
        assertEquals(expected, response);
    }


    @Test
    public void test_WebServer_ghostCall() throws IOException {
        int port = 5555;
        Socket socket = new Socket(LOCAL_HOST_IP, port);
        byte[] baMessage = new byte[]{0};
        SocketRWUtil.write(socket, baMessage);
        String expected = "HTTP/1.1 200 OK\r\n"
                + "Server: " + PropertiesDispatcher.RESP_HEADER_SERVER_NAME + "\r\n"
                + "Content-Type: text/html; charset=UTF-8\r\n"<commit_msg>Bug fix<commit_after>package com.atexpose;

import com.atexpose.dispatcher.PropertiesDispatcher;
import io.schinzel.basicutils.FunnyChars;
import io.schinzel.basicutils.Sandman;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.Socket;
import java.nio.charset.Charset;

import static org.junit.Assert.assertEquals;

/**
 * @author Schinzel
 */
public class WebServerTest {
    AtExpose mAtExpose;
    private static final String LOCAL_HOST_IP = "127.0.0.1";
    private static final String URL = "http://" + LOCAL_HOST_IP + ":5555/call/";


    @Before
    public void before() {
        Sandman.snoozeMillis(10);
        mAtExpose = AtExpose.create();
        mAtExpose.getWebServerBuilder()
                .numberOfThreads(5)
                .startWebServer();
    }


    @After
    public void after() {
        mAtExpose.shutdown();
        //Snooze required to get tests to work on Travis
        Sandman.snoozeMillis(10);
    }


    @Test
    public void test_WebServer_ping() throws IOException {
        Connection.Response response = Jsoup
                .connect(URL + "ping")
                .method(Connection.Method.GET)
                .execute();
        String result = response.body();
        assertEquals("pong", result);
    }


    @Test
    public void test_WebServer_noHeader() throws IOException {
        int port = 5555;
        Socket socket = new Socket(LOCAL_HOST_IP, port);
        String request = "no header call";
        byte[] baMessage = request.getBytes(Charset.forName("Utf-8"));
        SocketRWUtil.write(socket, baMessage);
        String expected = "HTTP/1.1 500 Internal Server Error\r\n"
                + "Server: " + PropertiesDispatcher.RESP_HEADER_SERVER_NAME + "\r\n"
                + "Content-Type: application/json; charset=UTF-8\r\n"
                + "Cache-Control: max-age=0\r\n"
                + "Content-Length: 138\r\n"
                + "\r\n"
                + "{\"error_message\":\"Error while reading from socket. Request not allowed. Request has to start with GET or POST. Request:' " + request + "'\"}\n\n";
        String response = SocketRWUtil.read(socket);
        assertEquals(expected, response);
    }


    @Test
    public void test_WebServer_ghostCall() throws IOException {
        int port = 5555;
        Socket socket = new Socket(LOCAL_HOST_IP, port);
        byte[] baMessage = new byte[]{0};
        SocketRWUtil.write(socket, baMessage);
        String expected = "HTTP/1.1 200 OK\r\n"
                + "Server: " + PropertiesDispatcher.RESP_HEADER_SERVER_NAME + "\r\n"
                + "Content-Type: text/html; charset=UTF-8\r\n"
{'fraction_non_alphanumeric': 0.11711054359722729, 'fraction_numerical': 0.014958044509303174, 'mean_word_length': 2.3971499380421313, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '820605', 'n_tokens_mistral': 1749, 'n_tokens_neox': 1633, 'n_words': 376}