From edfd62e42af921d0031ff95b7cf41ab1b6608a47 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Mon, 6 Mar 2017 20:58:51 +1300 Subject: [PATCH] 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. --- mitmproxy/options.py | 20 ++++++++++++++++---- mitmproxy/tools/cmdline.py | 24 +++--------------------- test/mitmproxy/addons/test_replace.py | 24 ++++++++++++------------ test/mitmproxy/addons/test_setheaders.py | 14 +++++++------- 4 files changed, 38 insertions(+), 44 deletions(-) diff --git a/mitmproxy/options.py b/mitmproxy/options.py index 41e020312..788df7e9e 100644 --- a/mitmproxy/options.py +++ b/mitmproxy/options.py @@ -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." diff --git a/mitmproxy/tools/cmdline.py b/mitmproxy/tools/cmdline.py index 1160e1e49..ef99ba372 100644 --- a/mitmproxy/tools/cmdline.py +++ b/mitmproxy/tools/cmdline.py @@ -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): diff --git a/test/mitmproxy/addons/test_replace.py b/test/mitmproxy/addons/test_replace.py index 126c6e3df..8c280c51c 100644 --- a/test/mitmproxy/addons/test_replace.py +++ b/test/mitmproxy/addons/test_replace.py @@ -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() diff --git a/test/mitmproxy/addons/test_setheaders.py b/test/mitmproxy/addons/test_setheaders.py index 6355f2be0..3aaee7f40 100644 --- a/test/mitmproxy/addons/test_setheaders.py +++ b/test/mitmproxy/addons/test_setheaders.py @@ -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()