aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/mitmproxy/mastertest.py4
-rw-r--r--test/mitmproxy/script/test_concurrent.py2
-rw-r--r--test/mitmproxy/test_controller.py131
-rw-r--r--test/mitmproxy/test_flow.py9
4 files changed, 120 insertions, 26 deletions
diff --git a/test/mitmproxy/mastertest.py b/test/mitmproxy/mastertest.py
index dcc0dc48..95597d2c 100644
--- a/test/mitmproxy/mastertest.py
+++ b/test/mitmproxy/mastertest.py
@@ -12,13 +12,11 @@ class MasterTest:
with master.handlecontext():
func = getattr(master, handler)
func(*message)
- if message:
- message[0].reply = controller.DummyReply()
def cycle(self, master, content):
f = tutils.tflow(req=netlib.tutils.treq(content=content))
l = proxy.Log("connect")
- l.reply = mock.MagicMock()
+ l.reply = controller.DummyReply()
master.log(l)
self.invoke(master, "clientconnect", f.client_conn)
self.invoke(master, "clientconnect", f.client_conn)
diff --git a/test/mitmproxy/script/test_concurrent.py b/test/mitmproxy/script/test_concurrent.py
index a5f76994..07ba1c57 100644
--- a/test/mitmproxy/script/test_concurrent.py
+++ b/test/mitmproxy/script/test_concurrent.py
@@ -29,7 +29,7 @@ class TestConcurrent(mastertest.MasterTest):
self.invoke(m, "request", f2)
start = time.time()
while time.time() - start < 5:
- if f1.reply.acked and f2.reply.acked:
+ if f1.reply.state == f2.reply.state == "committed":
return
raise ValueError("Script never acked")
diff --git a/test/mitmproxy/test_controller.py b/test/mitmproxy/test_controller.py
index 6d4b8fe6..8fe2453d 100644
--- a/test/mitmproxy/test_controller.py
+++ b/test/mitmproxy/test_controller.py
@@ -1,3 +1,4 @@
+from test.mitmproxy import tutils
from threading import Thread, Event
from mock import Mock
@@ -5,7 +6,7 @@ from mock import Mock
from mitmproxy import controller
from six.moves import queue
-from mitmproxy.exceptions import Kill
+from mitmproxy.exceptions import Kill, ControlException
from mitmproxy.proxy import DummyServer
from netlib.tutils import raises
@@ -55,7 +56,7 @@ class TestChannel(object):
def test_tell(self):
q = queue.Queue()
channel = controller.Channel(q, Event())
- m = Mock()
+ m = Mock(name="test_tell")
channel.tell("test", m)
assert q.get() == ("test", m)
assert m.reply
@@ -66,12 +67,15 @@ class TestChannel(object):
def reply():
m, obj = q.get()
assert m == "test"
+ obj.reply.handle()
obj.reply.send(42)
+ obj.reply.take()
+ obj.reply.commit()
Thread(target=reply).start()
channel = controller.Channel(q, Event())
- assert channel.ask("test", Mock()) == 42
+ assert channel.ask("test", Mock(name="test_ask_simple")) == 42
def test_ask_shutdown(self):
q = queue.Queue()
@@ -79,31 +83,122 @@ class TestChannel(object):
done.set()
channel = controller.Channel(q, done)
with raises(Kill):
- channel.ask("test", Mock())
-
-
-class TestDummyReply(object):
- def test_simple(self):
- reply = controller.DummyReply()
- assert not reply.acked
- reply.ack()
- assert reply.acked
+ channel.ask("test", Mock(name="test_ask_shutdown"))
class TestReply(object):
def test_simple(self):
reply = controller.Reply(42)
- assert not reply.acked
+ assert reply.state == "unhandled"
+
+ reply.handle()
+ assert reply.state == "handled"
+
reply.send("foo")
- assert reply.acked
+ assert reply.value == "foo"
+
+ reply.take()
+ assert reply.state == "taken"
+
+ with tutils.raises(queue.Empty):
+ reply.q.get_nowait()
+ reply.commit()
+ assert reply.state == "committed"
assert reply.q.get() == "foo"
- def test_default(self):
- reply = controller.Reply(42)
+ def test_kill(self):
+ reply = controller.Reply(43)
+ reply.handle()
+ reply.kill()
+ reply.take()
+ reply.commit()
+ assert reply.q.get() == Kill
+
+ def test_ack(self):
+ reply = controller.Reply(44)
+ reply.handle()
reply.ack()
- assert reply.q.get() == 42
+ reply.take()
+ reply.commit()
+ assert reply.q.get() == 44
def test_reply_none(self):
- reply = controller.Reply(42)
+ reply = controller.Reply(45)
+ reply.handle()
reply.send(None)
+ reply.take()
+ reply.commit()
assert reply.q.get() is None
+
+ def test_commit_no_reply(self):
+ reply = controller.Reply(46)
+ reply.handle()
+ reply.take()
+ with tutils.raises(ControlException):
+ reply.commit()
+ reply.ack()
+ reply.commit()
+
+ def test_double_send(self):
+ reply = controller.Reply(47)
+ reply.handle()
+ reply.send(1)
+ with tutils.raises(ControlException):
+ reply.send(2)
+ reply.take()
+ reply.commit()
+
+ def test_state_transitions(self):
+ states = {"unhandled", "handled", "taken", "committed"}
+ accept = {
+ "handle": {"unhandled"},
+ "take": {"handled"},
+ "commit": {"taken"},
+ "ack": {"handled", "taken"},
+ }
+ for fn, ok in accept.items():
+ for state in states:
+ r = controller.Reply(48)
+ r._state = state
+ if fn == "commit":
+ r.value = 49
+ if state in ok:
+ getattr(r, fn)()
+ else:
+ with tutils.raises(ControlException):
+ getattr(r, fn)()
+ r._state = "committed" # hide warnings on deletion
+
+ def test_del(self):
+ reply = controller.Reply(47)
+ with tutils.raises(ControlException):
+ reply.__del__()
+ reply.handle()
+ reply.ack()
+ reply.take()
+ reply.commit()
+
+
+class TestDummyReply(object):
+ def test_simple(self):
+ reply = controller.DummyReply()
+ for _ in range(2):
+ reply.handle()
+ reply.ack()
+ reply.take()
+ reply.commit()
+ reply.reset()
+ assert reply.state == "unhandled"
+
+ def test_reset(self):
+ reply = controller.DummyReply()
+ reply.handle()
+ reply.ack()
+ reply.take()
+ reply.commit()
+ reply.reset()
+ assert reply.state == "unhandled"
+
+ def test_del(self):
+ reply = controller.DummyReply()
+ reply.__del__() \ No newline at end of file
diff --git a/test/mitmproxy/test_flow.py b/test/mitmproxy/test_flow.py
index d4bf764c..256ee124 100644
--- a/test/mitmproxy/test_flow.py
+++ b/test/mitmproxy/test_flow.py
@@ -375,6 +375,7 @@ class TestHTTPFlow(object):
s = flow.State()
fm = flow.FlowMaster(None, None, s)
f = tutils.tflow()
+ f.reply.handle()
f.intercept(mock.Mock())
f.kill(fm)
for i in s.view:
@@ -385,6 +386,7 @@ class TestHTTPFlow(object):
fm = flow.FlowMaster(None, None, s)
f = tutils.tflow()
+ f.reply.handle()
f.intercept(fm)
s.killall(fm)
@@ -393,11 +395,11 @@ class TestHTTPFlow(object):
def test_accept_intercept(self):
f = tutils.tflow()
-
+ f.reply.handle()
f.intercept(mock.Mock())
- assert not f.reply.acked
+ assert f.reply.state == "taken"
f.accept_intercept(mock.Mock())
- assert f.reply.acked
+ assert f.reply.state == "committed"
def test_replace_unicode(self):
f = tutils.tflow(resp=True)
@@ -735,7 +737,6 @@ class TestFlowMaster:
fm.clientdisconnect(f.client_conn)
f.error = Error("msg")
- f.error.reply = controller.DummyReply()
fm.error(f)
fm.shutdown()