From b6abe96202d68a9f983d99a90457d1c85062fbfc Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Thu, 16 Mar 2017 11:18:23 +1300 Subject: Move dump handlers to addon --- mitmproxy/tools/dump.py | 24 ++++++++++++++++-------- mitmproxy/tools/main.py | 8 ++++---- test/mitmproxy/tools/test_dump.py | 2 +- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/mitmproxy/tools/dump.py b/mitmproxy/tools/dump.py index 42930a7e..adb1f7ef 100644 --- a/mitmproxy/tools/dump.py +++ b/mitmproxy/tools/dump.py @@ -1,10 +1,19 @@ -from mitmproxy import controller +from mitmproxy import ctx from mitmproxy import addons from mitmproxy import options from mitmproxy import master from mitmproxy.addons import dumper, termlog, termstatus, readstdin, keepserving +class ErrorCheck: + def __init__(self): + self.has_errored = False + + def log(self, e): + if e.level == "error": + self.has_errored = True + + class DumpMaster(master.Master): def __init__( @@ -15,15 +24,14 @@ class DumpMaster(master.Master): with_dumper=True, ) -> None: master.Master.__init__(self, options, server) - self.has_errored = False + self.errorcheck = ErrorCheck() if with_termlog: self.addons.add(termlog.TermLog(), termstatus.TermStatus()) self.addons.add(*addons.default_addons()) if with_dumper: self.addons.add(dumper.Dumper()) - self.addons.add(readstdin.ReadStdin(), keepserving.KeepServing()) - - @controller.handler - def log(self, e): - if e.level == "error": - self.has_errored = True + self.addons.add( + readstdin.ReadStdin(), + keepserving.KeepServing(), + self.errorcheck + ) diff --git a/mitmproxy/tools/main.py b/mitmproxy/tools/main.py index 35567b62..b321e8f8 100644 --- a/mitmproxy/tools/main.py +++ b/mitmproxy/tools/main.py @@ -91,9 +91,7 @@ def run(MasterKlass, args): # pragma: no cover sys.exit(1) except (KeyboardInterrupt, RuntimeError): pass - if master is None or getattr(master, "has_errored", None): - print("%s: errors occurred during run" % sys.argv[0], file=sys.stderr) - sys.exit(1) + return master def mitmproxy(args=None): # pragma: no cover @@ -109,7 +107,9 @@ def mitmproxy(args=None): # pragma: no cover def mitmdump(args=None): # pragma: no cover from mitmproxy.tools import dump - run(dump.DumpMaster, args) + m = run(dump.DumpMaster, args) + if m and m.errorcheck.has_errored: + sys.exit(1) def mitmweb(args=None): # pragma: no cover diff --git a/test/mitmproxy/tools/test_dump.py b/test/mitmproxy/tools/test_dump.py index 624bf08f..810fcaaa 100644 --- a/test/mitmproxy/tools/test_dump.py +++ b/test/mitmproxy/tools/test_dump.py @@ -21,7 +21,7 @@ class TestDumpMaster(tservers.MasterTest): ent = log.LogEntry("foo", "error") ent.reply = controller.DummyReply() m.log(ent) - assert m.has_errored + assert m.errorcheck.has_errored @pytest.mark.parametrize("termlog", [False, True]) def test_addons_termlog(self, termlog): -- cgit v1.2.3 From 85ddc5056b982f16451fc97c655e54843b76b377 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Thu, 16 Mar 2017 11:21:44 +1300 Subject: Move mitmproxy console handlers to addon --- mitmproxy/tools/console/master.py | 62 +++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/mitmproxy/tools/console/master.py b/mitmproxy/tools/console/master.py index b6339817..d39e5e5d 100644 --- a/mitmproxy/tools/console/master.py +++ b/mitmproxy/tools/console/master.py @@ -40,6 +40,35 @@ class Logger: signals.add_log(evt.msg, evt.level) +class UnsupportedLog: + """ + A small addon to dump info on flow types we don't support yet. + """ + def websocket_message(self, f): + message = f.messages[-1] + signals.add_log(f.message_info(message), "info") + signals.add_log(strutils.bytes_to_escaped_str(message.content), "debug") + + def websocket_end(self, f): + signals.add_log("WebSocket connection closed by {}: {} {}, {}".format( + f.close_sender, + f.close_code, + f.close_message, + f.close_reason), "info") + + def tcp_message(self, f): + message = f.messages[-1] + direction = "->" if message.from_client else "<-" + signals.add_log("{client_host}:{client_port} {direction} tcp {direction} {server_host}:{server_port}".format( + client_host=f.client_conn.address[0], + client_port=f.client_conn.address[1], + server_host=f.server_conn.address[0], + server_port=f.server_conn.address[1], + direction=direction, + ), "info") + signals.add_log(strutils.bytes_to_escaped_str(message.content), "debug") + + class ConsoleMaster(master.Master): palette = [] @@ -63,7 +92,7 @@ class ConsoleMaster(master.Master): signals.sig_add_log.connect(self.sig_add_log) self.addons.add(Logger()) self.addons.add(*addons.default_addons()) - self.addons.add(intercept.Intercept(), self.view) + self.addons.add(intercept.Intercept(), self.view, UnsupportedLog()) def sigint_handler(*args, **kwargs): self.prompt_for_exit() @@ -396,34 +425,3 @@ class ConsoleMaster(master.Master): def clear_events(self): self.logbuffer[:] = [] - - # Handlers - @controller.handler - def websocket_message(self, f): - super().websocket_message(f) - message = f.messages[-1] - signals.add_log(f.message_info(message), "info") - signals.add_log(strutils.bytes_to_escaped_str(message.content), "debug") - - @controller.handler - def websocket_end(self, f): - super().websocket_end(f) - signals.add_log("WebSocket connection closed by {}: {} {}, {}".format( - f.close_sender, - f.close_code, - f.close_message, - f.close_reason), "info") - - @controller.handler - def tcp_message(self, f): - super().tcp_message(f) - message = f.messages[-1] - direction = "->" if message.from_client else "<-" - signals.add_log("{client_host}:{client_port} {direction} tcp {direction} {server_host}:{server_port}".format( - client_host=f.client_conn.address[0], - client_port=f.client_conn.address[1], - server_host=f.server_conn.address[0], - server_port=f.server_conn.address[1], - direction=direction, - ), "info") - signals.add_log(strutils.bytes_to_escaped_str(message.content), "debug") -- cgit v1.2.3 From 1410cbb4b64173ac1763834272415dba1cd54653 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Thu, 16 Mar 2017 12:50:33 +1300 Subject: Remove test handlers by using taddons.RecordingMaster This also means expanding and tweaking the recording master API, which we reflect through the current test suite --- examples/complex/flowbasic.py | 43 --------------------------- mitmproxy/addons/check_alpn.py | 6 ++-- mitmproxy/test/taddons.py | 17 ++++++++--- mitmproxy/tools/console/master.py | 1 - mitmproxy/tools/dump.py | 1 - test/mitmproxy/addons/test_check_alpn.py | 4 +-- test/mitmproxy/addons/test_check_ca.py | 2 +- test/mitmproxy/addons/test_dumper.py | 2 +- test/mitmproxy/addons/test_script.py | 51 +++++++++++++++----------------- test/mitmproxy/proxy/test_server.py | 12 ++------ test/mitmproxy/script/test_concurrent.py | 2 +- test/mitmproxy/test_controller.py | 27 ++++++++--------- test/mitmproxy/tservers.py | 17 ++++------- 13 files changed, 65 insertions(+), 120 deletions(-) delete mode 100644 examples/complex/flowbasic.py diff --git a/examples/complex/flowbasic.py b/examples/complex/flowbasic.py deleted file mode 100644 index dafdd084..00000000 --- a/examples/complex/flowbasic.py +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env python3 -""" - This example shows how to build a proxy based on mitmproxy's Flow - primitives. - - Heads Up: In the majority of cases, you want to use inline scripts. - - Note that request and response messages are not automatically replied to, - so we need to implement handlers to do this. -""" -from mitmproxy import controller, options, master -from mitmproxy.proxy import ProxyServer, ProxyConfig - - -class MyMaster(master.Master): - def run(self): - try: - master.Master.run(self) - except KeyboardInterrupt: - self.shutdown() - - @controller.handler - def request(self, f): - print("request", f) - - @controller.handler - def response(self, f): - print("response", f) - - @controller.handler - def error(self, f): - print("error", f) - - @controller.handler - def log(self, l): - print("log", l.msg) - - -opts = options.Options(cadir="~/.mitmproxy/") -config = ProxyConfig(opts) -server = ProxyServer(config) -m = MyMaster(opts, server) -m.run() diff --git a/mitmproxy/addons/check_alpn.py b/mitmproxy/addons/check_alpn.py index c288d788..cb3c87e3 100644 --- a/mitmproxy/addons/check_alpn.py +++ b/mitmproxy/addons/check_alpn.py @@ -1,5 +1,6 @@ import mitmproxy from mitmproxy.net import tcp +from mitmproxy import ctx class CheckALPN: @@ -9,9 +10,8 @@ class CheckALPN: def configure(self, options, updated): self.failed = mitmproxy.ctx.master.options.http2 and not tcp.HAS_ALPN if self.failed: - mitmproxy.ctx.master.add_log( + ctx.log.warn( "HTTP/2 is disabled because ALPN support missing!\n" "OpenSSL 1.0.2+ required to support HTTP/2 connections.\n" - "Use --no-http2 to silence this warning.", - "warn", + "Use --no-http2 to silence this warning." ) diff --git a/mitmproxy/test/taddons.py b/mitmproxy/test/taddons.py index c3b65e92..29ae7aa9 100644 --- a/mitmproxy/test/taddons.py +++ b/mitmproxy/test/taddons.py @@ -12,7 +12,10 @@ class _AddonWrapper: self.addons = addons def trigger(self, event, *args, **kwargs): - self.master.events.append((event, args, kwargs)) + if event == "log": + self.master.logs.append(args[0]) + else: + self.master.events.append((event, args, kwargs)) return self.addons.trigger(event, *args, **kwargs) def __getattr__(self, attr): @@ -26,13 +29,19 @@ class RecordingMaster(mitmproxy.master.Master): self.events = [] self.logs = [] + def has_log(self, txt, level=None): + for i in self.logs: + if level and i.level != level: + continue + if txt.lower() in i.msg.lower(): + return True + return False + def has_event(self, name): for i in self.events: if i[0] == name: return True - - def add_log(self, e, level): - self.logs.append((level, e)) + return False def clear(self): self.logs = [] diff --git a/mitmproxy/tools/console/master.py b/mitmproxy/tools/console/master.py index d39e5e5d..d0e23712 100644 --- a/mitmproxy/tools/console/master.py +++ b/mitmproxy/tools/console/master.py @@ -13,7 +13,6 @@ import traceback import urwid from mitmproxy import addons -from mitmproxy import controller from mitmproxy import exceptions from mitmproxy import master from mitmproxy import io diff --git a/mitmproxy/tools/dump.py b/mitmproxy/tools/dump.py index adb1f7ef..e6f0c3df 100644 --- a/mitmproxy/tools/dump.py +++ b/mitmproxy/tools/dump.py @@ -1,4 +1,3 @@ -from mitmproxy import ctx from mitmproxy import addons from mitmproxy import options from mitmproxy import master diff --git a/test/mitmproxy/addons/test_check_alpn.py b/test/mitmproxy/addons/test_check_alpn.py index ffaf6cff..2b1d6058 100644 --- a/test/mitmproxy/addons/test_check_alpn.py +++ b/test/mitmproxy/addons/test_check_alpn.py @@ -12,7 +12,7 @@ class TestCheckALPN: with taddons.context() as tctx: a = check_alpn.CheckALPN() tctx.configure(a) - assert not any(msg in m for l, m in tctx.master.logs) + assert not tctx.master.has_log(msg) def test_check_no_alpn(self, disable_alpn): msg = 'ALPN support missing' @@ -20,4 +20,4 @@ class TestCheckALPN: with taddons.context() as tctx: a = check_alpn.CheckALPN() tctx.configure(a) - assert any(msg in m for l, m in tctx.master.logs) + assert tctx.master.has_log(msg) diff --git a/test/mitmproxy/addons/test_check_ca.py b/test/mitmproxy/addons/test_check_ca.py index 859b6d8d..cd34a9be 100644 --- a/test/mitmproxy/addons/test_check_ca.py +++ b/test/mitmproxy/addons/test_check_ca.py @@ -16,4 +16,4 @@ class TestCheckCA: tctx.master.server.config.certstore.default_ca.has_expired = mock.MagicMock(return_value=expired) a = check_ca.CheckCA() tctx.configure(a) - assert any(msg in m for l, m in tctx.master.logs) is expired + assert tctx.master.has_log(msg) is expired diff --git a/test/mitmproxy/addons/test_dumper.py b/test/mitmproxy/addons/test_dumper.py index 23299431..fbcc4d16 100644 --- a/test/mitmproxy/addons/test_dumper.py +++ b/test/mitmproxy/addons/test_dumper.py @@ -151,7 +151,7 @@ class TestContentView: with taddons.context(options=options.Options()) as ctx: ctx.configure(d, flow_detail=4, verbosity=3) d.response(tflow.tflow()) - assert "Content viewer failed" in ctx.master.logs[0][1] + assert ctx.master.has_log("content viewer failed") def test_tcp(): diff --git a/test/mitmproxy/addons/test_script.py b/test/mitmproxy/addons/test_script.py index ad3c9a1a..16827488 100644 --- a/test/mitmproxy/addons/test_script.py +++ b/test/mitmproxy/addons/test_script.py @@ -22,14 +22,12 @@ def test_scriptenv(): with taddons.context() as tctx: with script.scriptenv("path", []): raise SystemExit - assert tctx.master.logs[0][0] == "error" - assert "exited" in tctx.master.logs[0][1] + assert tctx.master.has_log("exited", "error") tctx.master.clear() with script.scriptenv("path", []): raise ValueError("fooo") - assert tctx.master.logs[0][0] == "error" - assert "foo" in tctx.master.logs[0][1] + assert tctx.master.has_log("fooo", "error") class Called: @@ -147,11 +145,11 @@ class TestScript: sc.start(tctx.options) f = tflow.tflow(resp=True) sc.request(f) - assert tctx.master.logs[0][0] == "error" - assert len(tctx.master.logs[0][1].splitlines()) == 6 - assert re.search(r'addonscripts[\\/]error.py", line \d+, in request', tctx.master.logs[0][1]) - assert re.search(r'addonscripts[\\/]error.py", line \d+, in mkerr', tctx.master.logs[0][1]) - assert tctx.master.logs[0][1].endswith("ValueError: Error!\n") + assert tctx.master.logs[0].level == "error" + assert len(tctx.master.logs[0].msg.splitlines()) == 6 + assert re.search(r'addonscripts[\\/]error.py", line \d+, in request', tctx.master.logs[0].msg) + assert re.search(r'addonscripts[\\/]error.py", line \d+, in mkerr', tctx.master.logs[0].msg) + assert tctx.master.logs[0].msg.endswith("ValueError: Error!\n") def test_addon(self): with taddons.context() as tctx: @@ -256,19 +254,19 @@ class TestScriptLoader: "%s %s" % (rec, "c"), ] ) - debug = [(i[0], i[1]) for i in tctx.master.logs if i[0] == "debug"] + debug = [i.msg for i in tctx.master.logs if i.level == "debug"] assert debug == [ - ('debug', 'a start'), - ('debug', 'a configure'), - ('debug', 'a running'), + 'a start', + 'a configure', + 'a running', - ('debug', 'b start'), - ('debug', 'b configure'), - ('debug', 'b running'), + 'b start', + 'b configure', + 'b running', - ('debug', 'c start'), - ('debug', 'c configure'), - ('debug', 'c running'), + 'c start', + 'c configure', + 'c running', ] tctx.master.logs = [] tctx.configure( @@ -279,8 +277,7 @@ class TestScriptLoader: "%s %s" % (rec, "b"), ] ) - debug = [(i[0], i[1]) for i in tctx.master.logs if i[0] == "debug"] - # No events, only order has changed + debug = [i.msg for i in tctx.master.logs if i.level == "debug"] assert debug == [] tctx.master.logs = [] @@ -291,11 +288,11 @@ class TestScriptLoader: "%s %s" % (rec, "a"), ] ) - debug = [(i[0], i[1]) for i in tctx.master.logs if i[0] == "debug"] + debug = [i.msg for i in tctx.master.logs if i.level == "debug"] assert debug == [ - ('debug', 'c done'), - ('debug', 'b done'), - ('debug', 'x start'), - ('debug', 'x configure'), - ('debug', 'x running'), + 'c done', + 'b done', + 'x start', + 'x configure', + 'x running', ] diff --git a/test/mitmproxy/proxy/test_server.py b/test/mitmproxy/proxy/test_server.py index 16efe415..ea7b69a3 100644 --- a/test/mitmproxy/proxy/test_server.py +++ b/test/mitmproxy/proxy/test_server.py @@ -250,17 +250,12 @@ class TestHTTP(tservers.HTTPProxyTest, CommonMixin): assert p.request(req) def test_get_connection_switching(self): - def switched(l): - for i in l: - if "serverdisconnect" in i: - return True - req = "get:'%s/p/200:b@1'" p = self.pathoc() with p.connect(): assert p.request(req % self.server.urlbase) assert p.request(req % self.server2.urlbase) - assert switched(self.proxy.tlog) + assert self.proxy.tmaster.has_log("serverdisconnect") def test_blank_leading_line(self): p = self.pathoc() @@ -602,7 +597,7 @@ class TestHttps2Http(tservers.ReverseProxyTest): p = self.pathoc(ssl=True, sni="example.com") with p.connect(): assert p.request("get:'/p/200'").status_code == 200 - assert all("Error in handle_sni" not in msg for msg in self.proxy.tlog) + assert not self.proxy.tmaster.has_log("error in handle_sni") def test_http(self): p = self.pathoc(ssl=False) @@ -877,8 +872,7 @@ class TestServerConnect(tservers.HTTPProxyTest): def test_unnecessary_serverconnect(self): """A replayed/fake response with no upstream_cert should not connect to an upstream server""" assert self.pathod("200").status_code == 200 - for msg in self.proxy.tmaster.tlog: - assert "serverconnect" not in msg + assert not self.proxy.tmaster.has_log("serverconnect") class MasterKillRequest(tservers.TestMaster): diff --git a/test/mitmproxy/script/test_concurrent.py b/test/mitmproxy/script/test_concurrent.py index 206482e2..0e397b8f 100644 --- a/test/mitmproxy/script/test_concurrent.py +++ b/test/mitmproxy/script/test_concurrent.py @@ -43,7 +43,7 @@ class TestConcurrent(tservers.MasterTest): ) ) sc.start(tctx.options) - assert "decorator not supported" in tctx.master.logs[0][1] + assert tctx.master.has_log("decorator not supported") def test_concurrent_class(self): with taddons.context() as tctx: diff --git a/test/mitmproxy/test_controller.py b/test/mitmproxy/test_controller.py index 6acd3e54..5f0e2d64 100644 --- a/test/mitmproxy/test_controller.py +++ b/test/mitmproxy/test_controller.py @@ -7,6 +7,7 @@ from mitmproxy.exceptions import Kill, ControlException from mitmproxy import controller from mitmproxy import master from mitmproxy import proxy +from mitmproxy.test import taddons class TMsg: @@ -15,22 +16,18 @@ class TMsg: class TestMaster: def test_simple(self): - class DummyMaster(master.Master): - @controller.handler + class tAddon: def log(self, _): - m.should_exit.set() - - def tick(self, timeout): - # Speed up test - super().tick(0) - - m = DummyMaster(None, proxy.DummyServer(None)) - assert not m.should_exit.is_set() - msg = TMsg() - msg.reply = controller.DummyReply() - m.event_queue.put(("log", msg)) - m.run() - assert m.should_exit.is_set() + ctx.master.should_exit.set() + + with taddons.context() as ctx: + ctx.master.addons.add(tAddon()) + assert not ctx.master.should_exit.is_set() + msg = TMsg() + msg.reply = controller.DummyReply() + ctx.master.event_queue.put(("log", msg)) + ctx.master.run() + assert ctx.master.should_exit.is_set() def test_server_simple(self): m = master.Master(None, proxy.DummyServer(None)) diff --git a/test/mitmproxy/tservers.py b/test/mitmproxy/tservers.py index 0f34e37e..9568976e 100644 --- a/test/mitmproxy/tservers.py +++ b/test/mitmproxy/tservers.py @@ -6,7 +6,6 @@ import sys import mitmproxy.platform from mitmproxy.proxy.config import ProxyConfig from mitmproxy.proxy.server import ProxyServer -from mitmproxy import master from mitmproxy import controller from mitmproxy import options from mitmproxy import exceptions @@ -17,6 +16,7 @@ import pathod.pathoc from mitmproxy.test import tflow from mitmproxy.test import tutils +from mitmproxy.test import taddons class MasterTest: @@ -68,11 +68,11 @@ class TestState: # self.flows.append(f) -class TestMaster(master.Master): +class TestMaster(taddons.RecordingMaster): def __init__(self, opts, config): s = ProxyServer(config) - master.Master.__init__(self, opts, s) + super().__init__(opts, s) def clear_addons(self, addons): self.addons.clear() @@ -82,16 +82,9 @@ class TestMaster(master.Master): self.addons.configure_all(self.options, self.options.keys()) self.addons.trigger("running") - def clear_log(self): - self.tlog = [] - def reset(self, addons): self.clear_addons(addons) - self.clear_log() - - @controller.handler - def log(self, e): - self.tlog.append(e.msg) + self.clear() class ProxyThread(threading.Thread): @@ -111,7 +104,7 @@ class ProxyThread(threading.Thread): @property def tlog(self): - return self.tmaster.tlog + return self.tmaster.logs def run(self): self.tmaster.run() -- cgit v1.2.3 From 3de982900381b9d3743e65defbfa413453cd3836 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Thu, 16 Mar 2017 14:00:10 +1300 Subject: Remove last handlers in test suite While we're here, make checking that there are NO logs on the proxy more efficient by setting a short timeout. This reduces total test suite running time by 15% on my system. --- test/mitmproxy/proxy/test_server.py | 56 ++++++++++++++----------------------- 1 file changed, 21 insertions(+), 35 deletions(-) diff --git a/test/mitmproxy/proxy/test_server.py b/test/mitmproxy/proxy/test_server.py index ea7b69a3..6f134b72 100644 --- a/test/mitmproxy/proxy/test_server.py +++ b/test/mitmproxy/proxy/test_server.py @@ -726,13 +726,12 @@ class TestProxySSL(tservers.HTTPProxyTest): assert not first_flow.server_conn.via -class MasterRedirectRequest(tservers.TestMaster): - redirect_port = None # Set by TestRedirectRequest +class ARedirectRequest: + def __init__(self, redirect_port): + self.redirect_port = redirect_port - @controller.handler def request(self, f): if f.request.path == "/p/201": - # This part should have no impact, but it should also not cause any exceptions. addr = f.live.server_conn.address addr2 = ("127.0.0.1", self.redirect_port) @@ -741,17 +740,13 @@ class MasterRedirectRequest(tservers.TestMaster): # This is the actual redirection. f.request.port = self.redirect_port - super().request(f) - @controller.handler def response(self, f): f.response.content = bytes(f.client_conn.address[1]) f.response.headers["server-conn-id"] = str(f.server_conn.source_address[1]) - super().response(f) class TestRedirectRequest(tservers.HTTPProxyTest): - masterclass = MasterRedirectRequest ssl = True def test_redirect(self): @@ -764,7 +759,7 @@ class TestRedirectRequest(tservers.HTTPProxyTest): This test verifies that the original destination is restored for the third request. """ - self.master.redirect_port = self.server2.port + self.proxy.tmaster.addons.add(ARedirectRequest(self.server2.port)) p = self.pathoc() with p.connect(): @@ -773,13 +768,13 @@ class TestRedirectRequest(tservers.HTTPProxyTest): r1 = p.request("get:'/p/200'") assert r1.status_code == 200 assert self.server.last_log() - assert not self.server2.last_log() + assert not self.server2.expect_log(1, 0.5) self.server.clear_log() self.server2.clear_log() r2 = p.request("get:'/p/201'") assert r2.status_code == 201 - assert not self.server.last_log() + assert not self.server.expect_log(1, 0.5) assert self.server2.last_log() self.server.clear_log() @@ -787,25 +782,23 @@ class TestRedirectRequest(tservers.HTTPProxyTest): r3 = p.request("get:'/p/202'") assert r3.status_code == 202 assert self.server.last_log() - assert not self.server2.last_log() + assert not self.server2.expect_log(1, 0.5) assert r1.content == r2.content == r3.content -class MasterStreamRequest(tservers.TestMaster): +class AStreamRequest: """ Enables the stream flag on the flow for all requests """ - @controller.handler def responseheaders(self, f): f.response.stream = True class TestStreamRequest(tservers.HTTPProxyTest): - masterclass = MasterStreamRequest - def test_stream_simple(self): + self.proxy.tmaster.addons.add(AStreamRequest()) p = self.pathoc() with p.connect(): # a request with 100k of data but without content-length @@ -814,6 +807,7 @@ class TestStreamRequest(tservers.HTTPProxyTest): assert len(r1.content) > 100000 def test_stream_multiple(self): + self.proxy.tmaster.addons.add(AStreamRequest()) p = self.pathoc() with p.connect(): # simple request with streaming turned on @@ -825,6 +819,7 @@ class TestStreamRequest(tservers.HTTPProxyTest): assert r1.status_code == 201 def test_stream_chunked(self): + self.proxy.tmaster.addons.add(AStreamRequest()) connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) connection.connect(("127.0.0.1", self.proxy.port)) fconn = connection.makefile("rb") @@ -845,22 +840,20 @@ class TestStreamRequest(tservers.HTTPProxyTest): connection.close() -class MasterFakeResponse(tservers.TestMaster): - @controller.handler +class AFakeResponse: def request(self, f): f.response = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp()) class TestFakeResponse(tservers.HTTPProxyTest): - masterclass = MasterFakeResponse def test_fake(self): + self.proxy.tmaster.addons.add(AFakeResponse()) f = self.pathod("200") assert "header-response" in f.headers class TestServerConnect(tservers.HTTPProxyTest): - masterclass = MasterFakeResponse ssl = True @classmethod @@ -871,38 +864,34 @@ class TestServerConnect(tservers.HTTPProxyTest): def test_unnecessary_serverconnect(self): """A replayed/fake response with no upstream_cert should not connect to an upstream server""" + self.proxy.tmaster.addons.add(AFakeResponse()) assert self.pathod("200").status_code == 200 assert not self.proxy.tmaster.has_log("serverconnect") -class MasterKillRequest(tservers.TestMaster): +class AKillRequest: - @controller.handler def request(self, f): f.reply.kill() class TestKillRequest(tservers.HTTPProxyTest): - masterclass = MasterKillRequest - def test_kill(self): + self.proxy.tmaster.addons.add(AKillRequest()) with pytest.raises(exceptions.HttpReadDisconnect): self.pathod("200") # Nothing should have hit the server - assert not self.server.last_log() - + assert not self.server.expect_log(1, 0.5) -class MasterKillResponse(tservers.TestMaster): - @controller.handler +class AKillResponse: def response(self, f): f.reply.kill() class TestKillResponse(tservers.HTTPProxyTest): - masterclass = MasterKillResponse - def test_kill(self): + self.proxy.tmaster.addons.add(AKillResponse()) with pytest.raises(exceptions.HttpReadDisconnect): self.pathod("200") # The server should have seen a request @@ -916,9 +905,7 @@ class TestTransparentResolveError(tservers.TransparentProxyTest): assert self.pathod("304").status_code == 502 -class MasterIncomplete(tservers.TestMaster): - - @controller.handler +class AIncomplete: def request(self, f): resp = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp()) resp.content = None @@ -926,9 +913,8 @@ class MasterIncomplete(tservers.TestMaster): class TestIncompleteResponse(tservers.HTTPProxyTest): - masterclass = MasterIncomplete - def test_incomplete(self): + self.proxy.tmaster.addons.add(AIncomplete()) assert self.pathod("200").status_code == 502 -- cgit v1.2.3 From c5e0dc64b9b367eae8f4af66a4917c738dd87569 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Thu, 16 Mar 2017 15:40:43 +1300 Subject: Rip out master handler mechanism All events are now handled by addons, and we no longer support any events on master. --- mitmproxy/addonmanager.py | 27 +++++++ mitmproxy/controller.py | 112 ++++++++-------------------- mitmproxy/flow.py | 7 +- mitmproxy/master.py | 99 +----------------------- mitmproxy/test/taddons.py | 17 ++--- test/mitmproxy/addons/test_disable_h2c.py | 1 - test/mitmproxy/addons/test_intercept.py | 1 - test/mitmproxy/proxy/test_server.py | 1 - test/mitmproxy/test_controller.py | 25 ++----- test/mitmproxy/test_examples.py | 22 +++--- test/mitmproxy/test_flow.py | 10 +-- test/mitmproxy/test_http.py | 2 - test/mitmproxy/test_taddons.py | 12 +++ test/mitmproxy/tools/console/test_master.py | 6 +- test/mitmproxy/tools/test_dump.py | 2 +- test/mitmproxy/tools/web/test_app.py | 3 - test/mitmproxy/tservers.py | 15 ++-- 17 files changed, 116 insertions(+), 246 deletions(-) create mode 100644 test/mitmproxy/test_taddons.py diff --git a/mitmproxy/addonmanager.py b/mitmproxy/addonmanager.py index 123f64b2..670c4f24 100644 --- a/mitmproxy/addonmanager.py +++ b/mitmproxy/addonmanager.py @@ -1,5 +1,6 @@ from mitmproxy import exceptions from mitmproxy import eventsequence +from mitmproxy import controller from . import ctx import pprint @@ -60,6 +61,32 @@ class AddonManager: def __str__(self): return pprint.pformat([str(i) for i in self.chain]) + def handle_lifecycle(self, name, message): + """ + Handle a lifecycle event. + """ + if not hasattr(message, "reply"): # pragma: no cover + raise exceptions.ControlException( + "Message %s has no reply attribute" % message + ) + + # We can use DummyReply objects multiple times. We only clear them up on + # the next handler so that we can access value and state in the + # meantime. + if isinstance(message.reply, controller.DummyReply): + message.reply.reset() + + self.trigger(name, message) + + if message.reply.state != "taken": + message.reply.take() + if not message.reply.has_message: + message.reply.ack() + message.reply.commit() + + if isinstance(message.reply, controller.DummyReply): + message.reply.mark_reset() + def invoke_addon(self, addon, name, *args, **kwargs): """ Invoke an event on an addon. This method must run within an diff --git a/mitmproxy/controller.py b/mitmproxy/controller.py index aa4dcbbc..63117ef0 100644 --- a/mitmproxy/controller.py +++ b/mitmproxy/controller.py @@ -1,4 +1,3 @@ -import functools import queue from mitmproxy import exceptions @@ -14,8 +13,8 @@ class Channel: def ask(self, mtype, m): """ - Decorate a message with a reply attribute, and send it to the - master. Then wait for a response. + Decorate a message with a reply attribute, and send it to the master. + Then wait for a response. Raises: exceptions.Kill: All connections should be closed immediately. @@ -36,83 +35,42 @@ class Channel: def tell(self, mtype, m): """ - Decorate a message with a dummy reply attribute, send it to the - master, then return immediately. + Decorate a message with a dummy reply attribute, send it to the master, + then return immediately. """ m.reply = DummyReply() self.q.put((mtype, m)) -def handler(f): - @functools.wraps(f) - def wrapper(master, message): - if not hasattr(message, "reply"): - raise exceptions.ControlException("Message %s has no reply attribute" % message) - - # DummyReplys may be reused multiple times. - # We only clear them up on the next handler so that we can access value and - # state in the meantime. - if isinstance(message.reply, DummyReply): - message.reply.reset() - - # The following ensures that inheritance with wrapped handlers in the - # base class works. If we're the first handler, then responsibility for - # acking is ours. If not, it's someone else's and we ignore it. - handling = False - # We're the first handler - ack responsibility is ours - if message.reply.state == "unhandled": - handling = True - message.reply.handle() - - with master.handlecontext(): - ret = f(master, message) - master.addons.trigger(f.__name__, message) - - # Reset the handled flag - it's common for us to feed the same object - # through handlers repeatedly, so we don't want this to persist across - # calls. - if handling and message.reply.state == "handled": - message.reply.take() - if not message.reply.has_message: - message.reply.ack() - message.reply.commit() - - # DummyReplys may be reused multiple times. - if isinstance(message.reply, DummyReply): - message.reply.mark_reset() - return ret - # Mark this function as a handler wrapper - wrapper.__dict__["__handler"] = True - return wrapper - - NO_REPLY = object() # special object we can distinguish from a valid "None" reply. class Reply: """ - Messages sent through a channel are decorated with a "reply" attribute. - This object is used to respond to the message through the return - channel. + Messages sent through a channel are decorated with a "reply" attribute. This + object is used to respond to the message through the return channel. """ def __init__(self, obj): self.obj = obj self.q = queue.Queue() # type: queue.Queue - self._state = "unhandled" # "unhandled" -> "handled" -> "taken" -> "committed" - self.value = NO_REPLY # holds the reply value. May change before things are actually commited. + self._state = "start" # "start" -> "taken" -> "committed" + + # Holds the reply value. May change before things are actually commited. + self.value = NO_REPLY @property def state(self): """ - The state the reply is currently in. A normal reply object goes sequentially through the following lifecycle: + The state the reply is currently in. A normal reply object goes + sequentially through the following lifecycle: - 1. unhandled: Initial State. - 2. handled: The reply object has been handled by the topmost handler function. - 3. taken: The reply object has been taken to be commited. - 4. committed: The reply has been sent back to the requesting party. + 1. start: Initial State. + 2. taken: The reply object has been taken to be commited. + 3. committed: The reply has been sent back to the requesting party. - This attribute is read-only and can only be modified by calling one of state transition functions. + This attribute is read-only and can only be modified by calling one of + state transition functions. """ return self._state @@ -120,47 +78,43 @@ class Reply: def has_message(self): return self.value != NO_REPLY - def handle(self): - """ - Reply are handled by controller.handlers, which may be nested. The first handler takes - responsibility and handles the reply. - """ - if self.state != "unhandled": - raise exceptions.ControlException("Reply is {}, but expected it to be unhandled.".format(self.state)) - self._state = "handled" - def take(self): """ Scripts or other parties make "take" a reply out of a normal flow. For example, intercepted flows are taken out so that the connection thread does not proceed. """ - if self.state != "handled": - raise exceptions.ControlException("Reply is {}, but expected it to be handled.".format(self.state)) + if self.state != "start": + raise exceptions.ControlException( + "Reply is {}, but expected it to be start.".format(self.state) + ) self._state = "taken" def commit(self): """ - Ultimately, messages are commited. This is done either automatically by the handler - if the message is not taken or manually by the entity which called .take(). + Ultimately, messages are commited. This is done either automatically by + if the message is not taken or manually by the entity which called + .take(). """ if self.state != "taken": - raise exceptions.ControlException("Reply is {}, but expected it to be taken.".format(self.state)) + raise exceptions.ControlException( + "Reply is {}, but expected it to be taken.".format(self.state) + ) if not self.has_message: raise exceptions.ControlException("There is no reply message.") self._state = "committed" self.q.put(self.value) def ack(self, force=False): + if self.state not in {"start", "taken"}: + raise exceptions.ControlException( + "Reply is {}, but expected it to be start or taken.".format(self.state) + ) self.send(self.obj, force) def kill(self, force=False): self.send(exceptions.Kill, force) def send(self, msg, force=False): - if self.state not in ("handled", "taken"): - raise exceptions.ControlException( - "Reply is {}, did not expect a call to .send().".format(self.state) - ) if self.has_message and not force: raise exceptions.ControlException("There is already a reply message.") self.value = msg @@ -174,7 +128,7 @@ class Reply: class DummyReply(Reply): """ A reply object that is not connected to anything. In contrast to regular - Reply objects, DummyReply objects are reset to "unhandled" at the end of an + Reply objects, DummyReply objects are reset to "start" at the end of an handler so that they can be used multiple times. Useful when we need an object to seem like it has a channel, and during testing. """ @@ -189,7 +143,7 @@ class DummyReply(Reply): def reset(self): if self._should_reset: - self._state = "unhandled" + self._state = "start" self.value = NO_REPLY def __del__(self): diff --git a/mitmproxy/flow.py b/mitmproxy/flow.py index bcc55559..294aba26 100644 --- a/mitmproxy/flow.py +++ b/mitmproxy/flow.py @@ -144,7 +144,7 @@ class Flow(stateobject.StateObject): @property def killable(self): - return self.reply and self.reply.state in {"handled", "taken"} + return self.reply and self.reply.state == "taken" def kill(self): """ @@ -152,8 +152,9 @@ class Flow(stateobject.StateObject): """ self.error = Error("Connection killed") self.intercepted = False - # reply.state should only be "handled" or "taken" here. - # if none of this is the case, .take() will raise an exception. + + # reply.state should be "taken" here, or .take() will raise an + # exception. if self.reply.state != "taken": self.reply.take() self.reply.kill(force=True) diff --git a/mitmproxy/master.py b/mitmproxy/master.py index 887a9240..946b25a4 100644 --- a/mitmproxy/master.py +++ b/mitmproxy/master.py @@ -93,18 +93,7 @@ class Master: raise exceptions.ControlException( "Unknown event %s" % repr(mtype) ) - handle_func = getattr(self, mtype) - if not callable(handle_func): - raise exceptions.ControlException( - "Handler %s not callable" % mtype - ) - if not handle_func.__dict__.get("__handler"): - raise exceptions.ControlException( - "Handler function %s is not decorated with controller.handler" % ( - handle_func - ) - ) - handle_func(obj) + self.addons.handle_lifecycle(mtype, obj) self.event_queue.task_done() changed = True except queue.Empty: @@ -143,7 +132,7 @@ class Master: f.request.scheme = self.server.config.upstream_server.scheme f.reply = controller.DummyReply() for e, o in eventsequence.iterate(f): - getattr(self, e)(o) + self.addons.handle_lifecycle(e, o) def replay_request( self, @@ -199,87 +188,3 @@ class Master: if block: rt.join() return rt - - @controller.handler - def log(self, l): - pass - - @controller.handler - def clientconnect(self, root_layer): - pass - - @controller.handler - def clientdisconnect(self, root_layer): - pass - - @controller.handler - def serverconnect(self, server_conn): - pass - - @controller.handler - def serverdisconnect(self, server_conn): - pass - - @controller.handler - def next_layer(self, top_layer): - pass - - @controller.handler - def http_connect(self, f): - pass - - @controller.handler - def error(self, f): - pass - - @controller.handler - def requestheaders(self, f): - pass - - @controller.handler - def request(self, f): - pass - - @controller.handler - def responseheaders(self, f): - pass - - @controller.handler - def response(self, f): - pass - - @controller.handler - def websocket_handshake(self, f): - pass - - @controller.handler - def websocket_start(self, flow): - pass - - @controller.handler - def websocket_message(self, flow): - pass - - @controller.handler - def websocket_error(self, flow): - pass - - @controller.handler - def websocket_end(self, flow): - pass - - @controller.handler - def tcp_start(self, flow): - pass - - @controller.handler - def tcp_message(self, flow): - pass - - @controller.handler - def tcp_error(self, flow): - pass - - @controller.handler - def tcp_end(self, flow): - pass diff --git a/mitmproxy/test/taddons.py b/mitmproxy/test/taddons.py index 29ae7aa9..8bc174c7 100644 --- a/mitmproxy/test/taddons.py +++ b/mitmproxy/test/taddons.py @@ -3,29 +3,26 @@ import contextlib import mitmproxy.master import mitmproxy.options from mitmproxy import proxy +from mitmproxy import addonmanager from mitmproxy import eventsequence -class _AddonWrapper: - def __init__(self, master, addons): - self.master = master - self.addons = addons +class TestAddons(addonmanager.AddonManager): + def __init__(self, master): + super().__init__(master) def trigger(self, event, *args, **kwargs): if event == "log": self.master.logs.append(args[0]) else: self.master.events.append((event, args, kwargs)) - return self.addons.trigger(event, *args, **kwargs) - - def __getattr__(self, attr): - return getattr(self.addons, attr) + super().trigger(event, *args, **kwargs) class RecordingMaster(mitmproxy.master.Master): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.addons = _AddonWrapper(self, self.addons) + self.addons = TestAddons(self) self.events = [] self.logs = [] @@ -76,7 +73,7 @@ class context: Cycles the flow through the events for the flow. Stops if a reply is taken (as in flow interception). """ - f.reply._state = "handled" + f.reply._state = "start" for evt, arg in eventsequence.iterate(f): h = getattr(addon, evt, None) if h: diff --git a/test/mitmproxy/addons/test_disable_h2c.py b/test/mitmproxy/addons/test_disable_h2c.py index d4df8390..cf20a368 100644 --- a/test/mitmproxy/addons/test_disable_h2c.py +++ b/test/mitmproxy/addons/test_disable_h2c.py @@ -31,7 +31,6 @@ class TestDisableH2CleartextUpgrade: b = io.BytesIO(b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n") f = tflow.tflow() f.request = http.HTTPRequest.wrap(http1.read_request(b)) - f.reply.handle() f.intercept() a.request(f) diff --git a/test/mitmproxy/addons/test_intercept.py b/test/mitmproxy/addons/test_intercept.py index 465e6433..f436a817 100644 --- a/test/mitmproxy/addons/test_intercept.py +++ b/test/mitmproxy/addons/test_intercept.py @@ -29,6 +29,5 @@ def test_simple(): assert not f.intercepted f = tflow.tflow(resp=True) - f.reply._state = "handled" r.response(f) assert f.intercepted diff --git a/test/mitmproxy/proxy/test_server.py b/test/mitmproxy/proxy/test_server.py index 6f134b72..447b15a5 100644 --- a/test/mitmproxy/proxy/test_server.py +++ b/test/mitmproxy/proxy/test_server.py @@ -5,7 +5,6 @@ import pytest from unittest import mock from mitmproxy.test import tutils -from mitmproxy import controller from mitmproxy import options from mitmproxy.addons import script from mitmproxy.addons import proxyauth diff --git a/test/mitmproxy/test_controller.py b/test/mitmproxy/test_controller.py index 5f0e2d64..ccc8bf35 100644 --- a/test/mitmproxy/test_controller.py +++ b/test/mitmproxy/test_controller.py @@ -60,7 +60,6 @@ class TestChannel: def reply(): m, obj = q.get() assert m == "test" - obj.reply.handle() obj.reply.send(42) obj.reply.take() obj.reply.commit() @@ -82,10 +81,7 @@ class TestChannel: class TestReply: def test_simple(self): reply = controller.Reply(42) - assert reply.state == "unhandled" - - reply.handle() - assert reply.state == "handled" + assert reply.state == "start" reply.send("foo") assert reply.value == "foo" @@ -101,7 +97,6 @@ class TestReply: def test_kill(self): reply = controller.Reply(43) - reply.handle() reply.kill() reply.take() reply.commit() @@ -109,7 +104,6 @@ class TestReply: def test_ack(self): reply = controller.Reply(44) - reply.handle() reply.ack() reply.take() reply.commit() @@ -117,7 +111,6 @@ class TestReply: def test_reply_none(self): reply = controller.Reply(45) - reply.handle() reply.send(None) reply.take() reply.commit() @@ -125,7 +118,6 @@ class TestReply: def test_commit_no_reply(self): reply = controller.Reply(46) - reply.handle() reply.take() with pytest.raises(ControlException): reply.commit() @@ -134,7 +126,6 @@ class TestReply: def test_double_send(self): reply = controller.Reply(47) - reply.handle() reply.send(1) with pytest.raises(ControlException): reply.send(2) @@ -142,12 +133,11 @@ class TestReply: reply.commit() def test_state_transitions(self): - states = {"unhandled", "handled", "taken", "committed"} + states = {"start", "taken", "committed"} accept = { - "handle": {"unhandled"}, - "take": {"handled"}, + "take": {"start"}, "commit": {"taken"}, - "ack": {"handled", "taken"}, + "ack": {"start", "taken"}, } for fn, ok in accept.items(): for state in states: @@ -166,7 +156,6 @@ class TestReply: reply = controller.Reply(47) with pytest.raises(ControlException): reply.__del__() - reply.handle() reply.ack() reply.take() reply.commit() @@ -176,24 +165,22 @@ class TestDummyReply: def test_simple(self): reply = controller.DummyReply() for _ in range(2): - reply.handle() reply.ack() reply.take() reply.commit() reply.mark_reset() reply.reset() - assert reply.state == "unhandled" + assert reply.state == "start" def test_reset(self): reply = controller.DummyReply() - reply.handle() reply.ack() reply.take() reply.commit() reply.mark_reset() assert reply.state == "committed" reply.reset() - assert reply.state == "unhandled" + assert reply.state == "start" def test_del(self): reply = controller.DummyReply() diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py index 56692364..030f2c4e 100644 --- a/test/mitmproxy/test_examples.py +++ b/test/mitmproxy/test_examples.py @@ -41,7 +41,7 @@ class TestScripts(tservers.MasterTest): def test_add_header(self): m, _ = tscript("simple/add_header.py") f = tflow.tflow(resp=tutils.tresp()) - m.response(f) + m.addons.handle_lifecycle("response", f) assert f.response.headers["newheader"] == "foo" def test_custom_contentviews(self): @@ -56,7 +56,7 @@ class TestScripts(tservers.MasterTest): m, sc = tscript("simple/modify_body_inject_iframe.py", "http://example.org/evil_iframe") f = tflow.tflow(resp=tutils.tresp(content=b"mitmproxy")) - m.response(f) + m.addons.handle_lifecycle("response", f) content = f.response.content assert b'iframe' in content and b'evil_iframe' in content @@ -65,41 +65,41 @@ class TestScripts(tservers.MasterTest): form_header = Headers(content_type="application/x-www-form-urlencoded") f = tflow.tflow(req=tutils.treq(headers=form_header)) - m.request(f) + m.addons.handle_lifecycle("request", f) assert f.request.urlencoded_form["mitmproxy"] == "rocks" f.request.headers["content-type"] = "" - m.request(f) + m.addons.handle_lifecycle("request", f) assert list(f.request.urlencoded_form.items()) == [("foo", "bar")] def test_modify_querystring(self): m, sc = tscript("simple/modify_querystring.py") f = tflow.tflow(req=tutils.treq(path="/search?q=term")) - m.request(f) + m.addons.handle_lifecycle("request", f) assert f.request.query["mitmproxy"] == "rocks" f.request.path = "/" - m.request(f) + m.addons.handle_lifecycle("request", f) assert f.request.query["mitmproxy"] == "rocks" def test_arguments(self): m, sc = tscript("simple/script_arguments.py", "mitmproxy rocks") f = tflow.tflow(resp=tutils.tresp(content=b"I <3 mitmproxy")) - m.response(f) + m.addons.handle_lifecycle("response", f) assert f.response.content == b"I <3 rocks" def test_redirect_requests(self): m, sc = tscript("simple/redirect_requests.py") f = tflow.tflow(req=tutils.treq(host="example.org")) - m.request(f) + m.addons.handle_lifecycle("request", f) assert f.request.host == "mitmproxy.org" def test_send_reply_from_proxy(self): m, sc = tscript("simple/send_reply_from_proxy.py") f = tflow.tflow(req=tutils.treq(host="example.com", port=80)) - m.request(f) + m.addons.handle_lifecycle("request", f) assert f.response.content == b"Hello World" def test_dns_spoofing(self): @@ -109,13 +109,13 @@ class TestScripts(tservers.MasterTest): host_header = Headers(host=original_host) f = tflow.tflow(req=tutils.treq(headers=host_header, port=80)) - m.requestheaders(f) + m.addons.handle_lifecycle("requestheaders", f) # Rewrite by reverse proxy mode f.request.scheme = "https" f.request.port = 443 - m.request(f) + m.addons.handle_lifecycle("request", f) assert f.request.scheme == "http" assert f.request.port == 80 diff --git a/test/mitmproxy/test_flow.py b/test/mitmproxy/test_flow.py index 4f87a6ae..630fc7e4 100644 --- a/test/mitmproxy/test_flow.py +++ b/test/mitmproxy/test_flow.py @@ -122,19 +122,19 @@ class TestFlowMaster: fm = master.Master(None, DummyServer()) fm.addons.add(s) f = tflow.tflow(req=None) - fm.clientconnect(f.client_conn) + fm.addons.handle_lifecycle("clientconnect", f.client_conn) f.request = http.HTTPRequest.wrap(mitmproxy.test.tutils.treq()) - fm.request(f) + fm.addons.handle_lifecycle("request", f) assert len(s.flows) == 1 f.response = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp()) - fm.response(f) + fm.addons.handle_lifecycle("response", f) assert len(s.flows) == 1 - fm.clientdisconnect(f.client_conn) + fm.addons.handle_lifecycle("clientdisconnect", f.client_conn) f.error = flow.Error("msg") - fm.error(f) + fm.addons.handle_lifecycle("error", f) fm.shutdown() diff --git a/test/mitmproxy/test_http.py b/test/mitmproxy/test_http.py index 889eb0a7..aa283530 100644 --- a/test/mitmproxy/test_http.py +++ b/test/mitmproxy/test_http.py @@ -175,7 +175,6 @@ class TestHTTPFlow: def test_kill(self): f = tflow.tflow() - f.reply.handle() f.intercept() assert f.killable f.kill() @@ -184,7 +183,6 @@ class TestHTTPFlow: def test_resume(self): f = tflow.tflow() - f.reply.handle() f.intercept() assert f.reply.state == "taken" f.resume() diff --git a/test/mitmproxy/test_taddons.py b/test/mitmproxy/test_taddons.py new file mode 100644 index 00000000..1e42141c --- /dev/null +++ b/test/mitmproxy/test_taddons.py @@ -0,0 +1,12 @@ + +from mitmproxy.test import taddons +from mitmproxy import ctx + + +def test_recordingmaster(): + with taddons.context() as tctx: + assert not tctx.master.has_log("nonexistent") + assert not tctx.master.has_event("nonexistent") + ctx.log.error("foo") + assert not tctx.master.has_log("foo", level="debug") + assert tctx.master.has_log("foo", level="error") diff --git a/test/mitmproxy/tools/console/test_master.py b/test/mitmproxy/tools/console/test_master.py index 45908450..44b9ff3f 100644 --- a/test/mitmproxy/tools/console/test_master.py +++ b/test/mitmproxy/tools/console/test_master.py @@ -52,11 +52,11 @@ class TestMaster(tservers.MasterTest): """regression test for https://github.com/mitmproxy/mitmproxy/issues/1605""" m = self.mkmaster(intercept="~b bar") f = tflow.tflow(req=tutils.treq(content=b"foo")) - m.request(f) + m.addons.handle_lifecycle("request", f) assert not m.view[0].intercepted f = tflow.tflow(req=tutils.treq(content=b"bar")) - m.request(f) + m.addons.handle_lifecycle("request", f) assert m.view[1].intercepted f = tflow.tflow(resp=tutils.tresp(content=b"bar")) - m.request(f) + m.addons.handle_lifecycle("request", f) assert m.view[2].intercepted diff --git a/test/mitmproxy/tools/test_dump.py b/test/mitmproxy/tools/test_dump.py index 810fcaaa..8e2fa5b2 100644 --- a/test/mitmproxy/tools/test_dump.py +++ b/test/mitmproxy/tools/test_dump.py @@ -20,7 +20,7 @@ class TestDumpMaster(tservers.MasterTest): m = self.mkmaster(None) ent = log.LogEntry("foo", "error") ent.reply = controller.DummyReply() - m.log(ent) + m.addons.trigger("log", ent) assert m.errorcheck.has_errored @pytest.mark.parametrize("termlog", [False, True]) diff --git a/test/mitmproxy/tools/web/test_app.py b/test/mitmproxy/tools/web/test_app.py index 00dc2c7c..e3d5dc44 100644 --- a/test/mitmproxy/tools/web/test_app.py +++ b/test/mitmproxy/tools/web/test_app.py @@ -83,7 +83,6 @@ class TestApp(tornado.testing.AsyncHTTPTestCase): def test_resume(self): for f in self.view: - f.reply.handle() f.intercept() assert self.fetch( @@ -95,7 +94,6 @@ class TestApp(tornado.testing.AsyncHTTPTestCase): def test_kill(self): for f in self.view: f.backup() - f.reply.handle() f.intercept() assert self.fetch("/flows/42/kill", method="POST").code == 200 @@ -109,7 +107,6 @@ class TestApp(tornado.testing.AsyncHTTPTestCase): f = self.view.get_by_id("42") assert f - f.reply.handle() assert self.fetch("/flows/42", method="DELETE").code == 200 assert not self.view.get_by_id("42") diff --git a/test/mitmproxy/tservers.py b/test/mitmproxy/tservers.py index 9568976e..b737b82a 100644 --- a/test/mitmproxy/tservers.py +++ b/test/mitmproxy/tservers.py @@ -10,10 +10,10 @@ from mitmproxy import controller from mitmproxy import options from mitmproxy import exceptions from mitmproxy import io -from mitmproxy import http import pathod.test import pathod.pathoc +from mitmproxy import eventsequence from mitmproxy.test import tflow from mitmproxy.test import tutils from mitmproxy.test import taddons @@ -23,15 +23,10 @@ class MasterTest: def cycle(self, master, content): f = tflow.tflow(req=tutils.treq(content=content)) - master.clientconnect(f.client_conn) - master.serverconnect(f.server_conn) - master.request(f) - if not f.error: - f.response = http.HTTPResponse.wrap( - tutils.tresp(content=content) - ) - master.response(f) - master.clientdisconnect(f) + master.addons.handle_lifecycle("clientconnect", f.client_conn) + for i in eventsequence.iterate(f): + master.addons.handle_lifecycle(*i) + master.addons.handle_lifecycle("clientdisconnect", f.client_conn) return f def dummy_cycle(self, master, n, content): -- cgit v1.2.3