<commit_before>from unittest import mock
import pytest

from mitmproxy import contentviews
from mitmproxy.exceptions import ContentViewException
from mitmproxy.net.http import Headers
from mitmproxy.test import tutils


class TestContentView(contentviews.View):
    name = "test"
    content_types = ["test/123"]


def test_add_remove():
    tcv = TestContentView()
    contentviews.add(tcv)
    assert tcv in contentviews.views

    # repeated addition causes exception
    with pytest.raises(ContentViewException, match="Duplicate view"):
        contentviews.add(tcv)

    contentviews.remove(tcv)
    assert tcv not in contentviews.views


def test_get_content_view():
    desc, lines, err = contentviews.get_content_view(
        contentviews.get("Raw"),
        b"[1, 2, 3]",
    )
    assert "Raw" in desc
    assert list(lines)
    assert not err

    desc, lines, err = contentviews.get_content_view(
        contentviews.get("Auto"),
        b"[1, 2, 3]",
        headers=Headers(content_type="application/json")
    )
    assert desc == "JSON"
    assert list(lines)

    desc, lines, err = contentviews.get_content_view(
        contentviews.get("JSON"),
        b"[1, 2",
    )
    assert "Couldn't parse" in desc

    with mock.patch("mitmproxy.contentviews.auto.ViewAuto.__call__") as view_auto:
        view_auto.side_effect = ValueError

        desc, lines, err = contentviews.get_content_view(
            contentviews.get("Auto"),
            b"[1, 2",
        )
        assert err
        assert "Couldn't parse" in desc


def test_get_message_content_view():
    r = tutils.treq()
    desc, lines, err = contentviews.get_message_content_view("raw", r)
    assert desc == "Raw"

    desc, lines, err = contentviews.get_message_content_view("unknown", r)
    assert desc == "Raw"

    r.encode("gzip")
    desc, lines, err = contentviews.get_message_content_view("raw", r)
    assert desc == "[decoded gzip] Raw"

    r.headers["content-encoding"] = "deflate"
    desc, lines, err = contentviews.get_message_content_view("raw", r)
    assert desc == "[cannot decode] Raw"

    r.content = None
    desc, lines, err = contentviews.get_message_content_view("raw", r)
    assert list(lines) == [[("error", "content missing")]]

<commit_msg>Flake, be happy<commit_after>from unittest import mock
import pytest

from mitmproxy import contentviews
from mitmproxy.exceptions import ContentViewException
from mitmproxy.net.http import Headers
from mitmproxy.test import tutils


class TestContentView(contentviews.View):
    name = "test"
    content_types = ["test/123"]


def test_add_remove():
    tcv = TestContentView()
    contentviews.add(tcv)
    assert tcv in contentviews.views

    # repeated addition causes exception
    with pytest.raises(ContentViewException, match="Duplicate view"):
        contentviews.add(tcv)

    contentviews.remove(tcv)
    assert tcv not in contentviews.views


def test_get_content_view():
    desc, lines, err = contentviews.get_content_view(
        contentviews.get("Raw"),
        b"[1, 2, 3]",
    )
    assert "Raw" in desc
    assert list(lines)
    assert not err

    desc, lines, err = contentviews.get_content_view(
        contentviews.get("Auto"),
        b"[1, 2, 3]",
        headers=Headers(content_type="application/json")
    )
    assert desc == "JSON"
    assert list(lines)

    desc, lines, err = contentviews.get_content_view(
        contentviews.get("JSON"),
        b"[1, 2",
    )
    assert "Couldn't parse" in desc

    with mock.patch("mitmproxy.contentviews.auto.ViewAuto.__call__") as view_auto:
        view_auto.side_effect = ValueError

        desc, lines, err = contentviews.get_content_view(
            contentviews.get("Auto"),
            b"[1, 2",
        )
        assert err
        assert "Couldn't parse" in desc


def test_get_message_content_view():
    r = tutils.treq()
    desc, lines, err = contentviews.get_message_content_view("raw", r)
    assert desc == "Raw"

    desc, lines, err = contentviews.get_message_content_view("unknown", r)
    assert desc == "Raw"

    r.encode("gzip")
    desc, lines, err = contentviews.get_message_content_view("raw", r)
    assert desc == "[decoded gzip] Raw"

    r.headers["content-encoding"] = "deflate"
    desc, lines, err = contentviews.get_message_content_view("raw", r)
    assert desc == "[cannot decode] Raw"

    r.content = None
    desc, lines, err = contentviews.get_message_content_view("raw", r)
    assert list(lines) == [[("error", "content missing")]]
{'fraction_non_alphanumeric': 0.11497563136907399, 'fraction_numerical': 0.0057598582188746125, 'mean_word_length': 3.271523178807947, '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': '6188204', 'n_tokens_mistral': 1441, 'n_tokens_neox': 1390, 'n_words': 348}