-
Notifications
You must be signed in to change notification settings - Fork 274
/
proxy.py
57 lines (44 loc) · 1.6 KB
/
proxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# coding: utf-8
from test.util import build_grab, temp_file
from test.util import BaseGrabTestCase
from grab.proxylist import ProxyList
DEFAULT_PLIST_DATA = \
'1.1.1.1:8080\n'\
'1.1.1.2:8080\n'
class GrabProxyTestCase(BaseGrabTestCase):
def setUp(self):
self.server.reset()
def test_no_proxy_list(self):
g = build_grab()
self.assertEqual(0, g.proxylist.size())
class ProxyListTestCase(BaseGrabTestCase):
def setUp(self):
self.server.reset()
def generate_plist_file(self, path, data=DEFAULT_PLIST_DATA):
with open(path, 'w') as out:
out.write(data)
return path
def test_basic(self):
pl = ProxyList()
self.assertEqual(0, pl.size())
def test_file_proxy_source(self):
with temp_file() as path:
pl = ProxyList()
self.generate_plist_file(path)
pl.load_file(path)
self.assertEqual(2, pl.size())
def test_web_proxy_source(self):
pl = ProxyList()
self.server.response['data'] = DEFAULT_PLIST_DATA
pl.load_url(self.server.get_url())
self.assertEqual(2, pl.size())
def test_get_next_proxy(self):
with temp_file() as path:
pl = ProxyList()
self.generate_plist_file(path, 'foo:1\nbar:1')
pl.load_file(path)
self.assertEqual(pl.get_next_proxy().host, 'foo')
self.assertEqual(pl.get_next_proxy().host, 'bar')
self.assertEqual(pl.get_next_proxy().host, 'foo')
pl.load_file(path)
self.assertEqual(pl.get_next_proxy().host, 'foo')