diff options
Diffstat (limited to 'test/mitmproxy/addons/test_clientplayback.py')
-rw-r--r-- | test/mitmproxy/addons/test_clientplayback.py | 75 |
1 files changed, 51 insertions, 24 deletions
diff --git a/test/mitmproxy/addons/test_clientplayback.py b/test/mitmproxy/addons/test_clientplayback.py index 03fcfe47..32eca536 100644 --- a/test/mitmproxy/addons/test_clientplayback.py +++ b/test/mitmproxy/addons/test_clientplayback.py @@ -1,38 +1,65 @@ -from mitmproxy.test import tflow +import os import mock +from mitmproxy.test import tflow +from mitmproxy.test import tutils +from mitmproxy import io +from mitmproxy import exceptions + from mitmproxy.addons import clientplayback -from mitmproxy import options +from mitmproxy.test import taddons + + +def tdump(path, flows): + w = io.FlowWriter(open(path, "wb")) + for i in flows: + w.add(i) -from .. import mastertest + +class MockThread(): + def is_alive(self): + return False class TestClientPlayback: def test_playback(self): cp = clientplayback.ClientPlayback() - cp.configure(options.Options(), []) - assert cp.count() == 0 - f = tflow.tflow(resp=True) - cp.load([f]) - assert cp.count() == 1 - RP = "mitmproxy.proxy.protocol.http_replay.RequestReplayThread" - with mock.patch(RP) as rp: - assert not cp.current - with mastertest.mockctx(): + with taddons.context(): + assert cp.count() == 0 + f = tflow.tflow(resp=True) + cp.load([f]) + assert cp.count() == 1 + RP = "mitmproxy.proxy.protocol.http_replay.RequestReplayThread" + with mock.patch(RP) as rp: + assert not cp.current_thread + cp.tick() + rp.assert_called() + assert cp.current_thread + + cp.keepserving = False + cp.flows = None + cp.current_thread = None + with mock.patch("mitmproxy.master.Master.shutdown") as sd: cp.tick() - rp.assert_called() - assert cp.current - - cp.keepserving = False - cp.flows = None - cp.current = None - with mock.patch("mitmproxy.master.Master.shutdown") as sd: - with mastertest.mockctx(): + sd.assert_called() + + cp.current_thread = MockThread() + with mock.patch("mitmproxy.master.Master.shutdown") as sd: cp.tick() - sd.assert_called() + assert cp.current_thread is None def test_configure(self): cp = clientplayback.ClientPlayback() - cp.configure( - options.Options(), [] - ) + with taddons.context() as tctx: + with tutils.tmpdir() as td: + path = os.path.join(td, "flows") + tdump(path, [tflow.tflow()]) + tctx.configure(cp, client_replay=[path]) + tctx.configure(cp, client_replay=[]) + tctx.configure(cp) + tutils.raises( + exceptions.OptionsError, + tctx.configure, + cp, + client_replay=["nonexistent"] + ) |