coverage++

This commit is contained in:
Maximilian Hils 2021-03-13 23:17:07 +01:00
parent 13db172320
commit c842abff2d
3 changed files with 29 additions and 10 deletions

View File

@ -91,8 +91,12 @@ class Layer:
@property
def stack_pos(self) -> str:
"""repr() for this layer and all its parent layers, only useful for debugging."""
idx = self.context.layers.index(self)
return " >> ".join(repr(x) for x in self.context.layers[:idx + 1])
try:
idx = self.context.layers.index(self)
except ValueError:
return repr(self)
else:
return " >> ".join(repr(x) for x in self.context.layers[:idx + 1])
@abstractmethod
def _handle_event(self, event: events.Event) -> CommandGenerator[None]:

View File

@ -79,8 +79,6 @@ class TunnelLayer(layer.Layer):
yield from self.on_handshake_error(err)
yield from self._handshake_finished(err)
self.tunnel_state = TunnelState.CLOSED
elif isinstance(event, events.MessageInjected):
yield from self.event_to_child(event)
else: # pragma: no cover
raise AssertionError(f"Unexpected event: {event}")
else:

View File

@ -1,11 +1,26 @@
import pytest
from mitmproxy.proxy import commands, events, layer
from mitmproxy.proxy.context import Context
from test.mitmproxy.proxy import tutils
class TestLayer:
def test_debug_messages(self, tctx):
def test_continue(self, tctx: Context):
class TLayer(layer.Layer):
def _handle_event(self, event: events.Event) -> layer.CommandGenerator[None]:
yield commands.OpenConnection(self.context.server)
yield commands.OpenConnection(self.context.server)
assert (
tutils.Playbook(TLayer(tctx))
<< commands.OpenConnection(tctx.server)
>> tutils.reply(None)
<< commands.OpenConnection(tctx.server)
>> tutils.reply(None)
)
def test_debug_messages(self, tctx: Context):
tctx.server.id = "serverid"
class TLayer(layer.Layer):
@ -53,7 +68,7 @@ class TestLayer:
class TestNextLayer:
def test_simple(self, tctx):
def test_simple(self, tctx: Context):
nl = layer.NextLayer(tctx, ask_on_start=True)
nl.debug = " "
playbook = tutils.Playbook(nl, hooks=True)
@ -79,7 +94,7 @@ class TestNextLayer:
<< commands.SendData(tctx.client, b"bar")
)
def test_late_hook_reply(self, tctx):
def test_late_hook_reply(self, tctx: Context):
"""
Properly handle case where we receive an additional event while we are waiting for
a reply from the proxy core.
@ -104,7 +119,7 @@ class TestNextLayer:
)
@pytest.mark.parametrize("layer_found", [True, False])
def test_receive_close(self, tctx, layer_found):
def test_receive_close(self, tctx: Context, layer_found: bool):
"""Test that we abort a client connection which has disconnected without any layer being found."""
nl = layer.NextLayer(tctx)
playbook = tutils.Playbook(nl)
@ -128,7 +143,7 @@ class TestNextLayer:
<< commands.CloseConnection(tctx.client)
)
def test_func_references(self, tctx):
def test_func_references(self, tctx: Context):
nl = layer.NextLayer(tctx)
playbook = tutils.Playbook(nl)
@ -147,7 +162,9 @@ class TestNextLayer:
sd, = handle(events.DataReceived(tctx.client, b"bar"))
assert isinstance(sd, commands.SendData)
def test_repr(self, tctx):
def test_repr(self, tctx: Context):
nl = layer.NextLayer(tctx)
nl.layer = tutils.EchoLayer(tctx)
assert repr(nl)
assert nl.stack_pos
assert nl.layer.stack_pos