<commit_before>/*
 * Copyright (c) 2014 the original author or authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.werval.runtime.http;

import io.werval.api.outcomes.Outcome;
import io.werval.runtime.routes.RoutesParserProvider;
import io.werval.test.WervalHttpRule;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.ClassRule;
import org.junit.Test;

import static com.jayway.restassured.RestAssured.expect;
import static com.jayway.restassured.RestAssured.given;
import static io.werval.api.context.CurrentContext.outcomes;
import static io.werval.api.http.Headers.Names.CONNECTION;
import static io.werval.api.http.Headers.Values.CLOSE;
import static io.werval.api.http.Headers.Values.KEEP_ALIVE;
import static java.util.Locale.US;
import static org.apache.http.params.CoreProtocolPNames.PROTOCOL_VERSION;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

/**
 * Assert correct Kepp-Alive behaviour.
 */
public class KeepAliveTest
{
    public static class Controller
    {
        public Outcome ok()
        {
            return outcomes().ok().build();
        }

        public Outcome clientError()
        {
            return outcomes().badRequest().build();
        }

        public Outcome serverError()
        {
            return outcomes().internalServerError().build();
        }

        public Outcome unknown()
        {
            return outcomes().status( 666 ).build();
        }
    }

    @ClassRule
    public static final WervalHttpRule WERVAL = new WervalHttpRule( new RoutesParserProvider(
        "\n"
        + "GET /ok io.werval.runtime.http.KeepAliveTest$Controller.ok\n"
        + "GET /clientError io.werval.runtime.http.KeepAliveTest$Controller.clientError\n"
        + "GET /serverError io.werval.runtime.http.KeepAliveTest$Controller.serverError\n"
        + "GET /unknown io.werval.runtime.http.KeepAliveTest$Controller.unknown\n"
    ) );

    @Test
    public void http11_keepalive()
    {
        expect().
            statusCode( 200 ).
            header( CONNECTION, KEEP_ALIVE ).
            when().get( "/ok" );
        expect().
            statusCode( 400 ).
            header( CONNECTION, CLOSE ).
            when().get( "/clientError" );
        expect().
            statusCode( 500 ).
            header( CONNECTION, CLOSE ).
            when().get( "/serverError" );
        expect().
            statusCode( 666 ).
            header( CONNECTION, CLOSE ).
            when().get( "/unknown" );
    }

    @Test
    public void http11_close()
    {
        given().
            header( CONNECTION, CLOSE ).
            expect().
            statusCode( 200 ).
            header( CONNECTION, CLOSE ).
            when().get( "/ok" );
        given().
            header( CONNECTION, KEEP_ALIVE ).
            expect().
            statusCode( 400 ).
            header( CONNECTION, CLOSE ).
            when().get( "/clientError" );
        given().
            header( CONNECTION, KEEP_ALIVE ).
            expect().
            statusCode( 500 ).
            header( CONNECTION, CLOSE ).
            when().get( "/serverError" );
        given().
            header( CONNECTION, KEEP_ALIVE ).
            expect().
            statusCode( 666 ).
            header( CONNECTION, CLOSE ).
            when().get( "/unknown" );
    }

    @Test
    public void http10_default()
        throws IOException
    {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet( WERVAL.baseHttpUrl() + "/ok" );
        get.getParams().setParameter( PROTOCOL_VERSION, new ProtocolVersion( "HTTP", 1, 0 ) );
        // Apache HttpClient is a mess, without this it force Keep-Alive on HTTP/1.0 requests ...
        get.setHeader( CONNECTION, "NOT PRESENT" );
        HttpResponse response = client.execute( get );
        assertThat( response.getProtocolVersion().getMinor(), is( 0 ) );
        assertThat( response.getFirstHeader( CONNECTION ).getValue().toLowerCase( US ), equalTo( CLOSE ) );
    }

    @Test
    public void http10_close()
        throws IOException
    {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet( WERVAL.baseHttpUrl() + "/ok" );
        get.getParams().setParameter( PROTOCOL_VERSION, new ProtocolVersion( "HTTP", 1, 0 ) );
        get.setHeader( CONNECTION, CLOSE );
        HttpResponse response = client.execute( get );
        assertThat( response.getProtocolVersion().getMinor(), is( 0 ) );
        assertThat( response.getFirstHeader( CONNECTION ).getValue().toLowerCase( US ), equalTo( CLOSE ) );
    }

    @Test
    public void http10_keepalive()
        throws IOException
    {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet( WERVAL.baseHttpUrl() + "/ok" );
        get.getParams().setParameter( PROTOCOL_VERSION, new ProtocolVersion( "HTTP", 1, 0 ) );
        get.setHeader( CONNECTION, KEEP_ALIVE );
        HttpResponse response = client.execute( get );
        assertThat( response.getProtocolVersion().getMinor(), is( 0 ) );
        assertThat( response.getFirstHeader( CONNECTION ).getValue().toLowerCase( US ), equalTo( KEEP_ALIVE ) );
    }
}<commit_msg>Runtime, minor, fix KeepAliveTest<commit_after>/*
 * Copyright (c) 2014 the original author or authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.werval.runtime.http;

import io.werval.api.outcomes.Outcome;
import io.werval.runtime.routes.RoutesParserProvider;
import io.werval.test.WervalHttpRule;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.ClassRule;
import org.junit.Test;

import static com.jayway.restassured.RestAssured.given;
import static com.jayway.restassured.RestAssured.when;
import static io.werval.api.context.CurrentContext.outcomes;
import static io.werval.api.http.Headers.Names.CONNECTION;
import static io.werval.api.http.Headers.Values.CLOSE;
import static io.werval.api.http.Headers.Values.KEEP_ALIVE;
import static java.util.Locale.US;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

/**
 * Assert correct Kepp-Alive behaviour.
 */
public class KeepAliveTest
{
    public static class Controller
    {
        public Outcome ok()
        {
            return outcomes().ok().build();
        }

        public Outcome clientError()
        {
            return outcomes().badRequest().build();
        }

        public Outcome serverError()
        {
            return outcomes().internalServerError().build();
        }
    }

    @ClassRule
    public static final WervalHttpRule WERVAL = new WervalHttpRule( new RoutesParserProvider(
        "\n"
        + "GET /ok io.werval.runtime.http.KeepAliveTest$Controller.ok\n"
        + "GET /clientError io.werval.runtime.http.KeepAliveTest$Controller.clientError\n"
        + "GET /serverError io.werval.runtime.http.KeepAliveTest$Controller.serverError\n"
    ) );

    @Test
    public void http11_keepalive()
    {
        when().get( "/ok" )
            .then().statusCode( 200 )
            .and().header( CONNECTION, KEEP_ALIVE );
        when().get( "/clientError" )
            .then().statusCode( 400 )
            .and().header( CONNECTION, CLOSE );
        when().get( "/serverError" )
            .then().statusCode( 500 )
            .and().header( CONNECTION, CLOSE );
    }

    @Test
    public void http11_close()
    {
        given().header( CONNECTION, CLOSE )
            .when().get( "/ok" )
            .then().statusCode( 200 )
            .and().header( CONNECTION, CLOSE );
        given().header( CONNECTION, KEEP_ALIVE )
            .when().get( "/clientError" )
            .then().statusCode( 400 )
            .and().header( CONNECTION, CLOSE );
        given().header( CONNECTION, KEEP_ALIVE )
            .when().get( "/serverError" )
            .then().statusCode( 500 )
            .and().header( CONNECTION, CLOSE );
    }

    @Test
    public void http10_default()
        throws IOException
    {
        try( CloseableHttpClient client = HttpClientBuilder.create().build() )
        {
            HttpGet get = new HttpGet( WERVAL.baseHttpUrl() + "/ok" );
            get.setProtocolVersion( new ProtocolVersion( "HTTP", 1, 0 ) );
            // Apache HttpClient is a mess, without this it force Keep-Alive on HTTP/1.0 requests ...
            get.setHeader( CONNECTION, "NOT PRESENT" );
            HttpResponse response = client.execute( get );
            assertThat( response.getProtocolVersion().getMinor(), is( 0 ) );
            assertThat( response.getFirstHeader( CONNECTION ).getValue().toLowerCase( US ), equalTo( CLOSE ) );
        }
    }

    @Test
    public void http10_close()
        throws IOException
    {
        try( CloseableHttpClient client = HttpClientBuilder.create().build() )
        {
            HttpGet get = new HttpGet( WERVAL.baseHttpUrl() + "/ok" );
            get.setProtocolVersion( new ProtocolVersion( "HTTP", 1, 0 ) );
            get.setHeader( CONNECTION, CLOSE );
            HttpResponse response = client.execute( get );
            assertThat( response.getProtocolVersion().getMinor(), is( 0 ) );
            assertThat( response.getFirstHeader( CONNECTION ).getValue().toLowerCase( US ), equalTo( CLOSE ) );
        }
    }

    @Test
    public void http10_keepalive()
        throws IOException
    {
        try( CloseableHttpClient client = HttpClientBuilder.create().build() )
        {
            HttpGet get = new HttpGet( WERVAL.baseHttpUrl() + "/ok" );
            get.setProtocolVersion( new ProtocolVersion( "HTTP", 1, 0 ) );
            get.setHeader( CONNECTION, KEEP_ALIVE );
            HttpResponse response = client.execute( get );
            assertThat( response.getProtocolVersion().getMinor(), is( 0 ) );
            assertThat( response.getFirstHeader( CONNECTION ).getValue().toLowerCase( US ), equalTo( KEEP_ALIVE ) );
        }
    }
}
{'fraction_non_alphanumeric': 0.11639100376301742, 'fraction_numerical': 0.00901373938916601, 'mean_word_length': 2.4830844254800364, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2773567', 'n_tokens_mistral': 3356, 'n_tokens_neox': 3232, 'n_words': 806}