diff options
author | Aldo Cortesi <aldo@corte.si> | 2018-04-28 10:59:12 +1200 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2018-04-30 17:17:03 +1200 |
commit | 236a2fb6fde4ff8837f85cf0a217f915b0bfed79 (patch) | |
tree | 7342213c11fbfb93701976457d78a84cf2e6dd89 /test | |
parent | 28d53d5a245cbac19896bac30a41435024b17b78 (diff) | |
download | mitmproxy-236a2fb6fde4ff8837f85cf0a217f915b0bfed79.tar.gz mitmproxy-236a2fb6fde4ff8837f85cf0a217f915b0bfed79.tar.bz2 mitmproxy-236a2fb6fde4ff8837f85cf0a217f915b0bfed79.zip |
client replay: re-design
Re-design the way client replay works. Before, we would fire up a thread,
replay, wait for the thread to complete, get the next flow, and repeat the
procedure. Now, we have one replay thread that starts when the addon starts,
which pops flows off a thread-safe queue. This is much cleaner, removes the
need for busy tick, and sets the scene for optimisations like server connection
reuse down the track.
Diffstat (limited to 'test')
-rw-r--r-- | test/mitmproxy/addons/test_check_ca.py | 4 | ||||
-rw-r--r-- | test/mitmproxy/addons/test_clientplayback.py | 72 | ||||
-rw-r--r-- | test/mitmproxy/addons/test_readfile.py | 2 | ||||
-rw-r--r-- | test/mitmproxy/addons/test_save.py | 8 | ||||
-rw-r--r-- | test/mitmproxy/addons/test_script.py | 17 | ||||
-rw-r--r-- | test/mitmproxy/addons/test_view.py | 8 |
6 files changed, 52 insertions, 59 deletions
diff --git a/test/mitmproxy/addons/test_check_ca.py b/test/mitmproxy/addons/test_check_ca.py index 5e820b6d..27e6f7e6 100644 --- a/test/mitmproxy/addons/test_check_ca.py +++ b/test/mitmproxy/addons/test_check_ca.py @@ -12,11 +12,11 @@ class TestCheckCA: async def test_check_ca(self, expired): msg = 'The mitmproxy certificate authority has expired!' - with taddons.context() as tctx: + a = check_ca.CheckCA() + with taddons.context(a) as tctx: tctx.master.server = mock.MagicMock() tctx.master.server.config.certstore.default_ca.has_expired = mock.MagicMock( return_value = expired ) - a = check_ca.CheckCA() tctx.configure(a) assert await tctx.master.await_log(msg) == expired diff --git a/test/mitmproxy/addons/test_clientplayback.py b/test/mitmproxy/addons/test_clientplayback.py index 0bb24e87..a63bec53 100644 --- a/test/mitmproxy/addons/test_clientplayback.py +++ b/test/mitmproxy/addons/test_clientplayback.py @@ -92,47 +92,13 @@ class TestClientPlayback: # assert rt.f.request.http_version == "HTTP/1.1" # assert ":authority" not in rt.f.request.headers - def test_playback(self): - cp = clientplayback.ClientPlayback() - with taddons.context(cp) as tctx: - assert cp.count() == 0 - f = tflow.tflow(resp=True) - cp.start_replay([f]) - assert cp.count() == 1 - RP = "mitmproxy.addons.clientplayback.RequestReplayThread" - with mock.patch(RP) as rp: - assert not cp.current_thread - cp.tick() - assert rp.called - assert cp.current_thread - - cp.flows = [] - cp.current_thread.is_alive.return_value = False - assert cp.count() == 1 - cp.tick() - assert cp.count() == 0 - assert tctx.master.has_event("update") - assert tctx.master.has_event("processing_complete") - - cp.current_thread = MockThread() - cp.tick() - assert cp.current_thread is None - - cp.start_replay([f]) - cp.stop_replay() - assert not cp.flows - - df = tflow.DummyFlow(tflow.tclient_conn(), tflow.tserver_conn(), True) - with pytest.raises(exceptions.CommandError, match="Can't replay live flow."): - cp.start_replay([df]) - def test_load_file(self, tmpdir): cp = clientplayback.ClientPlayback() with taddons.context(cp): fpath = str(tmpdir.join("flows")) tdump(fpath, [tflow.tflow(resp=True)]) cp.load_file(fpath) - assert cp.flows + assert cp.count() == 1 with pytest.raises(exceptions.CommandError): cp.load_file("/nonexistent") @@ -141,11 +107,39 @@ class TestClientPlayback: with taddons.context(cp) as tctx: path = str(tmpdir.join("flows")) tdump(path, [tflow.tflow()]) + assert cp.count() == 0 tctx.configure(cp, client_replay=[path]) - cp.configured = False + assert cp.count() == 1 tctx.configure(cp, client_replay=[]) - cp.configured = False - tctx.configure(cp) - cp.configured = False with pytest.raises(exceptions.OptionsError): tctx.configure(cp, client_replay=["nonexistent"]) + + def test_check(self): + cp = clientplayback.ClientPlayback() + with taddons.context(cp): + f = tflow.tflow(resp=True) + f.live = True + assert "live flow" in cp.check(f) + + f = tflow.tflow(resp=True) + f.intercepted = True + assert "intercepted flow" in cp.check(f) + + f = tflow.tflow(resp=True) + f.request = None + assert "missing request" in cp.check(f) + + f = tflow.tflow(resp=True) + f.request.raw_content = None + assert "missing content" in cp.check(f) + + def test_playback(self): + cp = clientplayback.ClientPlayback() + with taddons.context(cp): + assert cp.count() == 0 + f = tflow.tflow(resp=True) + cp.start_replay([f]) + assert cp.count() == 1 + + cp.stop_replay() + assert cp.count() == 0
\ No newline at end of file diff --git a/test/mitmproxy/addons/test_readfile.py b/test/mitmproxy/addons/test_readfile.py index f7e0c5c5..d22382a8 100644 --- a/test/mitmproxy/addons/test_readfile.py +++ b/test/mitmproxy/addons/test_readfile.py @@ -42,7 +42,7 @@ def corrupt_data(): class TestReadFile: def test_configure(self): rf = readfile.ReadFile() - with taddons.context() as tctx: + with taddons.context(rf) as tctx: tctx.configure(rf, readfile_filter="~q") with pytest.raises(Exception, match="Invalid readfile filter"): tctx.configure(rf, readfile_filter="~~") diff --git a/test/mitmproxy/addons/test_save.py b/test/mitmproxy/addons/test_save.py index 4486ff78..616caf58 100644 --- a/test/mitmproxy/addons/test_save.py +++ b/test/mitmproxy/addons/test_save.py @@ -11,7 +11,7 @@ from mitmproxy.addons import view def test_configure(tmpdir): sa = save.Save() - with taddons.context() as tctx: + with taddons.context(sa) as tctx: with pytest.raises(exceptions.OptionsError): tctx.configure(sa, save_stream_file=str(tmpdir)) with pytest.raises(Exception, match="Invalid filter"): @@ -32,7 +32,7 @@ def rd(p): def test_tcp(tmpdir): sa = save.Save() - with taddons.context() as tctx: + with taddons.context(sa) as tctx: p = str(tmpdir.join("foo")) tctx.configure(sa, save_stream_file=p) @@ -45,7 +45,7 @@ def test_tcp(tmpdir): def test_websocket(tmpdir): sa = save.Save() - with taddons.context() as tctx: + with taddons.context(sa) as tctx: p = str(tmpdir.join("foo")) tctx.configure(sa, save_stream_file=p) @@ -78,7 +78,7 @@ def test_save_command(tmpdir): def test_simple(tmpdir): sa = save.Save() - with taddons.context() as tctx: + with taddons.context(sa) as tctx: p = str(tmpdir.join("foo")) tctx.configure(sa, save_stream_file=p) diff --git a/test/mitmproxy/addons/test_script.py b/test/mitmproxy/addons/test_script.py index c358f019..91637489 100644 --- a/test/mitmproxy/addons/test_script.py +++ b/test/mitmproxy/addons/test_script.py @@ -92,14 +92,13 @@ class TestScript: @pytest.mark.asyncio async def test_simple(self, tdata): - with taddons.context() as tctx: - sc = script.Script( - tdata.path( - "mitmproxy/data/addonscripts/recorder/recorder.py" - ), - True, - ) - tctx.master.addons.add(sc) + sc = script.Script( + tdata.path( + "mitmproxy/data/addonscripts/recorder/recorder.py" + ), + True, + ) + with taddons.context(sc) as tctx: tctx.configure(sc) await tctx.master.await_log("recorder running") rec = tctx.master.addons.get("recorder") @@ -284,7 +283,7 @@ class TestScriptLoader: rec = tdata.path("mitmproxy/data/addonscripts/recorder") sc = script.ScriptLoader() sc.is_running = True - with taddons.context() as tctx: + with taddons.context(sc) as tctx: tctx.configure( sc, scripts = [ diff --git a/test/mitmproxy/addons/test_view.py b/test/mitmproxy/addons/test_view.py index 62a6aeb0..bd724950 100644 --- a/test/mitmproxy/addons/test_view.py +++ b/test/mitmproxy/addons/test_view.py @@ -155,7 +155,7 @@ def test_create(): def test_orders(): v = view.View() - with taddons.context(): + with taddons.context(v): assert v.order_options() @@ -303,7 +303,7 @@ def test_setgetval(): def test_order(): v = view.View() - with taddons.context() as tctx: + with taddons.context(v) as tctx: v.request(tft(method="get", start=1)) v.request(tft(method="put", start=2)) v.request(tft(method="get", start=3)) @@ -434,7 +434,7 @@ def test_signals(): def test_focus_follow(): v = view.View() - with taddons.context() as tctx: + with taddons.context(v) as tctx: console_addon = consoleaddons.ConsoleAddon(tctx.master) tctx.configure(console_addon) tctx.configure(v, console_focus_follow=True, view_filter="~m get") @@ -553,7 +553,7 @@ def test_settings(): def test_configure(): v = view.View() - with taddons.context() as tctx: + with taddons.context(v) as tctx: tctx.configure(v, view_filter="~q") with pytest.raises(Exception, match="Invalid interception filter"): tctx.configure(v, view_filter="~~") |