Increase test coverage

This commit is contained in:
Ujjwal Verma 2017-05-31 04:05:10 +05:30
parent ec7d7c995c
commit 33f735ef50
3 changed files with 32 additions and 9 deletions

View File

@ -147,14 +147,14 @@ class Master:
raise exceptions.ReplayException( raise exceptions.ReplayException(
"Can't replay intercepted flow." "Can't replay intercepted flow."
) )
if f.request.raw_content is None:
raise exceptions.ReplayException(
"Can't replay flow with missing content."
)
if not f.request: if not f.request:
raise exceptions.ReplayException( raise exceptions.ReplayException(
"Can't replay flow with missing request." "Can't replay flow with missing request."
) )
if f.request.raw_content is None:
raise exceptions.ReplayException(
"Can't replay flow with missing content."
)
f.backup() f.backup()
f.request.is_replay = True f.request.is_replay = True

View File

@ -7,7 +7,7 @@ from mitmproxy import flowfilter
from mitmproxy import options from mitmproxy import options
from mitmproxy.proxy import config from mitmproxy.proxy import config
from mitmproxy.io import tnetstring from mitmproxy.io import tnetstring
from mitmproxy.exceptions import FlowReadException from mitmproxy.exceptions import FlowReadException, ReplayException, ControlException
from mitmproxy import flow from mitmproxy import flow
from mitmproxy import http from mitmproxy import http
from mitmproxy.proxy.server import DummyServer from mitmproxy.proxy.server import DummyServer
@ -102,15 +102,19 @@ class TestFlowMaster:
fm = master.Master(None, DummyServer()) fm = master.Master(None, DummyServer())
f = tflow.tflow(resp=True) f = tflow.tflow(resp=True)
f.request.content = None f.request.content = None
with pytest.raises(Exception, match="missing"): with pytest.raises(ReplayException, match="missing"):
fm.replay_request(f)
f.request = None
with pytest.raises(ReplayException, match="request"):
fm.replay_request(f) fm.replay_request(f)
f.intercepted = True f.intercepted = True
with pytest.raises(Exception, match="intercepted"): with pytest.raises(ReplayException, match="intercepted"):
fm.replay_request(f) fm.replay_request(f)
f.live = True f.live = True
with pytest.raises(Exception, match="live"): with pytest.raises(ReplayException, match="live"):
fm.replay_request(f) fm.replay_request(f)
def test_all(self): def test_all(self):
@ -132,6 +136,10 @@ class TestFlowMaster:
f.error = flow.Error("msg") f.error = flow.Error("msg")
fm.addons.handle_lifecycle("error", f) fm.addons.handle_lifecycle("error", f)
fm.tell("foo", f)
with pytest.raises(ControlException):
fm.tick(timeout=1)
fm.shutdown() fm.shutdown()

View File

@ -4,7 +4,7 @@ from mitmproxy.test import tflow
from mitmproxy.net.http import Headers from mitmproxy.net.http import Headers
import mitmproxy.io import mitmproxy.io
from mitmproxy import flowfilter from mitmproxy import flowfilter
from mitmproxy.exceptions import Kill from mitmproxy.exceptions import Kill, ControlException
from mitmproxy import flow from mitmproxy import flow
from mitmproxy import http from mitmproxy import http
@ -170,10 +170,18 @@ class TestHTTPFlow:
assert not f == f2 assert not f == f2
f2.error = flow.Error("e2") f2.error = flow.Error("e2")
assert not f == f2 assert not f == f2
f2.backup()
f2.intercept() # to change the state
f.set_state(f2.get_state()) f.set_state(f2.get_state())
assert f.get_state() == f2.get_state() assert f.get_state() == f2.get_state()
def test_kill(self): def test_kill(self):
f = tflow.tflow()
with pytest.raises(ControlException):
f.intercept()
f.resume()
f.kill()
f = tflow.tflow() f = tflow.tflow()
f.intercept() f.intercept()
assert f.killable assert f.killable
@ -181,6 +189,13 @@ class TestHTTPFlow:
assert not f.killable assert not f.killable
assert f.reply.value == Kill assert f.reply.value == Kill
def test_intercept(self):
f = tflow.tflow()
f.intercept()
assert f.reply.state == "taken"
f.intercept()
assert f.reply.state == "taken"
def test_resume(self): def test_resume(self):
f = tflow.tflow() f = tflow.tflow()
f.intercept() f.intercept()