Replacements and setheaders are always strings

Instead of having two representations we have one canonical specification.
Fixing the editor in console is left ot a further patch.
This commit is contained in:
Aldo Cortesi 2017-03-06 20:58:51 +13:00
parent 82163a1e68
commit edfd62e42a
4 changed files with 38 additions and 44 deletions

View File

@ -1,4 +1,4 @@
from typing import Tuple, Optional, Sequence, Union
from typing import Optional, Sequence
from mitmproxy.net import tcp
from mitmproxy import optmanager
@ -114,14 +114,26 @@ class Options(optmanager.OptManager):
"showhost", False, bool,
"Use the Host header to construct URLs for display."
)
self.add_option("replacements", [], Sequence[Union[Tuple[str, str, str], str]])
self.add_option("replacement_files", [], Sequence[Union[Tuple[str, str, str], str]])
self.add_option(
"replacements", [], Sequence[str],
"Replacement patterns."
)
self.add_option(
"replacement_files", [], Sequence[str],
"""
Replacement pattern, where the replacement clause is a path to a
file.
"""
)
self.add_option(
"server_replay_use_headers", [], Sequence[str],
"Request headers to be considered during replay. "
"Can be passed multiple times."
)
self.add_option("setheaders", [], Sequence[Union[Tuple[str, str, str], str]])
self.add_option(
"setheaders", [], Sequence[str],
help="Header set pattern."
)
self.add_option(
"server_replay", [], Sequence[str],
"Replay server responses from a saved file."

View File

@ -198,21 +198,8 @@ def replacements(parser, opts):
for more information.
""".strip()
)
group.add_argument(
"--replace",
action="append", type=str, dest="replacements",
metavar="PATTERN",
help="Replacement pattern."
)
group.add_argument(
"--replace-from-file",
action="append", type=str, dest="replacement_files",
metavar="PATH",
help="""
Replacement pattern, where the replacement clause is a path to a
file.
"""
)
opts.make_parser(group, "replacements", metavar="PATTERN")
opts.make_parser(group, "replacement_files", metavar="PATTERN")
def set_headers(parser, opts):
@ -224,12 +211,7 @@ def set_headers(parser, opts):
documentation for more information.
""".strip()
)
group.add_argument(
"--setheader",
action="append", type=str, dest="setheaders",
metavar="PATTERN",
help="Header set pattern."
)
opts.make_parser(group, "setheaders", metavar="PATTERN")
def proxy_authentication(parser, opts):

View File

@ -22,11 +22,11 @@ class TestReplace:
def test_configure(self):
r = replace.Replace()
with taddons.context() as tctx:
tctx.configure(r, replacements=[("one", "two", "three")])
tctx.configure(r, replacements=["one/two/three"])
with pytest.raises(Exception, match="Invalid filter pattern"):
tctx.configure(r, replacements=[("~b", "two", "three")])
tctx.configure(r, replacements=["/~b/two/three"])
with pytest.raises(Exception, match="Invalid regular expression"):
tctx.configure(r, replacements=[("foo", "+", "three")])
tctx.configure(r, replacements=["/foo/+/three"])
tctx.configure(r, replacements=["/a/b/c/"])
def test_simple(self):
@ -35,8 +35,8 @@ class TestReplace:
tctx.configure(
r,
replacements = [
("~q", "foo", "bar"),
("~s", "foo", "bar"),
"/~q/foo/bar",
"/~s/foo/bar",
]
)
f = tflow.tflow()
@ -58,10 +58,10 @@ class TestUpstreamProxy(tservers.HTTPUpstreamProxyTest):
self.proxy.tmaster.addons.add(sa)
self.proxy.tmaster.options.replacements = [
("~q", "foo", "bar"),
("~q", "bar", "baz"),
("~q", "foo", "oh noes!"),
("~s", "baz", "ORLY")
"/~q/foo/bar",
"/~q/bar/baz",
"/~q/foo/oh noes!",
"/~s/baz/ORLY"
]
p = self.pathoc()
with p.connect():
@ -81,9 +81,9 @@ class TestReplaceFile:
tctx.configure(
r,
replacement_files = [
("~q", "foo", rp),
("~s", "foo", rp),
("~b nonexistent", "nonexistent", "nonexistent"),
"/~q/foo/" + rp,
"/~s/foo/" + rp,
"/~b nonexistent/nonexistent/nonexistent",
]
)
f = tflow.tflow()

View File

@ -21,7 +21,7 @@ class TestSetHeaders:
sh = setheaders.SetHeaders()
with taddons.context() as tctx:
with pytest.raises(Exception, match="Invalid setheader filter pattern"):
tctx.configure(sh, setheaders = [("~b", "one", "two")])
tctx.configure(sh, setheaders = ["/~b/one/two"])
tctx.configure(sh, setheaders = ["/foo/bar/voing"])
def test_setheaders(self):
@ -30,8 +30,8 @@ class TestSetHeaders:
tctx.configure(
sh,
setheaders = [
("~q", "one", "two"),
("~s", "one", "three")
"/~q/one/two",
"/~s/one/three"
]
)
f = tflow.tflow()
@ -47,8 +47,8 @@ class TestSetHeaders:
tctx.configure(
sh,
setheaders = [
("~s", "one", "two"),
("~s", "one", "three")
"/~s/one/two",
"/~s/one/three"
]
)
f = tflow.tflow(resp=True)
@ -60,8 +60,8 @@ class TestSetHeaders:
tctx.configure(
sh,
setheaders = [
("~q", "one", "two"),
("~q", "one", "three")
"/~q/one/two",
"/~q/one/three"
]
)
f = tflow.tflow()