-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
198 lines (154 loc) · 5.91 KB
/
test.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import os
import sys
import glob
import time
import unittest
import tempfile
from multiprocessing import Process
import flask
import scrapelib
app = flask.Flask(__name__)
@app.route('/')
def index():
resp = app.make_response("Hello world!")
return resp
@app.route('/ua')
def ua():
resp = app.make_response(flask.request.headers['user-agent'])
resp.headers['cache-control'] = 'no-cache'
return resp
@app.route('/p/s.html')
def secret():
return "secret"
@app.route('/redirect')
def redirect():
return flask.redirect(flask.url_for('index'))
@app.route('/500')
def fivehundred():
flask.abort(500)
@app.route('/robots.txt')
def robots():
return """
User-agent: *
Disallow: /p/
Allow: /
"""
def run_server():
class NullFile(object):
def write(self, s):
pass
sys.stdout = NullFile()
sys.stderr = NullFile()
app.run()
class ScraperTest(unittest.TestCase):
def setUp(self):
self.cache_dir = tempfile.mkdtemp()
self.error_dir = tempfile.mkdtemp()
self.s = scrapelib.Scraper(requests_per_minute=0,
error_dir=self.error_dir,
cache_dir=self.cache_dir,
use_cache_first=True)
def tearDown(self):
for path in glob.iglob(os.path.join(self.cache_dir, "*")):
os.remove(path)
os.rmdir(self.cache_dir)
for path in glob.iglob(os.path.join(self.error_dir, "*")):
os.remove(path)
os.rmdir(self.error_dir)
def test_get(self):
self.assertEqual('Hello world!',
self.s.urlopen("http://localhost:5000/"))
def test_request_throttling(self):
requests = 0
s = scrapelib.Scraper(requests_per_minute=30)
begin = time.time()
while time.time() <= (begin + 1):
s.urlopen("http://localhost:5000/")
requests += 1
self.assert_(requests <= 2)
s.requests_per_minute = 500
requests = 0
begin = time.time()
while time.time() <= (begin + 1):
s.urlopen("http://localhost:5000/")
requests += 1
self.assert_(requests > 5)
def test_user_agent(self):
resp = self.s.urlopen("http://localhost:5000/ua")
self.assertEqual(resp, scrapelib._user_agent)
self.s.user_agent = 'a different agent'
resp = self.s.urlopen("http://localhost:5000/ua")
self.assertEqual(resp, 'a different agent')
def test_default_to_http(self):
self.assertEqual('Hello world!',
self.s.urlopen("localhost:5000/"))
def test_follow_robots(self):
self.assertRaises(scrapelib.RobotExclusionError, self.s.urlopen,
"http://localhost:5000/p/s.html")
self.assertRaises(scrapelib.RobotExclusionError, self.s.urlopen,
"http://localhost:5000/p/a/t/h/")
self.s.follow_robots = False
self.assertEqual("secret",
self.s.urlopen("http://localhost:5000/p/s.html"))
self.assertRaises(scrapelib.HTTPError, self.s.urlopen,
"http://localhost:5000/p/a/t/h/")
def test_error_context(self):
def raises():
with self.s.urlopen("http://localhost:5000/"):
raise Exception('test')
self.assertRaises(Exception, raises)
self.assertTrue(os.path.isfile(os.path.join(
self.error_dir, "http:,,localhost:5000,")))
def test_404(self):
self.assertRaises(scrapelib.HTTPError, self.s.urlopen,
"http://localhost:5000/does/not/exist")
self.s.raise_errors = False
resp = self.s.urlopen("http://localhost:5000/does/not/exist")
self.assertEqual(404, resp.response.code)
def test_500(self):
self.assertRaises(scrapelib.HTTPError, self.s.urlopen,
"http://localhost:5000/500")
self.s.raise_errors = False
resp = self.s.urlopen("http://localhost:5000/500")
self.assertEqual(resp.response.code, 500)
def test_follow_redirect(self):
resp = self.s.urlopen("http://localhost:5000/redirect")
self.assertEqual("http://localhost:5000/", resp.response.url)
self.assertEqual("http://localhost:5000/redirect",
resp.response.requested_url)
self.assertEqual(200, resp.response.code)
self.s.follow_redirects = False
resp = self.s.urlopen("http://localhost:5000/redirect")
self.assertEqual("http://localhost:5000/redirect",
resp.response.url)
self.assertEqual("http://localhost:5000/redirect",
resp.response.requested_url)
self.assertEqual(302, resp.response.code)
def test_caching(self):
resp = self.s.urlopen("http://localhost:5000/")
self.assertFalse(resp.response.fromcache)
resp = self.s.urlopen("http://localhost:5000/")
self.assert_(resp.response.fromcache)
self.s.use_cache_first = False
resp = self.s.urlopen("http://localhost:5000/")
self.assertFalse(resp.response.fromcache)
def test_urlretrieve(self):
fname, resp = self.s.urlretrieve("http://localhost:5000/")
with open(fname) as f:
self.assertEqual(f.read(), "Hello world!")
self.assertEqual(200, resp.code)
os.remove(fname)
(fh, set_fname) = tempfile.mkstemp()
fname, resp = self.s.urlretrieve("http://localhost:5000/",
set_fname)
self.assertEqual(fname, set_fname)
with open(set_fname) as f:
self.assertEqual(f.read(), "Hello world!")
self.assertEqual(200, resp.code)
os.remove(set_fname)
if __name__ == '__main__':
process = Process(target=run_server)
process.start()
time.sleep(0.1)
unittest.main(exit=False)
process.terminate()