diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2016-10-29 15:44:48 +1300 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2016-10-29 15:44:48 +1300 |
commit | 2dc3284fbb6cc7218e47174c00ba64bf92a50cdf (patch) | |
tree | aa1b4c5f1973bde084de763be9e0b0f4c2511d7c | |
parent | 71d2636594555dc48a9a25837ddd182ed3d5d564 (diff) | |
download | mitmproxy-2dc3284fbb6cc7218e47174c00ba64bf92a50cdf.tar.gz mitmproxy-2dc3284fbb6cc7218e47174c00ba64bf92a50cdf.tar.bz2 mitmproxy-2dc3284fbb6cc7218e47174c00ba64bf92a50cdf.zip |
Add addons.intercept
- Add an addon to handle intercept based on a filter pattern
- Start sketching out a nicer testing truss for addons in mitmproxy.test.taddon
-rw-r--r-- | mitmproxy/addons/intercept.py | 6 | ||||
-rw-r--r-- | mitmproxy/test/taddons.py | 37 | ||||
-rw-r--r-- | test/mitmproxy/addons/test_intercept.py | 25 | ||||
-rw-r--r-- | test/mitmproxy/mastertest.py | 4 |
4 files changed, 65 insertions, 7 deletions
diff --git a/mitmproxy/addons/intercept.py b/mitmproxy/addons/intercept.py index 20599e19..14ef7410 100644 --- a/mitmproxy/addons/intercept.py +++ b/mitmproxy/addons/intercept.py @@ -1,4 +1,4 @@ -import ctx +from mitmproxy import ctx from mitmproxy import flowfilter from mitmproxy import exceptions @@ -11,8 +11,8 @@ class Intercept: if "intercept" in updated: if not opts.intercept: self.filt = None - filt = flowfilter.parse(opts.intercept) - if not filt: + self.filt = flowfilter.parse(opts.intercept) + if not self.filt: raise exceptions.OptionsError( "Invalid interception filter: %s" % opts.intercept ) diff --git a/mitmproxy/test/taddons.py b/mitmproxy/test/taddons.py new file mode 100644 index 00000000..a22fd5e2 --- /dev/null +++ b/mitmproxy/test/taddons.py @@ -0,0 +1,37 @@ +import mitmproxy.master +import mitmproxy.options +from mitmproxy import proxy + + +class context: + """ + A helper context for testing addons. Its most important function is to + set up the ctx module so handlers can be called just like they would be + when running from within mitmproxy. + """ + def __init__(self, master = None, options = None): + self.options = options or mitmproxy.options.Options() + self.master = master or mitmproxy.master.Master( + options, proxy.DummyServer(options) + ) + self.wrapped = None + + def __enter__(self): + self.wrapped = self.master.handlecontext() + self.wrapped.__enter__() + return self + + def __exit__(self, exc_type, exc_value, traceback): + self.wrapped.__exit__(exc_type, exc_value, traceback) + self.wrapped = None + return False + + def configure(self, addon, **kwargs): + """ + A helper for testing configure methods. Modifies the registered + Options object with the given keyword arguments, then calls the + configure method on the addon with the updated value. + """ + for k, v in kwargs.items(): + setattr(self.options, k, v) + addon.configure(self.options, kwargs.keys()) diff --git a/test/mitmproxy/addons/test_intercept.py b/test/mitmproxy/addons/test_intercept.py index e69de29b..19828a62 100644 --- a/test/mitmproxy/addons/test_intercept.py +++ b/test/mitmproxy/addons/test_intercept.py @@ -0,0 +1,25 @@ +from mitmproxy.addons import intercept +from mitmproxy import options +from mitmproxy import exceptions +from mitmproxy.test import taddons +from mitmproxy.test import tutils + + +class Options(options.Options): + def __init__(self, *, intercept=None, **kwargs): + self.intercept = intercept + super().__init__(**kwargs) + + +def test_simple(): + r = intercept.Intercept() + with taddons.context(options=Options()) as tctx: + assert not r.filt + tctx.configure(r, intercept="~q") + assert r.filt + tutils.raises( + exceptions.OptionsError, + tctx.configure, + r, + intercept="~~" + ) diff --git a/test/mitmproxy/mastertest.py b/test/mitmproxy/mastertest.py index cdfb5ad5..0672add9 100644 --- a/test/mitmproxy/mastertest.py +++ b/test/mitmproxy/mastertest.py @@ -10,10 +10,6 @@ from mitmproxy import http from mitmproxy import options -class TestMaster: - pass - - class MasterTest: def cycle(self, master, content): |