Skip to content

Commit 7beef28

Browse files
authored
[Coverage] For newly added components (#1014)
* Add newly added code cov * Fix spelling
1 parent b40b91f commit 7beef28

File tree

8 files changed

+67
-18
lines changed

8 files changed

+67
-18
lines changed

examples/ssl_echo_server.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class EchoSSLServerHandler(BaseTcpServerHandler[TcpClientConnection]):
2121
"""Wraps client socket during initialization."""
2222

2323
@staticmethod
24-
def create(**kwargs: Any) -> TcpClientConnection:
24+
def create(**kwargs: Any) -> TcpClientConnection: # pragma: no cover
2525
return TcpClientConnection(**kwargs)
2626

2727
def initialize(self) -> None:

examples/tcp_echo_server.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class EchoServerHandler(BaseTcpServerHandler[TcpClientConnection]):
2020
"""Sets client socket to non-blocking during initialization."""
2121

2222
@staticmethod
23-
def create(**kwargs: Any) -> TcpClientConnection:
23+
def create(**kwargs: Any) -> TcpClientConnection: # pragma: no cover
2424
return TcpClientConnection(**kwargs)
2525

2626
def initialize(self) -> None:

proxy/core/base/tcp_tunnel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def handle_data(self, data: memoryview) -> Optional[bool]:
4848
pass # pragma: no cover
4949

5050
@staticmethod
51-
def create(**kwargs: Any) -> TcpClientConnection:
51+
def create(**kwargs: Any) -> TcpClientConnection: # pragma: no cover
5252
return TcpClientConnection(**kwargs)
5353

5454
def initialize(self) -> None:

proxy/core/connection/pool.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def __init__(self) -> None:
7878
self.pools: Dict[Tuple[str, int], Set[TcpServerConnection]] = {}
7979

8080
@staticmethod
81-
def create(**kwargs: Any) -> TcpServerConnection:
81+
def create(**kwargs: Any) -> TcpServerConnection: # pragma: no cover
8282
return TcpServerConnection(**kwargs)
8383

8484
def acquire(self, addr: Tuple[str, int]) -> Tuple[bool, TcpServerConnection]:

proxy/core/ssh/listener.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from paramiko.transport import Transport
1919
if TYPE_CHECKING: # pragma: no cover
2020
from paramiko.channel import Channel
21-
except ImportError:
21+
except ImportError: # pragma: no cover
2222
pass
2323

2424
from ...common.flag import flags

proxy/http/handler.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def __init__(self, *args: Any, **kwargs: Any):
5858
##
5959

6060
@staticmethod
61-
def create(**kwargs: Any) -> HttpClientConnection:
61+
def create(**kwargs: Any) -> HttpClientConnection: # pragma: no cover
6262
return HttpClientConnection(**kwargs)
6363

6464
def initialize(self) -> None:
@@ -205,12 +205,14 @@ async def handle_writables(self, writables: Writables) -> bool:
205205
if teardown:
206206
return True
207207
except BrokenPipeError:
208-
logger.warning(
208+
logger.warning( # pragma: no cover
209209
'BrokenPipeError when flushing buffer for client',
210210
)
211211
return True
212212
except OSError:
213-
logger.warning('OSError when flushing buffer to client')
213+
logger.warning( # pragma: no cover
214+
'OSError when flushing buffer to client',
215+
)
214216
return True
215217
return False
216218

@@ -261,8 +263,6 @@ def _initialize_plugin(
261263

262264
def _discover_plugin_klass(self, protocol: int) -> Optional[Type['HttpProtocolHandlerPlugin']]:
263265
"""Discovers and return matching HTTP handler plugin matching protocol."""
264-
if protocol == httpProtocols.UNKNOWN:
265-
return None
266266
if b'HttpProtocolHandlerPlugin' in self.flags.plugins:
267267
for klass in self.flags.plugins[b'HttpProtocolHandlerPlugin']:
268268
k: Type['HttpProtocolHandlerPlugin'] = klass
@@ -287,6 +287,10 @@ def _parse_first_request(self, data: memoryview) -> bool:
287287
) from e
288288
if not self.request.is_complete:
289289
return False
290+
# Bail out if http protocol is unknown
291+
if self.request.http_handler_protocol == httpProtocols.UNKNOWN:
292+
self.work.queue(BAD_REQUEST_RESPONSE_PKT)
293+
return True
290294
# Discover which HTTP handler plugin is capable of
291295
# handling the current incoming request
292296
klass = self._discover_plugin_klass(

tests/http/parser/test_http_parser.py

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
)
1818
from proxy.http.exception import HttpProtocolException
1919
from proxy.http.responses import okResponse
20+
from proxy.http.protocols import httpProtocols
2021
from proxy.common.constants import CRLF, HTTP_1_0
2122

2223

@@ -832,6 +833,10 @@ def test_parses_icap_protocol(self) -> None:
832833
self.assertEqual(self.parser.method, b'REQMOD')
833834
assert self.parser._url is not None
834835
self.assertEqual(self.parser._url.scheme, b'icap')
836+
self.assertEqual(
837+
self.parser.http_handler_protocol,
838+
httpProtocols.UNKNOWN,
839+
)
835840

836841
def test_cannot_parse_sip_protocol(self) -> None:
837842
# Will fail to parse because of invalid host and port in the request line

tests/test_main.py

+48-8
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,51 @@ def test_enable_devtools(
308308
mock_acceptor_pool.return_value.setup.assert_called_once()
309309
mock_listener.return_value.setup.assert_called_once()
310310

311-
# def test_pac_file(self) -> None:
312-
# pass
313-
314-
# def test_imports_plugin(self) -> None:
315-
# pass
316-
317-
# def test_cannot_enable_https_proxy_and_tls_interception_mutually(self) -> None:
318-
# pass
311+
@mock.patch('time.sleep')
312+
@mock.patch('proxy.common.plugins.Plugins.load')
313+
@mock.patch('proxy.common.flag.FlagParser.parse_args')
314+
@mock.patch('proxy.proxy.EventManager')
315+
@mock.patch('proxy.proxy.AcceptorPool')
316+
@mock.patch('proxy.proxy.ThreadlessPool')
317+
@mock.patch('proxy.proxy.Listener')
318+
@mock.patch('proxy.proxy.SshHttpProtocolHandler')
319+
@mock.patch('proxy.proxy.SshTunnelListener')
320+
def test_enable_ssh_tunnel(
321+
self,
322+
mock_ssh_tunnel_listener: mock.Mock,
323+
mock_ssh_http_proto_handler: mock.Mock,
324+
mock_listener: mock.Mock,
325+
mock_executor_pool: mock.Mock,
326+
mock_acceptor_pool: mock.Mock,
327+
mock_event_manager: mock.Mock,
328+
mock_parse_args: mock.Mock,
329+
mock_load_plugins: mock.Mock,
330+
mock_sleep: mock.Mock,
331+
) -> None:
332+
mock_sleep.side_effect = KeyboardInterrupt()
333+
mock_args = mock_parse_args.return_value
334+
self.mock_default_args(mock_args)
335+
mock_args.enable_ssh_tunnel = True
336+
mock_args.local_executor = 0
337+
main(enable_ssh_tunnel=True, local_executor=0)
338+
mock_load_plugins.assert_called()
339+
self.assertEqual(
340+
mock_load_plugins.call_args_list[0][0][0], [
341+
bytes_(PLUGIN_HTTP_PROXY),
342+
],
343+
)
344+
mock_parse_args.assert_called_once()
345+
mock_event_manager.assert_not_called()
346+
if _env_threadless_compliant():
347+
mock_executor_pool.assert_called_once()
348+
mock_executor_pool.return_value.setup.assert_called_once()
349+
mock_acceptor_pool.assert_called_once()
350+
mock_acceptor_pool.return_value.setup.assert_called_once()
351+
mock_listener.return_value.setup.assert_called_once()
352+
mock_ssh_http_proto_handler.assert_called_once()
353+
mock_ssh_tunnel_listener.assert_called_once()
354+
mock_ssh_tunnel_listener.return_value.setup.assert_called_once()
355+
mock_ssh_tunnel_listener.return_value.start_port_forward.assert_called_once()
356+
mock_ssh_tunnel_listener.return_value.shutdown.assert_called_once()
357+
# shutdown will internally call stop port forward
358+
mock_ssh_tunnel_listener.return_value.stop_port_forward.assert_not_called()

0 commit comments

Comments
 (0)