2023-11-06 19:41:05 +00:00
|
|
|
import subprocess
|
|
|
|
import time
|
|
|
|
import pytest
|
2023-11-06 21:22:38 +00:00
|
|
|
from harvester.proxy import fetch_list, fetch_all
|
2023-11-06 19:41:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session', autouse=True)
|
|
|
|
def start_web_server():
|
2023-11-08 02:27:10 +00:00
|
|
|
"""Start a local webserver to serve unit test data."""
|
2023-11-06 19:41:05 +00:00
|
|
|
server_process = subprocess.Popen(['python', '-m', 'http.server', '8888'], cwd='tests/data')
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
server_process.terminate()
|
|
|
|
|
|
|
|
|
|
|
|
def test_fetch_list():
|
2023-11-08 02:27:10 +00:00
|
|
|
"""Proxies should be returned in the form of [username:password]@ip:port."""
|
2023-11-06 19:41:05 +00:00
|
|
|
expected = ['127.0.0.1:9000', '127.0.0.1:9001', 'username:pa$$@word@127.0.0.1:9002']
|
2023-11-06 21:22:38 +00:00
|
|
|
result = fetch_list('http://localhost:8888/proxies1.txt')
|
2023-11-06 22:23:21 +00:00
|
|
|
assert set(expected).issubset(result)
|
2023-11-06 19:41:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_fetch_list_fail():
|
2023-11-08 02:27:10 +00:00
|
|
|
"""Failures are logged to console and an empty list is returned."""
|
2023-11-06 19:41:05 +00:00
|
|
|
expected = []
|
2023-11-06 21:22:38 +00:00
|
|
|
result = fetch_list('http://localhost:12345/proxies1.txt')
|
2023-11-06 19:41:05 +00:00
|
|
|
assert result == expected
|
|
|
|
|
|
|
|
|
2023-11-06 21:22:38 +00:00
|
|
|
def test_fetch_all():
|
|
|
|
expected = ['127.0.0.1:9000', '127.0.0.1:9001', 'username:pa$$@word@127.0.0.1:9002', '127.0.0.1:9999']
|
|
|
|
result = fetch_all(['http://localhost:8888/proxies1.txt', 'http://localhost:8888/proxies2.txt'])
|
2023-11-06 22:23:21 +00:00
|
|
|
assert set(expected).issubset(result)
|