diff options
author | Maximilian Hils <git@maximilianhils.com> | 2017-04-26 11:12:14 +0200 |
---|---|---|
committer | Maximilian Hils <git@maximilianhils.com> | 2017-04-26 12:11:37 +0200 |
commit | ca2827886a97de88f4ab2937e71588fc9320ba4d (patch) | |
tree | c02e759f64d5468920ae51a8f7212d3452ef1aaf /test | |
parent | b3a11433389b47c913cfcd3c56a72d39ff6b93ef (diff) | |
download | mitmproxy-ca2827886a97de88f4ab2937e71588fc9320ba4d.tar.gz mitmproxy-ca2827886a97de88f4ab2937e71588fc9320ba4d.tar.bz2 mitmproxy-ca2827886a97de88f4ab2937e71588fc9320ba4d.zip |
separate reading from stdin into its own addon
Diffstat (limited to 'test')
-rw-r--r-- | test/mitmproxy/addons/test_readfile.py | 117 |
1 files changed, 63 insertions, 54 deletions
diff --git a/test/mitmproxy/addons/test_readfile.py b/test/mitmproxy/addons/test_readfile.py index 689d9779..813aa10e 100644 --- a/test/mitmproxy/addons/test_readfile.py +++ b/test/mitmproxy/addons/test_readfile.py @@ -37,59 +37,68 @@ def corrupt_data(): return f -@mock.patch('mitmproxy.master.Master.load_flow') -def test_configure(mck, tmpdir, data, corrupt_data): - rf = readfile.ReadFile() - with taddons.context() as tctx: - tf = tmpdir.join("tfile") - - tf.write(data.getvalue()) - tctx.configure(rf, rfile=str(tf)) - assert not mck.called - rf.running() - assert mck.called - - tf.write(corrupt_data.getvalue()) - tctx.configure(rf, rfile=str(tf)) - with pytest.raises(exceptions.OptionsError): +class TestReadFile: + @mock.patch('mitmproxy.master.Master.load_flow') + def test_configure(self, mck, tmpdir, data, corrupt_data): + rf = readfile.ReadFile() + with taddons.context() as tctx: + tf = tmpdir.join("tfile") + + tf.write(data.getvalue()) + tctx.configure(rf, rfile=str(tf)) + assert not mck.called rf.running() - - -@mock.patch('mitmproxy.master.Master.load_flow') -@mock.patch('sys.stdin') -def test_configure_stdin(stdin, load_flow, data, corrupt_data): - rf = readfile.ReadFile() - with taddons.context() as tctx: - stdin.buffer = data - tctx.configure(rf, rfile="-") - assert not load_flow.called - rf.running() - assert load_flow.called - - stdin.buffer = corrupt_data - tctx.configure(rf, rfile="-") - with pytest.raises(exceptions.OptionsError): + assert mck.called + + tf.write(corrupt_data.getvalue()) + tctx.configure(rf, rfile=str(tf)) + with pytest.raises(exceptions.OptionsError): + rf.running() + + @mock.patch('mitmproxy.master.Master.load_flow') + def test_corrupt(self, mck, corrupt_data): + rf = readfile.ReadFile() + with taddons.context() as tctx: + with pytest.raises(exceptions.FlowReadException): + rf.load_flows(io.BytesIO(b"qibble")) + assert not mck.called + assert len(tctx.master.logs) == 1 + + with pytest.raises(exceptions.FlowReadException): + rf.load_flows(corrupt_data) + assert mck.called + assert len(tctx.master.logs) == 2 + + def test_nonexisting_file(self): + rf = readfile.ReadFile() + with taddons.context() as tctx: + with pytest.raises(exceptions.FlowReadException): + rf.load_flows_from_path("nonexistent") + assert len(tctx.master.logs) == 1 + + +class TestReadFileStdin: + @mock.patch('mitmproxy.master.Master.load_flow') + @mock.patch('sys.stdin') + def test_stdin(self, stdin, load_flow, data, corrupt_data): + rf = readfile.ReadFileStdin() + with taddons.context() as tctx: + stdin.buffer = data + tctx.configure(rf, rfile="-") + assert not load_flow.called rf.running() - - -@mock.patch('mitmproxy.master.Master.load_flow') -def test_corrupt(mck, corrupt_data): - rf = readfile.ReadFile() - with taddons.context() as tctx: - with pytest.raises(exceptions.FlowReadException): - rf.load_flows(io.BytesIO(b"qibble")) - assert not mck.called - assert len(tctx.master.logs) == 1 - - with pytest.raises(exceptions.FlowReadException): - rf.load_flows(corrupt_data) - assert mck.called - assert len(tctx.master.logs) == 2 - - -def test_nonexisting_file(): - rf = readfile.ReadFile() - with taddons.context() as tctx: - with pytest.raises(exceptions.FlowReadException): - rf.load_flows_from_path("nonexistent") - assert len(tctx.master.logs) == 1 + assert load_flow.called + + stdin.buffer = corrupt_data + tctx.configure(rf, rfile="-") + with pytest.raises(exceptions.OptionsError): + rf.running() + + @mock.patch('mitmproxy.master.Master.load_flow') + def test_normal(self, load_flow, tmpdir, data): + rf = readfile.ReadFileStdin() + with taddons.context(): + tfile = tmpdir.join("tfile") + tfile.write(data.getvalue()) + rf.load_flows_from_path(str(tfile)) + assert load_flow.called |