From 144b559b468ac2c67e0d79fae8754ae3385f1958 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 24 Feb 2018 15:22:28 +1300 Subject: [PATCH] addon options: migrate replace, simplify taddons.context --- mitmproxy/addons/replace.py | 10 ++++++++++ mitmproxy/options.py | 8 -------- mitmproxy/test/taddons.py | 7 +++---- test/mitmproxy/addons/test_replace.py | 10 +++++----- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/mitmproxy/addons/replace.py b/mitmproxy/addons/replace.py index 054264fa4..5173254a3 100644 --- a/mitmproxy/addons/replace.py +++ b/mitmproxy/addons/replace.py @@ -1,5 +1,6 @@ import os import re +import typing from mitmproxy import exceptions from mitmproxy import flowfilter @@ -47,6 +48,15 @@ class Replace: def __init__(self): self.lst = [] + def load(self, loader): + loader.add_option( + "replacements", typing.Sequence[str], [], + """ + Replacement patterns of the form "/pattern/regex/replacement", where + the separator can be any character. + """ + ) + def configure(self, updated): """ .replacements is a list of tuples (fpat, rex, s): diff --git a/mitmproxy/options.py b/mitmproxy/options.py index 02ae9c0fb..6db2b907a 100644 --- a/mitmproxy/options.py +++ b/mitmproxy/options.py @@ -67,7 +67,6 @@ class Options(optmanager.OptManager): view_filter = None # type: Optional[str] # FIXME: Options that should be uncomplicated to migrate to addons - replacements = None # type: Sequence[str] rfile = None # type: Optional[str] save_stream_file = None # type: Optional[str] save_stream_filter = None # type: Optional[str] @@ -105,13 +104,6 @@ class Options(optmanager.OptManager): "showhost", bool, False, "Use the Host header to construct URLs for display." ) - self.add_option( - "replacements", Sequence[str], [], - """ - Replacement patterns of the form "/pattern/regex/replacement", where - the separator can be any character. - """ - ) self.add_option( "setheaders", Sequence[str], [], """ diff --git a/mitmproxy/test/taddons.py b/mitmproxy/test/taddons.py index 12fc09861..7efa2eaf8 100644 --- a/mitmproxy/test/taddons.py +++ b/mitmproxy/test/taddons.py @@ -59,17 +59,16 @@ class context: provides a number of helper methods for common testing scenarios. """ - def __init__(self, *addons, master=None, options=None): + def __init__(self, *addons, options=None): options = options or mitmproxy.options.Options() - self.master = master or RecordingMaster( + self.master = RecordingMaster( options ) self.options = self.master.options self.wrapped = None - loader = addonmanager.Loader(self.master) for a in addons: - self.master.addons.invoke_addon(a, "load", loader) + self.master.addons.register(a) def ctx(self): """ diff --git a/test/mitmproxy/addons/test_replace.py b/test/mitmproxy/addons/test_replace.py index 9002afb5c..9c1f7f79f 100644 --- a/test/mitmproxy/addons/test_replace.py +++ b/test/mitmproxy/addons/test_replace.py @@ -18,7 +18,7 @@ class TestReplace: def test_configure(self): r = replace.Replace() - with taddons.context() as tctx: + with taddons.context(r) as tctx: tctx.configure(r, replacements=["one/two/three"]) with pytest.raises(Exception, match="Invalid filter pattern"): tctx.configure(r, replacements=["/~b/two/three"]) @@ -28,7 +28,7 @@ class TestReplace: def test_simple(self): r = replace.Replace() - with taddons.context() as tctx: + with taddons.context(r) as tctx: tctx.configure( r, replacements=[ @@ -48,7 +48,7 @@ class TestReplace: def test_order(self): r = replace.Replace() - with taddons.context() as tctx: + with taddons.context(r) as tctx: tctx.configure( r, replacements=[ @@ -67,7 +67,7 @@ class TestReplace: class TestReplaceFile: def test_simple(self, tmpdir): r = replace.Replace() - with taddons.context() as tctx: + with taddons.context(r) as tctx: tmpfile = tmpdir.join("replacement") tmpfile.write("bar") tctx.configure( @@ -81,7 +81,7 @@ class TestReplaceFile: def test_nonexistent(self, tmpdir): r = replace.Replace() - with taddons.context() as tctx: + with taddons.context(r) as tctx: with pytest.raises(Exception, match="Invalid file path"): tctx.configure( r,