replacements: error right away when file does not exist

This commit is contained in:
Maximilian Hils 2017-03-12 20:46:00 +01:00
parent 4684617d2c
commit 0f4b523868
2 changed files with 34 additions and 28 deletions

View File

@ -72,6 +72,10 @@ class Replace:
raise exceptions.OptionsError(
"Invalid regular expression: %s - %s" % (rex, str(e))
)
if s.startswith("@") and not os.path.isfile(s[1:]):
raise exceptions.OptionsError(
"Invalid file path: {}".format(s[1:])
)
lst.append((rex, s, flt))
self.lst = lst

View File

@ -1,11 +1,8 @@
import os.path
import pytest
from mitmproxy.addons import replace
from mitmproxy.test import taddons
from mitmproxy.test import tflow
from mitmproxy.test import tutils
class TestReplace:
@ -68,33 +65,38 @@ class TestReplace:
class TestReplaceFile:
def test_simple(self):
def test_simple(self, tmpdir):
r = replace.Replace()
with tutils.tmpdir() as td:
rp = os.path.join(td, "replacement")
with open(rp, "w") as f:
f.write("bar")
with taddons.context() as tctx:
with taddons.context() as tctx:
tmpfile = tmpdir.join("replacement")
tmpfile.write("bar")
tctx.configure(
r,
replacements=["/~q/foo/@" + str(tmpfile)]
)
f = tflow.tflow()
f.request.content = b"foo"
r.request(f)
assert f.request.content == b"bar"
def test_nonexistent(self, tmpdir):
r = replace.Replace()
with taddons.context() as tctx:
with pytest.raises(Exception, match="Invalid file path"):
tctx.configure(
r,
replacements=[
"/~q/foo/@" + rp,
"/~s/foo/@" + rp,
"/~b nonexistent/nonexistent/@nonexistent",
]
replacements=["/~q/foo/@nonexistent"]
)
f = tflow.tflow()
f.request.content = b"foo"
r.request(f)
assert f.request.content == b"bar"
f = tflow.tflow(resp=True)
f.response.content = b"foo"
r.response(f)
assert f.response.content == b"bar"
f = tflow.tflow()
f.request.content = b"nonexistent"
assert not tctx.master.event_log
r.request(f)
assert tctx.master.event_log
tmpfile = tmpdir.join("replacement")
tmpfile.write("bar")
tctx.configure(
r,
replacements=["/~q/foo/@" + str(tmpfile)]
)
tmpfile.remove()
f = tflow.tflow()
f.request.content = b"foo"
assert not tctx.master.event_log
r.request(f)
assert tctx.master.event_log