maplocal addon: improve tests

This commit is contained in:
Martin Plattner 2020-07-08 23:52:22 +02:00
parent 55bba92653
commit 41c99810ef

View File

@ -1,77 +1,44 @@
from pathlib import Path
import pytest
from mitmproxy.addons.maplocal import MapLocal
from mitmproxy.addons.maplocal import MapLocal, file_candidates
from mitmproxy.test import taddons
from mitmproxy.test import tflow
from mitmproxy.addons.modifyheaders import parse_modify_spec
@pytest.mark.parametrize(
"url,spec,expected_candidates", [
(
"https://example.org/img/topic/subtopic/test.jpg",
":example.com/foo:/tmp",
["/tmp/img/topic/subtopic/test.jpg", "/tmp/img/topic/test.jpg", "/tmp/img/test.jpg", "/tmp/test.jpg"]
),
(
"https://example.org/img/topic/subtopic/",
":/img:/tmp",
["/tmp/img/topic/subtopic/index.html", "/tmp/img/topic/index.html", "/tmp/img/index.html", "/tmp/index.html"]
),
(
"https://example.org",
":org:/tmp",
["/tmp/index.html"]
),
]
)
def test_file_candidates(url, spec, expected_candidates):
spec = parse_modify_spec(spec, True, True)
candidates = file_candidates(url, spec.replacement)
assert [str(x) for x in candidates] == expected_candidates
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):
def test_map_local(self, tmpdir):
ml = MapLocal()
with taddons.context(ml) as tctx:
tmpfile = tmpdir.join("test1.jpg")
tmpfile.write("local content 1")
tmpfile.write("foo")
tctx.configure(
ml,
map_local=[
@ -81,11 +48,10 @@ class TestMapLocal:
f = tflow.tflow()
f.request.url = b"https://example.org/images/test1.jpg"
ml.request(f)
assert f.response.content == b"local content 1"
assert f.response.content == b"foo"
tmpfile = tmpdir.join("images", "test2.jpg")
tmpfile.write("local content 2", ensure=True)
tmpfile.write("bar", ensure=True)
tctx.configure(
ml,
map_local=[
@ -95,11 +61,10 @@ class TestMapLocal:
f = tflow.tflow()
f.request.url = b"https://example.org/images/test2.jpg"
ml.request(f)
assert f.response.content == b"local content 2"
assert f.response.content == b"bar"
tmpfile = tmpdir.join("images", "test3.jpg")
tmpfile.write("local content 3", ensure=True)
tmpfile.write("foobar", ensure=True)
tctx.configure(
ml,
map_local=[
@ -107,6 +72,6 @@ class TestMapLocal:
]
)
f = tflow.tflow()
f.request.url = b"https://example.org/images/test3.jpg"
f.request.url = b"https://example.org/foo.jpg"
ml.request(f)
assert f.response.content == b"local content 3"
assert f.response.content == b"foobar"