Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions tests/utils/test_unity_proxy_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import pytest

from unityproxy.utils.unity import UnityProxy
from unityproxy.exceptions import invalid_params as exceptions


@pytest.mark.parametrize("line",
(
"{login}:{password}@{ip}:{port}",
"{login}:{password}:{ip}:{port}",
"{ip}:{port}@{login}:{password}",
"{ip}:{port}:{login}:{password}",
"{ip};{port};{login};{password}",
"{login};{password};{ip};{port}",
"{ip}@{port}@{login}@{password}",
)
)
def test_ignore_errors_true(line: str):
ip = "invalid"
port = 8080
login = "login"
password = "password"
created_line = line.format(login=login, password=password, ip=ip, port=port)

unity = UnityProxy(ignore_parse_errors=True)
unity.add_by_line(created_line, "socks5")

assert (len(unity._UnityProxy__proxies) == 0)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тоже самое



@pytest.mark.parametrize("line",
(
"{login}:{password}@{ip}:{port}",
"{login}:{password}:{ip}:{port}",
"{ip}:{port}@{login}:{password}",
"{ip}:{port}:{login}:{password}",
"{ip};{port};{login};{password}",
"{login};{password};{ip};{port}",
"{ip}@{port}@{login}@{password}",
)
)
def test_ignore_errors_false(line: str):
ip = "invalid"
port = 8080
login = "login"
password = "password"
created_line = line.format(login=login, password=password, ip=ip, port=port)

unity = UnityProxy(ignore_parse_errors=False)

with pytest.raises(exceptions.CanNotParseProxy):
unity.add_by_line(created_line, "socks5")

assert (len(unity._UnityProxy__proxies) == 0)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

len(unity) вернёт кол-во прокси. Не нужно лезть в приватные атрибуты) На то они и приватные

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def __len__(self) -> int:
        return len(self.__proxies)

33 changes: 18 additions & 15 deletions unityproxy/utils/unity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from io import TextIOWrapper
from copy import deepcopy
from typing import List, Union, Callable
from functools import wraps

from .proxy import Proxy
from ..types import PROXY_TYPEHINT
Expand All @@ -26,23 +27,25 @@ def __init__(self, ignore_parse_errors: bool = True, custom_parser: Callable=Non
self.from_ = UnityAddFrom(self)
self.to = UnityConvertTo(self)

def ignore_errors(method):
@wraps(method)
def wrapper(self, *args, **kwargs):
if self.__ignore_err:
try:
return method(self, *args, **kwargs)
except Exception as err:
print(f"ignored error in {method.__name__}: {err}")
else:
return method(self, *args, **kwargs)
return wrapper

@ignore_errors
def add_by_line(self, line: str, proxy_type: PROXY_TYPEHINT):
if self.__ignore_err:
try:
self.__proxies.append(Proxy.from_line(line, proxy_type, self.__custom_parser))
except:...
else:
self.__proxies.append(Proxy.from_line(line, proxy_type, self.__custom_parser))
self.__proxies.append(Proxy.from_line(line, proxy_type, self.__custom_parser))

@ignore_errors
def add_by_values(self, ip: str, port: int, type_: PROXY_TYPEHINT, login: str=None, password: str=None):
#TODO mb create decorator
if self.__ignore_err:
try:
self.__proxies.append(Proxy(ip=ip, port=port, type_=type_, login=login, password=password))
except: ...
else:
self.__proxies.append(Proxy(ip=ip, port=port, type_=type_, login=login, password=password))

self.__proxies.append(Proxy(ip=ip, port=port, type_=type_, login=login, password=password))

def remove(self, proxy: Proxy):
self.__proxies.remove(proxy)
Expand Down Expand Up @@ -119,4 +122,4 @@ def queue(self) -> Queue[Proxy]:
q = Queue()
for proxy in self.__obj:
q.put(deepcopy(proxy))
return q
return q