<commit_before>package com.vtence.molecule.testing.http;

import org.hamcrest.Matcher;
import org.junit.Assert;

import java.net.HttpCookie;
import java.util.List;

import static com.vtence.molecule.testing.CharsetDetector.detectCharsetOf;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.equalToIgnoringCase;

public class HttpResponseAssert {
    private final HttpResponse response;

    public HttpResponseAssert(HttpResponse response) {
        this.response = response;
    }

    public static HttpResponseAssert assertThat(HttpResponse response) {
        return new HttpResponseAssert(response);
    }

    public HttpResponseAssert isOK() {
        return hasStatusCode(200);
    }

    public HttpResponseAssert hasStatusCode(int code) {
        return hasStatusCode(is(code));
    }

    private HttpResponseAssert hasStatusCode(Matcher<? super Integer> matching) {
        Assert.assertThat("response status code", response.statusCode(), matching);
        return this;
    }

    public HttpResponseAssert hasStatusMessage(String message) {
        return hasStatusMessage(is(message));
    }

    public HttpResponseAssert hasStatusMessage(Matcher<? super String> matching) {
        Assert.assertThat("response status message", response.statusMessage(), matching);
        return this;
    }

    public HttpResponseAssert hasHeader(String named) {
        return hasHeader(named, notNullValue());
    }

    public HttpResponseAssert hasNoHeader(String named) {
        return hasHeader(named, nullValue());
    }

    public HttpResponseAssert hasHeader(String name, String value) {
        return hasHeader(name, equalTo(value));
    }

    public HttpResponseAssert hasHeader(String name, Matcher<? super String> matching) {
        Assert.assertThat("response '" + name + "' header", response.header(name), matching);
        return this;
    }

    public HttpResponseAssert hasHeaders(String name, Matcher<? super List<String>> matching) {
        Assert.assertThat("response '" + name + "' headers", response.headers(name), matching);
        return this;
    }

    public HttpResponseAssert isChunked() {
        return hasHeader("Transfer-Encoding", "chunked");
    }

    public HttpResponseAssert isNotChunked() {
        return hasHeader("Transfer-Encoding", not("chunked"));
    }

    public HttpResponseAssert hasBodyText(String text) {
        return hasBodyText(equalTo(text));
    }

    public HttpResponseAssert hasBodyText(Matcher<? super String> matching) {
        Assert.assertThat("response body text", response.bodyText(), matching);
        return this;
    }

    public HttpResponseAssert hasBodySize(int size) {
        return hasBodySize(is(size));
    }

    private HttpResponseAssert hasBodySize(Matcher<? super Integer> matching) {
        Assert.assertThat("response body size", response.body().length, matching);
        return this;
    }

    public HttpResponseAssert hasContentEncodedAs(String charset) {
        return hasContentEncodedAs(equalToIgnoringCase(charset));
    }

    public HttpResponseAssert hasContentEncodedAs(Matcher<? super String> matching) {
        Assert.assertThat("response content encoding", detectCharsetOf(response.body()), matching);
        return this;
    }

    public HttpCookieAssert hasCookie(String named) {
        HttpCookie cookie = response.cookie(named);
        Assert.assertTrue("response contains no cookie named '" + named + "'", cookie != null);
        return HttpCookieAssert.assertThat(cookie);
    }

    public HttpResponseAssert hasNoCookie(String named) {
        HttpCookie cookie = response.cookie(named);
        Assert.assertTrue("response contains unexpected cookie '" + named + "'", cookie == null);
        return this;
    }

    public HttpResponseAssert hasContentType(String contentType) {
        return hasContentType(equalTo(contentType));
    }

    private HttpResponseAssert hasContentType(Matcher<? super String> matching) {<commit_msg>Expose assertion methods on http requests<commit_after>package com.vtence.molecule.testing.http;

import org.hamcrest.Matcher;
import org.junit.Assert;

import java.net.HttpCookie;
import java.util.List;

import static com.vtence.molecule.testing.CharsetDetector.detectCharsetOf;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.equalToIgnoringCase;

public class HttpResponseAssert {
    private final HttpResponse response;

    public HttpResponseAssert(HttpResponse response) {
        this.response = response;
    }

    public static HttpResponseAssert assertThat(HttpResponse response) {
        return new HttpResponseAssert(response);
    }

    public HttpResponseAssert isOK() {
        return hasStatusCode(200);
    }

    public HttpResponseAssert hasStatusCode(int code) {
        return hasStatusCode(is(code));
    }

    private HttpResponseAssert hasStatusCode(Matcher<? super Integer> matching) {
        Assert.assertThat("response status code", response.statusCode(), matching);
        return this;
    }

    public HttpResponseAssert hasStatusMessage(String message) {
        return hasStatusMessage(is(message));
    }

    public HttpResponseAssert hasStatusMessage(Matcher<? super String> matching) {
        Assert.assertThat("response status message", response.statusMessage(), matching);
        return this;
    }

    public HttpResponseAssert hasHeader(String named) {
        return hasHeader(named, notNullValue());
    }

    public HttpResponseAssert hasNoHeader(String named) {
        return hasHeader(named, nullValue());
    }

    public HttpResponseAssert hasHeader(String name, String value) {
        return hasHeader(name, equalTo(value));
    }

    public HttpResponseAssert hasHeader(String name, Matcher<? super String> matching) {
        Assert.assertThat("response '" + name + "' header", response.header(name), matching);
        return this;
    }

    public HttpResponseAssert hasHeaders(String name, Matcher<? super List<String>> matching) {
        Assert.assertThat("response '" + name + "' headers", response.headers(name), matching);
        return this;
    }

    public HttpResponseAssert isChunked() {
        return hasHeader("Transfer-Encoding", "chunked");
    }

    public HttpResponseAssert isNotChunked() {
        return hasHeader("Transfer-Encoding", not("chunked"));
    }

    public HttpResponseAssert hasBodyText(String text) {
        return hasBodyText(equalTo(text));
    }

    public HttpResponseAssert hasBodyText(Matcher<? super String> matching) {
        Assert.assertThat("response body text", response.bodyText(), matching);
        return this;
    }

    public HttpResponseAssert hasBodySize(int size) {
        return hasBodySize(is(size));
    }

    public HttpResponseAssert hasBodySize(Matcher<? super Integer> matching) {
        Assert.assertThat("response body size", response.body().length, matching);
        return this;
    }

    public HttpResponseAssert hasContentEncodedAs(String charset) {
        return hasContentEncodedAs(equalToIgnoringCase(charset));
    }

    public HttpResponseAssert hasContentEncodedAs(Matcher<? super String> matching) {
        Assert.assertThat("response content encoding", detectCharsetOf(response.body()), matching);
        return this;
    }

    public HttpCookieAssert hasCookie(String named) {
        HttpCookie cookie = response.cookie(named);
        Assert.assertTrue("response contains no cookie named '" + named + "'", cookie != null);
        return HttpCookieAssert.assertThat(cookie);
    }

    public HttpResponseAssert hasNoCookie(String named) {
        HttpCookie cookie = response.cookie(named);
        Assert.assertTrue("response contains unexpected cookie '" + named + "'", cookie == null);
        return this;
    }

    public HttpResponseAssert hasContentType(String contentType) {
        return hasContentType(equalTo(contentType));
    }

    public HttpResponseAssert hasContentType(Matcher<? super String> matching) {
{'fraction_non_alphanumeric': 0.09427689384944193, 'fraction_numerical': 0.0007124198527665638, 'mean_word_length': 3.7749433106575965, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '2894010', 'n_tokens_mistral': 2178, 'n_tokens_neox': 2130, 'n_words': 527}