aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/mitmproxy/addons/test_check_alpn.py4
-rw-r--r--test/mitmproxy/addons/test_check_ca.py2
-rw-r--r--test/mitmproxy/addons/test_disable_h2c.py1
-rw-r--r--test/mitmproxy/addons/test_dumper.py2
-rw-r--r--test/mitmproxy/addons/test_intercept.py1
-rw-r--r--test/mitmproxy/addons/test_script.py51
-rw-r--r--test/mitmproxy/proxy/test_server.py69
-rw-r--r--test/mitmproxy/script/test_concurrent.py2
-rw-r--r--test/mitmproxy/test_controller.py50
-rw-r--r--test/mitmproxy/test_examples.py22
-rw-r--r--test/mitmproxy/test_flow.py10
-rw-r--r--test/mitmproxy/test_http.py2
-rw-r--r--test/mitmproxy/test_taddons.py12
-rw-r--r--test/mitmproxy/tools/console/test_master.py6
-rw-r--r--test/mitmproxy/tools/test_dump.py4
-rw-r--r--test/mitmproxy/tools/web/test_app.py3
-rw-r--r--test/mitmproxy/tservers.py32
17 files changed, 113 insertions, 160 deletions
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_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_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_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/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..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
@@ -250,17 +249,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 +596,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)
@@ -731,13 +725,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)
@@ -746,17 +739,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):
@@ -769,7 +758,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():
@@ -778,13 +767,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()
@@ -792,25 +781,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
@@ -819,6 +806,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
@@ -830,6 +818,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")
@@ -850,22 +839,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
@@ -876,39 +863,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
- for msg in self.proxy.tmaster.tlog:
- assert "serverconnect" not in msg
+ 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
@@ -922,9 +904,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
@@ -932,9 +912,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
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..ccc8bf35 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()
+ ctx.master.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()
+ 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))
@@ -63,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()
@@ -85,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"
@@ -104,7 +97,6 @@ class TestReply:
def test_kill(self):
reply = controller.Reply(43)
- reply.handle()
reply.kill()
reply.take()
reply.commit()
@@ -112,7 +104,6 @@ class TestReply:
def test_ack(self):
reply = controller.Reply(44)
- reply.handle()
reply.ack()
reply.take()
reply.commit()
@@ -120,7 +111,6 @@ class TestReply:
def test_reply_none(self):
reply = controller.Reply(45)
- reply.handle()
reply.send(None)
reply.take()
reply.commit()
@@ -128,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()
@@ -137,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)
@@ -145,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:
@@ -169,7 +156,6 @@ class TestReply:
reply = controller.Reply(47)
with pytest.raises(ControlException):
reply.__del__()
- reply.handle()
reply.ack()
reply.take()
reply.commit()
@@ -179,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"<html><body>mitmproxy</body></html>"))
- 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 624bf08f..8e2fa5b2 100644
--- a/test/mitmproxy/tools/test_dump.py
+++ b/test/mitmproxy/tools/test_dump.py
@@ -20,8 +20,8 @@ class TestDumpMaster(tservers.MasterTest):
m = self.mkmaster(None)
ent = log.LogEntry("foo", "error")
ent.reply = controller.DummyReply()
- m.log(ent)
- assert m.has_errored
+ m.addons.trigger("log", ent)
+ assert m.errorcheck.has_errored
@pytest.mark.parametrize("termlog", [False, True])
def test_addons_termlog(self, termlog):
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 0f34e37e..b737b82a 100644
--- a/test/mitmproxy/tservers.py
+++ b/test/mitmproxy/tservers.py
@@ -6,32 +6,27 @@ 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
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
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):
@@ -68,11 +63,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 +77,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 +99,7 @@ class ProxyThread(threading.Thread):
@property
def tlog(self):
- return self.tmaster.tlog
+ return self.tmaster.logs
def run(self):
self.tmaster.run()