paijo77 commited on
Commit
868133e
·
verified ·
1 Parent(s): 24a5e4d

update tests/unit/test_github_grabber.py

Browse files
Files changed (1) hide show
  1. tests/unit/test_github_grabber.py +93 -0
tests/unit/test_github_grabber.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ import asyncio
3
+ from aioresponses import aioresponses
4
+ from app.grabber.github_grabber import GitHubGrabber
5
+ from app.models.source import SourceConfig, SourceType
6
+
7
+
8
+ class TestGitHubGrabber:
9
+ @pytest.mark.asyncio
10
+ async def test_fetch_content_success(self):
11
+ grabber = GitHubGrabber()
12
+ source = SourceConfig(
13
+ url="https://raw.githubusercontent.com/user/repo/main/list.txt",
14
+ type=SourceType.GITHUB_RAW,
15
+ )
16
+
17
+ with aioresponses() as mocked:
18
+ mocked.get(
19
+ str(source.url), status=200, body="192.168.1.1:8080\n10.0.0.1:3128"
20
+ )
21
+
22
+ content = await grabber.fetch_content(source)
23
+
24
+ assert "192.168.1.1:8080" in content
25
+ assert "10.0.0.1:3128" in content
26
+
27
+ @pytest.mark.asyncio
28
+ async def test_fetch_content_timeout(self):
29
+ grabber = GitHubGrabber(timeout=1)
30
+ source = SourceConfig(
31
+ url="https://raw.githubusercontent.com/user/repo/main/list.txt",
32
+ type=SourceType.GITHUB_RAW,
33
+ )
34
+
35
+ with aioresponses() as mocked:
36
+ mocked.get(str(source.url), exception=asyncio.TimeoutError())
37
+
38
+ with pytest.raises(Exception):
39
+ await grabber.fetch_content(source)
40
+
41
+ @pytest.mark.asyncio
42
+ async def test_fetch_content_404(self):
43
+ grabber = GitHubGrabber()
44
+ source = SourceConfig(
45
+ url="https://raw.githubusercontent.com/user/repo/main/notfound.txt",
46
+ type=SourceType.GITHUB_RAW,
47
+ )
48
+
49
+ with aioresponses() as mocked:
50
+ mocked.get(str(source.url), status=404)
51
+
52
+ with pytest.raises(Exception):
53
+ await grabber.fetch_content(source)
54
+
55
+ @pytest.mark.asyncio
56
+ async def test_extract_proxies_integration(self):
57
+ grabber = GitHubGrabber()
58
+ source = SourceConfig(
59
+ url="https://raw.githubusercontent.com/user/repo/main/mixed.txt",
60
+ type=SourceType.GITHUB_RAW,
61
+ )
62
+
63
+ mixed_content = """
64
+ 192.168.1.1:8080
65
+ 10.0.0.1:3128
66
+ vmess://eyJhZGQiOiIxMjcuMC4wLjEiLCJwb3J0Ijo0NDN9
67
+ vless://uuid@example.com:443?type=tcp
68
+ """
69
+
70
+ with aioresponses() as mocked:
71
+ mocked.get(str(source.url), status=200, body=mixed_content)
72
+
73
+ proxies = await grabber.extract_proxies(source)
74
+
75
+ assert len(proxies) >= 2
76
+ assert any(p.protocol == "http" for p in proxies)
77
+
78
+ @pytest.mark.asyncio
79
+ async def test_retry_on_failure(self):
80
+ grabber = GitHubGrabber(max_retries=3, retry_delay=0.1)
81
+ source = SourceConfig(
82
+ url="https://raw.githubusercontent.com/user/repo/main/list.txt",
83
+ type=SourceType.GITHUB_RAW,
84
+ )
85
+
86
+ with aioresponses() as mocked:
87
+ mocked.get(str(source.url), status=500)
88
+ mocked.get(str(source.url), status=500)
89
+ mocked.get(str(source.url), status=200, body="192.168.1.1:8080")
90
+
91
+ content = await grabber.fetch_content(source)
92
+
93
+ assert "192.168.1.1:8080" in content