<commit_before>/*
 * Copyright (C) 2011 <NAME>
 *
 * 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 com.github.tomakehurst.wiremock;

import com.github.tomakehurst.wiremock.core.Options;
import com.github.tomakehurst.wiremock.http.HttpClientFactory;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.Assert.assertThat;

public class ResponseDribbleAcceptanceTest {

    private static final int SOCKET_TIMEOUT_MILLISECONDS = 500;
    private static final int DOUBLE_THE_SOCKET_TIMEOUT = SOCKET_TIMEOUT_MILLISECONDS * 2;

    private static final byte[] BODY_OF_THREE_BYTES = new byte[] {1, 2, 3};

    @Rule
    public WireMockRule wireMockRule = new WireMockRule(Options.DYNAMIC_PORT, Options.DYNAMIC_PORT);

    private HttpClient httpClient;

    @Before
    public void init() {
        httpClient = HttpClientFactory.createClient(SOCKET_TIMEOUT_MILLISECONDS);
    }

    @Test
    public void requestIsSuccessfulButTakesLongerThanSocketTimeoutWhenDribbleIsEnabled() throws Exception {

        stubFor(get(urlEqualTo("/delayedDribble")).willReturn(
                aResponse()
                        .withStatus(200)
                        .withBody(BODY_OF_THREE_BYTES)
                        .withChunkedDribbleDelay(BODY_OF_THREE_BYTES.length, DOUBLE_THE_SOCKET_TIMEOUT)));

        HttpResponse response = httpClient.execute(new HttpGet(String.format("http://localhost:%d/delayedDribble", wireMockRule.port())));

        assertThat(response.getStatusLine().getStatusCode(), is(200));

        long start = System.currentTimeMillis();
        byte[] responseBody = IOUtils.toByteArray(response.getEntity().getContent());
        int duration = (int) (System.currentTimeMillis() - start);

        assertThat(BODY_OF_THREE_BYTES, is(responseBody));
        assertThat(duration, greaterThanOrEqualTo(SOCKET_TIMEOUT_MILLISECONDS));
    }

    @Test
    public void requestIsSuccessfulAndBelowSocketTimeoutWhenDribbleIsDisabled() throws Exception {
        stubFor(get(urlEqualTo("/nonDelayedDribble")).willReturn(
                aResponse()
                        .withStatus(200)
                        .withBody(BODY_OF_THREE_BYTES)));

        HttpResponse response = httpClient.execute(new HttpGet(String.format("http://localhost:%d/nonDelayedDribble", wireMockRule.port())));

        assertThat(response.getStatusLine().getStatusCode(), is(200));

        long start = System.currentTimeMillis();
        byte[] responseBody = IOUtils.toByteArray(response.getEntity().getContent());
        int duration = (int) (System.currentTimeMillis() - start);

        assertThat(BODY_OF_THREE_BYTES, is(responseBody));
        assertThat(duration, lessThan(SOCKET_TIMEOUT_MILLISECONDS));
    }
}<commit_msg>Added a test showing a failure case for the byte dribbling feature<commit_after>/*
 * Copyright (C) 2011 <NAME>
 *
 * 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 com.github.tomakehurst.wiremock;

import com.github.tomakehurst.wiremock.core.Options;
import com.github.tomakehurst.wiremock.http.HttpClientFactory;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.Assert.assertThat;

public class ResponseDribbleAcceptanceTest {

    private static final int SOCKET_TIMEOUT_MILLISECONDS = 500;
    private static final int DOUBLE_THE_SOCKET_TIMEOUT = SOCKET_TIMEOUT_MILLISECONDS * 2;

    private static final byte[] BODY_BYTES = "the long sentence being sent".getBytes();

    @Rule
//    public WireMockRule wireMockRule = new WireMockRule(Options.DYNAMIC_PORT, Options.DYNAMIC_PORT);
    public WireMockRule wireMockRule = new WireMockRule(8989, Options.DYNAMIC_PORT);

    private HttpClient httpClient;

    @Before
    public void init() {
        httpClient = HttpClientFactory.createClient(SOCKET_TIMEOUT_MILLISECONDS);
    }

    @Test
    public void requestIsSuccessfulButTakesLongerThanSocketTimeoutWhenDribbleIsEnabled() throws Exception {
        stubFor(get("/delayedDribble").willReturn(
                ok()
                    .withBody(BODY_BYTES)
                    .withChunkedDribbleDelay(BODY_BYTES.length, DOUBLE_THE_SOCKET_TIMEOUT)));

        HttpResponse response = httpClient.execute(new HttpGet(String.format("http://localhost:%d/delayedDribble", wireMockRule.port())));

        assertThat(response.getStatusLine().getStatusCode(), is(200));

        long start = System.currentTimeMillis();
        byte[] responseBody = IOUtils.toByteArray(response.getEntity().getContent());
        int duration = (int) (System.currentTimeMillis() - start);

        assertThat(responseBody, is(BODY_BYTES));
        assertThat(duration, greaterThanOrEqualTo(SOCKET_TIMEOUT_MILLISECONDS));
    }

    @Test
    public void stringBody() throws Exception {
        stubFor(get("/delayedDribble").willReturn(
            ok()
                .withBody("Send this in many pieces please!!!")
                .withChunkedDribbleDelay(2, DOUBLE_THE_SOCKET_TIMEOUT)));

        HttpResponse response = httpClient.execute(new HttpGet(String.format("http://localhost:%d/delayedDribble", wireMockRule.port())));

        assertThat(response.getStatusLine().getStatusCode(), is(200));

        long start = System.currentTimeMillis();
        String responseBody = EntityUtils.toString(response.getEntity());
        int duration = (int) (System.currentTimeMillis() - start);

        assertThat(responseBody, is("Send this in many pieces please!!!"));
        assertThat(duration, greaterThanOrEqualTo(SOCKET_TIMEOUT_MILLISECONDS));
    }

    @Test
    public void requestIsSuccessfulAndBelowSocketTimeoutWhenDribbleIsDisabled() throws Exception {
        stubFor(get("/nonDelayedDribble").willReturn(
                ok()
                    .withBody(BODY_BYTES)));

        HttpResponse response = httpClient.execute(new HttpGet(String.format("http://localhost:%d/nonDelayedDribble", wireMockRule.port())));

        assertThat(response.getStatusLine().getStatusCode(), is(200));

        long start = System.currentTimeMillis();
        byte[] responseBody = IOUtils.toByteArray(response.getEntity().getContent());
        int duration = (int) (System.currentTimeMillis() - start);

        assertThat(BODY_BYTES, is(responseBody));
        assertThat(duration, lessThan(SOCKET_TIMEOUT_MILLISECONDS));
    }
}
{'fraction_non_alphanumeric': 0.10168242662409431, 'fraction_numerical': 0.006508657742846617, 'mean_word_length': 3.8883553421368546, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '379436', 'n_tokens_mistral': 2376, 'n_tokens_neox': 2328, 'n_words': 510}