proxy-pool/tests/plugins/test_plaintext_parser.py

61 lines
1.8 KiB
Python

import uuid
import pytest
from proxy_pool.plugins.builtin.parsers.plaintext import PlaintextParser
@pytest.fixture
def parser():
return PlaintextParser()
class TestSupports:
def test_matches_txt_extension(self, parser):
assert parser.supports("https://example.com/proxies.txt") is True
def test_matches_plain_in_url(self, parser):
assert parser.supports("https://example.com/plain-list") is True
def test_rejects_unrelated_url(self, parser):
assert parser.supports("https://example.com/proxies.json") is False
class TestParse:
async def test_parses_valid_lines(self, parser):
raw = b"192.168.1.1:8080\n10.0.0.1:3128\n"
source_id = uuid.uuid4()
results = await parser.parse(raw, "http://example.com", source_id, "http")
assert len(results) == 2
assert results[0].ip == "192.168.1.1"
assert results[0].port == 8080
assert results[0].protocol == "http"
assert results[0].source_id == source_id
async def test_skips_invalid_lines(self, parser):
raw = b"192.168.1.1:8080\nnot a proxy\n\n# comment\n10.0.0.1:3128\n"
results = await parser.parse(raw, "http://example.com", None, "http")
assert len(results) == 2
async def test_handles_empty_input(self, parser):
results = await parser.parse(b"", "http://example.com", None, "http")
assert results == []
async def test_handles_bad_encoding(self, parser):
raw = b"192.168.1.1:8080\n\xff\xfe broken\n10.0.0.1:3128\n"
results = await parser.parse(raw, "http://example.com", None, "http")
assert len(results) == 2
async def test_uses_default_protocol(self, parser):
raw = b"1.2.3.4:1080\n"
results = await parser.parse(raw, "http://example.com", None, "socks5")
assert results[0].protocol == "socks5"