2022-09-18 10:44:18 +00:00
|
|
|
from __future__ import annotations
|
2022-11-29 13:28:41 +00:00
|
|
|
|
2022-09-17 13:41:54 +00:00
|
|
|
import asyncio
|
2017-01-23 21:53:56 +00:00
|
|
|
import os
|
2017-10-20 11:37:26 +00:00
|
|
|
import socket
|
2024-01-04 09:53:13 +00:00
|
|
|
import sys
|
2017-10-20 11:37:26 +00:00
|
|
|
|
2016-12-01 09:36:18 +00:00
|
|
|
import pytest
|
|
|
|
|
2022-09-17 13:41:54 +00:00
|
|
|
from mitmproxy.utils import data
|
|
|
|
|
2022-04-26 11:53:35 +00:00
|
|
|
skip_windows = pytest.mark.skipif(os.name == "nt", reason="Skipping due to Windows")
|
2017-02-01 15:17:22 +00:00
|
|
|
|
|
|
|
skip_not_windows = pytest.mark.skipif(
|
2022-04-26 11:53:35 +00:00
|
|
|
os.name != "nt", reason="Skipping due to not Windows"
|
2017-02-01 15:17:22 +00:00
|
|
|
)
|
|
|
|
|
2017-10-20 11:37:26 +00:00
|
|
|
try:
|
|
|
|
s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
|
|
|
|
s.bind(("::1", 0))
|
|
|
|
s.close()
|
|
|
|
except OSError:
|
|
|
|
no_ipv6 = True
|
|
|
|
else:
|
|
|
|
no_ipv6 = False
|
|
|
|
|
2022-04-26 11:53:35 +00:00
|
|
|
skip_no_ipv6 = pytest.mark.skipif(no_ipv6, reason="Host has no IPv6 support")
|
2018-04-22 23:05:58 +00:00
|
|
|
|
|
|
|
|
2024-01-04 09:53:13 +00:00
|
|
|
class EagerTaskCreationEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
|
|
|
|
def new_event_loop(self):
|
|
|
|
loop = super().new_event_loop()
|
|
|
|
if sys.version_info >= (3, 12):
|
|
|
|
loop.set_task_factory(asyncio.eager_task_factory)
|
|
|
|
return loop
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def event_loop_policy(request):
|
|
|
|
return EagerTaskCreationEventLoopPolicy()
|
|
|
|
|
|
|
|
|
2018-04-22 23:05:58 +00:00
|
|
|
@pytest.fixture()
|
|
|
|
def tdata():
|
|
|
|
return data.Data(__name__)
|
2022-09-17 13:41:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AsyncLogCaptureFixture:
|
|
|
|
def __init__(self, caplog: pytest.LogCaptureFixture):
|
|
|
|
self.caplog = caplog
|
|
|
|
|
2022-09-18 10:44:18 +00:00
|
|
|
def set_level(self, level: int | str, logger: str | None = None) -> None:
|
|
|
|
self.caplog.set_level(level, logger)
|
|
|
|
|
2022-09-17 13:41:54 +00:00
|
|
|
async def await_log(self, text, timeout=2):
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
for i in range(int(timeout / 0.01)):
|
|
|
|
if text in self.caplog.text:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
await asyncio.sleep(0.01)
|
2023-02-13 21:45:02 +00:00
|
|
|
raise AssertionError(f"Did not find {text!r} in log:\n{self.caplog.text}")
|
2022-09-17 13:41:54 +00:00
|
|
|
|
2022-09-18 10:44:18 +00:00
|
|
|
def clear(self) -> None:
|
|
|
|
self.caplog.clear()
|
|
|
|
|
2022-09-17 13:41:54 +00:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def caplog_async(caplog):
|
|
|
|
return AsyncLogCaptureFixture(caplog)
|