diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/mitmproxy/builtins/test_clientplayback.py | 37 | ||||
-rw-r--r-- | test/mitmproxy/mastertest.py | 13 | ||||
-rw-r--r-- | test/mitmproxy/test_dump.py | 1 | ||||
-rw-r--r-- | test/mitmproxy/test_flow.py | 59 | ||||
-rw-r--r-- | test/mitmproxy/test_server.py | 4 |
5 files changed, 54 insertions, 60 deletions
diff --git a/test/mitmproxy/builtins/test_clientplayback.py b/test/mitmproxy/builtins/test_clientplayback.py new file mode 100644 index 00000000..15702340 --- /dev/null +++ b/test/mitmproxy/builtins/test_clientplayback.py @@ -0,0 +1,37 @@ +import mock + +from mitmproxy.builtins import clientplayback +from mitmproxy import options + +from .. import tutils, mastertest + + +class TestClientPlayback: + def test_playback(self): + cp = clientplayback.ClientPlayback() + cp.configure(options.Options(), []) + assert cp.count() == 0 + f = tutils.tflow(resp=True) + cp.load([f]) + assert cp.count() == 1 + RP = "mitmproxy.protocol.http_replay.RequestReplayThread" + with mock.patch(RP) as rp: + assert not cp.current + with mastertest.mockctx(): + cp.tick() + rp.assert_called() + assert cp.current + + cp.keepserving = False + cp.flows = None + cp.current = None + with mock.patch("mitmproxy.controller.Master.shutdown") as sd: + with mastertest.mockctx(): + cp.tick() + sd.assert_called() + + def test_configure(self): + cp = clientplayback.ClientPlayback() + cp.configure( + options.Options(), [] + ) diff --git a/test/mitmproxy/mastertest.py b/test/mitmproxy/mastertest.py index 68d88ea1..a14fe02a 100644 --- a/test/mitmproxy/mastertest.py +++ b/test/mitmproxy/mastertest.py @@ -1,8 +1,10 @@ +import contextlib + from . import tutils import netlib.tutils from mitmproxy.flow import master -from mitmproxy import flow, proxy, models, controller +from mitmproxy import flow, proxy, models, controller, options class TestMaster: @@ -47,3 +49,12 @@ class RecordingMaster(master.FlowMaster): def add_log(self, e, level): self.event_log.append((level, e)) + + +@contextlib.contextmanager +def mockctx(): + state = flow.State() + o = options.Options(refresh_server_playback = True, keepserving=False) + m = RecordingMaster(o, proxy.DummyServer(o), state) + with m.handlecontext(): + yield diff --git a/test/mitmproxy/test_dump.py b/test/mitmproxy/test_dump.py index 9fd5ff57..06f39e9d 100644 --- a/test/mitmproxy/test_dump.py +++ b/test/mitmproxy/test_dump.py @@ -45,7 +45,6 @@ class TestDumpMaster(mastertest.MasterTest): m = dump.DumpMaster(None, o) f = tutils.tflow(err=True) m.error(f) - assert m.error(f) assert "error" in o.tfile.getvalue() def test_replay(self): diff --git a/test/mitmproxy/test_flow.py b/test/mitmproxy/test_flow.py index 91013efc..0fe45afb 100644 --- a/test/mitmproxy/test_flow.py +++ b/test/mitmproxy/test_flow.py @@ -37,39 +37,6 @@ def test_app_registry(): assert ar.get(r) -class TestClientPlaybackState: - - def test_tick(self): - first = tutils.tflow() - s = flow.State() - fm = flow.FlowMaster(None, None, s) - fm.start_client_playback([first, tutils.tflow()], True) - c = fm.client_playback - c.testing = True - - assert not c.done() - assert not s.flow_count() - assert c.count() == 2 - c.tick(fm) - assert s.flow_count() - assert c.count() == 1 - - c.tick(fm) - assert c.count() == 1 - - c.clear(c.current) - c.tick(fm) - assert c.count() == 0 - c.clear(c.current) - assert c.done() - - fm.state.clear() - fm.tick(timeout=0) - - fm.stop_client_playback() - assert not fm.client_playback - - class TestHTTPFlow(object): def test_copy(self): @@ -477,13 +444,13 @@ class TestFlowMaster: fm = flow.FlowMaster(None, None, s) f = tutils.tflow(resp=True) f.request.content = None - assert "missing" in fm.replay_request(f) + tutils.raises("missing", fm.replay_request, f) f.intercepted = True - assert "intercepting" in fm.replay_request(f) + tutils.raises("intercepted", fm.replay_request, f) f.live = True - assert "live" in fm.replay_request(f) + tutils.raises("live", fm.replay_request, f) def test_duplicate_flow(self): s = flow.State() @@ -521,26 +488,6 @@ class TestFlowMaster: fm.shutdown() - def test_client_playback(self): - s = flow.State() - - f = tutils.tflow(resp=True) - pb = [tutils.tflow(resp=True), f] - fm = flow.FlowMaster( - options.Options(), - DummyServer(ProxyConfig(options.Options())), - s - ) - assert not fm.start_client_playback(pb, False) - fm.client_playback.testing = True - - assert not fm.state.flow_count() - fm.tick(0) - assert fm.state.flow_count() - - f.error = Error("error") - fm.error(f) - class TestRequest: diff --git a/test/mitmproxy/test_server.py b/test/mitmproxy/test_server.py index 321bb11f..c5a5bb71 100644 --- a/test/mitmproxy/test_server.py +++ b/test/mitmproxy/test_server.py @@ -60,7 +60,7 @@ class CommonMixin: # Disconnect error l.request.path = "/p/305:d0" rt = self.master.replay_request(l, block=True) - assert not rt + assert rt if isinstance(self, tservers.HTTPUpstreamProxyTest): assert l.response.status_code == 502 else: @@ -72,7 +72,7 @@ class CommonMixin: # In upstream mode with ssl, the replay will fail as we cannot establish # SSL with the upstream proxy. rt = self.master.replay_request(l, block=True) - assert not rt + assert rt if isinstance(self, tservers.HTTPUpstreamProxyTest): assert l.response.status_code == 502 else: |