<commit_before>import pytest
from pathlib import Path

from mitmproxy.addons.maplocal import MapLocal
from mitmproxy.test import taddons
from mitmproxy.test import tflow

from mitmproxy.addons.modifyheaders import parse_modify_spec


class TestMapLocal:
    def test_file_candidates(self, tmpdir):
        ml = MapLocal()

        url = "https://example.org/img/topic/subtopic/test.jpg"
        spec = parse_modify_spec(":/img/jpg:" + str(tmpdir), True, True)
        file_candidates = ml.file_candidates(url, spec)
        assert file_candidates[0] == str(tmpdir) + "/img/topic/subtopic/test.jpg"
        assert file_candidates[1] == str(tmpdir) + "/img/topic/test.jpg"
        assert file_candidates[2] == str(tmpdir) + "/img/test.jpg"
        assert file_candidates[3] == str(tmpdir) + "/test.jpg"

        url = "https://example.org/img/topic/subtopic/"
        spec = parse_modify_spec(":/img:" + str(tmpdir), True, True)
        file_candidates = ml.file_candidates(url, spec)
        assert file_candidates[0] == str(tmpdir) + "/img/topic/subtopic/index.html"
        assert file_candidates[1] == str(tmpdir) + "/img/topic/index.html"
        assert file_candidates[2] == str(tmpdir) + "/img/index.html"
        assert file_candidates[3] == str(tmpdir) + "/index.html"

        url = "https://example.org"
        spec = parse_modify_spec(":org:" + str(tmpdir), True, True)
        file_candidates = ml.file_candidates(url, spec)
        assert file_candidates[0] == str(tmpdir) + "/index.html"<commit_msg>maplocal addon: add some tests<commit_after>from pathlib import Path

from mitmproxy.addons.maplocal import MapLocal
from mitmproxy.test import taddons
from mitmproxy.test import tflow

from mitmproxy.addons.modifyheaders import parse_modify_spec


class TestMapLocal:
    def test_file_candidates(self, tmpdir):
        ml = MapLocal()

        url = "https://example.org/img/topic/subtopic/test.jpg"
        spec = parse_modify_spec(":/img/jpg:" + str(tmpdir), True, True)
        file_candidates = ml.file_candidates(url, spec)
        assert file_candidates[0] == str(tmpdir) + "/img/topic/subtopic/test.jpg"
        assert file_candidates[1] == str(tmpdir) + "/img/topic/test.jpg"
        assert file_candidates[2] == str(tmpdir) + "/img/test.jpg"
        assert file_candidates[3] == str(tmpdir) + "/test.jpg"

        url = "https://example.org/img/topic/subtopic/"
        spec = parse_modify_spec(":/img:" + str(tmpdir), True, True)
        file_candidates = ml.file_candidates(url, spec)
        assert file_candidates[0] == str(tmpdir) + "/img/topic/subtopic/index.html"
        assert file_candidates[1] == str(tmpdir) + "/img/topic/index.html"
        assert file_candidates[2] == str(tmpdir) + "/img/index.html"
        assert file_candidates[3] == str(tmpdir) + "/index.html"

        url = "https://example.org"
        spec = parse_modify_spec(":org:" + str(tmpdir), True, True)
        file_candidates = ml.file_candidates(url, spec)
        assert file_candidates[0] == str(tmpdir) + "/index.html"

    def test_sanitize_candidate_path(self, tmpdir):
        base_dir = Path(str(tmpdir))

        tmpdir.join("testdir1", "testdir2", "testdir3", "testdir4", "testfile").write("bar", ensure=True)

        ml = MapLocal()
        assert ml.sanitize_candidate_path(
            base_dir.joinpath("..", "bar"), base_dir
        ) is None
        assert ml.sanitize_candidate_path(
            base_dir.joinpath(".."), base_dir
        ) is None
        assert ml.sanitize_candidate_path(
            base_dir.joinpath("..", ".."), base_dir
        ) is None
        assert ml.sanitize_candidate_path(
            base_dir.joinpath("..", "..", "..", "..", "..", "..", "etc", "passwd"), base_dir
        ) is None

        assert ml.sanitize_candidate_path(
            base_dir.joinpath("testdir1"), base_dir
        ) is not None
        assert ml.sanitize_candidate_path(
            base_dir.joinpath("testdir1", "testdir2"), base_dir
        ) is not None
        assert ml.sanitize_candidate_path(
            base_dir.joinpath("testdir1", "testdir2", "testdir3", "testdir4", "testfile"), base_dir
        ) is not None
        assert ml.sanitize_candidate_path(
            base_dir.joinpath("testdir1", "testdir2", "testdir3", "testdir4", "testfile"),
            base_dir.joinpath("testdir1", "testdir2", "testdir3", "testdir4", "testfile")
        ) is not None

    def test_modify_headers(self, tmpdir):
        ml = MapLocal()

        with taddons.context(ml) as tctx:
            tmpfile = tmpdir.join("test1.jpg")
            tmpfile.write("local content 1")

            tctx.configure(
                ml,
                map_local=[
                    ":jpg:" + str(tmpdir)
                ]
            )
            f = tflow.tflow()
            f.request.url = b"https://example.org/images/test1.jpg"
            ml.request(f)
            assert f.response.content == b"local content 1"

            tmpfile = tmpdir.join("images", "test2.jpg")
            tmpfile.write("local content 2", ensure=True)

            tctx.configure(
                ml,
                map_local=[
                    ":jpg:" + str(tmpdir)
                ]
            )
            f = tflow.tflow()
            f.request.url = b"https://example.org/images/test2.jpg"
            ml.request(f)
            assert f.response.content == b"local content 2"

            tmpfile = tmpdir.join("images", "test3.jpg")
            tmpfile.write("local content 3", ensure=True)

            tctx.configure(
                ml,
                map_local=[
                    ":jpg:" + str(tmpfile)
                ]
            )
            f = tflow.tflow()
            f.request.url = b"https://example.org/images/test3.jpg"
            ml.request(f)
            assert f.response.content == b"local content 3"
{'fraction_non_alphanumeric': 0.1366906474820144, 'fraction_numerical': 0.008393285371702638, 'mean_word_length': 2.30634201585504, 'pattern_counts': {'":': 9, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 9, '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': '3817491', 'n_tokens_mistral': 1930, 'n_tokens_neox': 1865, 'n_words': 345}