<commit_before>package examples.performance;

import com.vtence.molecule.WebServer;
import com.vtence.molecule.support.Delorean;
import com.vtence.molecule.testing.http.HttpRequest;
import com.vtence.molecule.testing.http.HttpResponse;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

import static com.vtence.molecule.testing.http.HttpResponseAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.equalTo;

public class CachingAndCompressionTest {
    Delorean delorean = new Delorean();
    CachingAndCompressionExample caching = new CachingAndCompressionExample(delorean);
    WebServer server = WebServer.create(9999);

    HttpRequest request = new HttpRequest(9999);
    HttpResponse response;

    @Before
    public void startServer() throws IOException {
        caching.run(server);
    }

    @After
    public void stopServer() throws IOException {
        server.stop();
    }

    @Test
    public void compressingResponses() throws IOException {
        String contentLengthUnderJvm16 = "170";
        String contentLengthUnderJvm18 = "173";

        response = request.header("Accept-Encoding", "gzip; q=0.9, deflate").get("/");

        assertThat(response).isOK()
                            // We expect deflate compression, which is preferred by the client
                            .hasHeader("Content-Encoding", "deflate")
                            .hasHeader("Content-Length", anyOf(equalTo(contentLengthUnderJvm16), equalTo(contentLengthUnderJvm18)));
    }

    @Test
    public void addingETagValidatorAndCacheDirectivesToDynamicContent() throws IOException {
        response = request.get("/");

        assertThat(response).isOK()
                            .hasContentType("text/html")
                            // We expect an ETag, since no other validation information is generated
                            .hasHeader("ETag")
                            // These are the default cache directives
                            .hasHeader("Cache-Control", "max-age=0; private; no-cache");
    }

    @Test public void
    notGeneratingTheResponseBodyWhenETagHasNotChanged() throws IOException {
        response = request.get("/");

        assertThat(response).isOK()
                            .hasHeader("ETag");

        // Play the same request with freshness information...
        response = request.header("If-None-Match", response.header("ETag")).get("/");
        // ... and expect a not modified
        assertThat(response).hasStatusCode(304);
    }

    @Test
    public void addingLastModifiedValidatorAndCacheDirectivesToStaticFiles() throws IOException {
        response = request.get("/js/fox.js");
<commit_msg>Made the compression test more solid: we make sure the stream is an actual gzip stream using the magic numbers.<commit_after>package examples.performance;

import com.vtence.molecule.WebServer;
import com.vtence.molecule.support.Delorean;
import com.vtence.molecule.testing.http.HttpRequest;
import com.vtence.molecule.testing.http.HttpResponse;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

import static com.vtence.molecule.testing.http.HttpResponseAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.core.StringStartsWith.startsWith;

public class CachingAndCompressionTest {
    String GZIP_HEADER = new String(new char[]{0x1f, 0x8b, 0x08});
    Delorean delorean = new Delorean();
    CachingAndCompressionExample caching = new CachingAndCompressionExample(delorean);
    WebServer server = WebServer.create(9999);

    HttpRequest request = new HttpRequest(9999);
    HttpResponse response;

    @Before
    public void startServer() throws IOException {
        caching.run(server);
    }

    @After
    public void stopServer() throws IOException {
        server.stop();
    }

    @Test
    public void compressingResponses() throws IOException {
        response = request.header("Accept-Encoding", "deflate; q=0.9, gzip").get("/");
        assertThat(response).isOK()
                // We expect gzip compression, which is preferred by the client
                .hasHeader("Content-Encoding", "gzip")
                .hasBodyText(startsWith(GZIP_HEADER));
    }

    @Test
    public void addingETagValidatorAndCacheDirectivesToDynamicContent() throws IOException {
        response = request.get("/");

        assertThat(response).isOK()
                            .hasContentType("text/html")
                            // We expect an ETag, since no other validation information is generated
                            .hasHeader("ETag")
                            // These are the default cache directives
                            .hasHeader("Cache-Control", "max-age=0; private; no-cache");
    }

    @Test public void
    notGeneratingTheResponseBodyWhenETagHasNotChanged() throws IOException {
        response = request.get("/");

        assertThat(response).isOK()
                            .hasHeader("ETag");

        // Play the same request with freshness information...
        response = request.header("If-None-Match", response.header("ETag")).get("/");
        // ... and expect a not modified
        assertThat(response).hasStatusCode(304);
    }

    @Test
    public void addingLastModifiedValidatorAndCacheDirectivesToStaticFiles() throws IOException {
        response = request.get("/js/fox.js");

{'fraction_non_alphanumeric': 0.10086455331412104, 'fraction_numerical': 0.008825648414985591, 'mean_word_length': 2.7293485560779045, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6743662', 'n_tokens_mistral': 1504, 'n_tokens_neox': 1437, 'n_words': 341}