source: python/trunk/Lib/test/test_robotparser.py

Last change on this file was 391, checked in by dmik, 11 years ago

python: Merge vendor 2.7.6 to trunk.

  • Property svn:eol-style set to native
File size: 6.6 KB
Line 
1import unittest, StringIO, robotparser
2from test import test_support
3from urllib2 import urlopen, HTTPError
4
5class RobotTestCase(unittest.TestCase):
6 def __init__(self, index, parser, url, good, agent):
7 unittest.TestCase.__init__(self)
8 if good:
9 self.str = "RobotTest(%d, good, %s)" % (index, url)
10 else:
11 self.str = "RobotTest(%d, bad, %s)" % (index, url)
12 self.parser = parser
13 self.url = url
14 self.good = good
15 self.agent = agent
16
17 def runTest(self):
18 if isinstance(self.url, tuple):
19 agent, url = self.url
20 else:
21 url = self.url
22 agent = self.agent
23 if self.good:
24 self.assertTrue(self.parser.can_fetch(agent, url))
25 else:
26 self.assertFalse(self.parser.can_fetch(agent, url))
27
28 def __str__(self):
29 return self.str
30
31tests = unittest.TestSuite()
32
33def RobotTest(index, robots_txt, good_urls, bad_urls,
34 agent="test_robotparser"):
35
36 lines = StringIO.StringIO(robots_txt).readlines()
37 parser = robotparser.RobotFileParser()
38 parser.parse(lines)
39 for url in good_urls:
40 tests.addTest(RobotTestCase(index, parser, url, 1, agent))
41 for url in bad_urls:
42 tests.addTest(RobotTestCase(index, parser, url, 0, agent))
43
44# Examples from http://www.robotstxt.org/wc/norobots.html (fetched 2002)
45
46# 1.
47doc = """
48User-agent: *
49Disallow: /cyberworld/map/ # This is an infinite virtual URL space
50Disallow: /tmp/ # these will soon disappear
51Disallow: /foo.html
52"""
53
54good = ['/','/test.html']
55bad = ['/cyberworld/map/index.html','/tmp/xxx','/foo.html']
56
57RobotTest(1, doc, good, bad)
58
59# 2.
60doc = """
61# robots.txt for http://www.example.com/
62
63User-agent: *
64Disallow: /cyberworld/map/ # This is an infinite virtual URL space
65
66# Cybermapper knows where to go.
67User-agent: cybermapper
68Disallow:
69
70"""
71
72good = ['/','/test.html',('cybermapper','/cyberworld/map/index.html')]
73bad = ['/cyberworld/map/index.html']
74
75RobotTest(2, doc, good, bad)
76
77# 3.
78doc = """
79# go away
80User-agent: *
81Disallow: /
82"""
83
84good = []
85bad = ['/cyberworld/map/index.html','/','/tmp/']
86
87RobotTest(3, doc, good, bad)
88
89# Examples from http://www.robotstxt.org/wc/norobots-rfc.html (fetched 2002)
90
91# 4.
92doc = """
93User-agent: figtree
94Disallow: /tmp
95Disallow: /a%3cd.html
96Disallow: /a%2fb.html
97Disallow: /%7ejoe/index.html
98"""
99
100good = [] # XFAIL '/a/b.html'
101bad = ['/tmp','/tmp.html','/tmp/a.html',
102 '/a%3cd.html','/a%3Cd.html','/a%2fb.html',
103 '/~joe/index.html'
104 ]
105
106RobotTest(4, doc, good, bad, 'figtree')
107RobotTest(5, doc, good, bad, 'FigTree Robot libwww-perl/5.04')
108
109# 6.
110doc = """
111User-agent: *
112Disallow: /tmp/
113Disallow: /a%3Cd.html
114Disallow: /a/b.html
115Disallow: /%7ejoe/index.html
116"""
117
118good = ['/tmp',] # XFAIL: '/a%2fb.html'
119bad = ['/tmp/','/tmp/a.html',
120 '/a%3cd.html','/a%3Cd.html',"/a/b.html",
121 '/%7Ejoe/index.html']
122
123RobotTest(6, doc, good, bad)
124
125# From bug report #523041
126
127# 7.
128doc = """
129User-Agent: *
130Disallow: /.
131"""
132
133good = ['/foo.html']
134bad = [] # Bug report says "/" should be denied, but that is not in the RFC
135
136RobotTest(7, doc, good, bad)
137
138# From Google: http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=40364
139
140# 8.
141doc = """
142User-agent: Googlebot
143Allow: /folder1/myfile.html
144Disallow: /folder1/
145"""
146
147good = ['/folder1/myfile.html']
148bad = ['/folder1/anotherfile.html']
149
150RobotTest(8, doc, good, bad, agent="Googlebot")
151
152# 9. This file is incorrect because "Googlebot" is a substring of
153# "Googlebot-Mobile", so test 10 works just like test 9.
154doc = """
155User-agent: Googlebot
156Disallow: /
157
158User-agent: Googlebot-Mobile
159Allow: /
160"""
161
162good = []
163bad = ['/something.jpg']
164
165RobotTest(9, doc, good, bad, agent="Googlebot")
166
167good = []
168bad = ['/something.jpg']
169
170RobotTest(10, doc, good, bad, agent="Googlebot-Mobile")
171
172# 11. Get the order correct.
173doc = """
174User-agent: Googlebot-Mobile
175Allow: /
176
177User-agent: Googlebot
178Disallow: /
179"""
180
181good = []
182bad = ['/something.jpg']
183
184RobotTest(11, doc, good, bad, agent="Googlebot")
185
186good = ['/something.jpg']
187bad = []
188
189RobotTest(12, doc, good, bad, agent="Googlebot-Mobile")
190
191
192# 13. Google also got the order wrong in #8. You need to specify the
193# URLs from more specific to more general.
194doc = """
195User-agent: Googlebot
196Allow: /folder1/myfile.html
197Disallow: /folder1/
198"""
199
200good = ['/folder1/myfile.html']
201bad = ['/folder1/anotherfile.html']
202
203RobotTest(13, doc, good, bad, agent="googlebot")
204
205
206# 14. For issue #6325 (query string support)
207doc = """
208User-agent: *
209Disallow: /some/path?name=value
210"""
211
212good = ['/some/path']
213bad = ['/some/path?name=value']
214
215RobotTest(14, doc, good, bad)
216
217# 15. For issue #4108 (obey first * entry)
218doc = """
219User-agent: *
220Disallow: /some/path
221
222User-agent: *
223Disallow: /another/path
224"""
225
226good = ['/another/path']
227bad = ['/some/path']
228
229RobotTest(15, doc, good, bad)
230
231# 16. Empty query (issue #17403). Normalizing the url first.
232doc = """
233User-agent: *
234Allow: /some/path?
235Disallow: /another/path?
236"""
237
238good = ['/some/path?']
239bad = ['/another/path?']
240
241RobotTest(16, doc, good, bad)
242
243
244class NetworkTestCase(unittest.TestCase):
245
246 def testPasswordProtectedSite(self):
247 test_support.requires('network')
248 with test_support.transient_internet('mueblesmoraleda.com'):
249 url = 'http://mueblesmoraleda.com'
250 robots_url = url + "/robots.txt"
251 # First check the URL is usable for our purposes, since the
252 # test site is a bit flaky.
253 try:
254 urlopen(robots_url)
255 except HTTPError as e:
256 if e.code not in {401, 403}:
257 self.skipTest(
258 "%r should return a 401 or 403 HTTP error, not %r"
259 % (robots_url, e.code))
260 else:
261 self.skipTest(
262 "%r should return a 401 or 403 HTTP error, not succeed"
263 % (robots_url))
264 parser = robotparser.RobotFileParser()
265 parser.set_url(url)
266 try:
267 parser.read()
268 except IOError:
269 self.skipTest('%s is unavailable' % url)
270 self.assertEqual(parser.can_fetch("*", robots_url), False)
271
272 def testPythonOrg(self):
273 test_support.requires('network')
274 with test_support.transient_internet('www.python.org'):
275 parser = robotparser.RobotFileParser(
276 "http://www.python.org/robots.txt")
277 parser.read()
278 self.assertTrue(
279 parser.can_fetch("*", "http://www.python.org/robots.txt"))
280
281
282def test_main():
283 test_support.run_unittest(tests)
284 test_support.run_unittest(NetworkTestCase)
285
286if __name__=='__main__':
287 test_support.verbose = 1
288 test_main()
Note: See TracBrowser for help on using the repository browser.