blob: 4e530f0d3d874330face0e6fd80d83f296be2733 [file] [log] [blame]
Guido van Rossum34d19282007-08-09 01:03:29 +00001import unittest, robotparser
2import io
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test import support
Martin v. Löwis1c63f6e2002-02-28 15:24:47 +00004
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.failUnless(self.parser.can_fetch(agent, url))
25 else:
26 self.failIf(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"):
Tim Peters863ac442002-04-16 01:38:40 +000035
Guido van Rossum34d19282007-08-09 01:03:29 +000036 lines = io.StringIO(robots_txt).readlines()
Tim Peters863ac442002-04-16 01:38:40 +000037 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))
Martin v. Löwis1c63f6e2002-02-28 15:24:47 +000043
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",
Tim Peters863ac442002-04-16 01:38:40 +0000121 '/%7Ejoe/index.html']
Martin v. Löwis1c63f6e2002-02-28 15:24:47 +0000122
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
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000138class TestCase(unittest.TestCase):
139 def runTest(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000140 support.requires('network')
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000141 # whole site is password-protected.
142 url = 'http://mueblesmoraleda.com'
143 parser = robotparser.RobotFileParser()
144 parser.set_url(url)
145 parser.read()
146 self.assertEqual(parser.can_fetch("*", url+"/robots.txt"), False)
147
Martin v. Löwis1c63f6e2002-02-28 15:24:47 +0000148def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000149 support.run_unittest(tests)
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000150 TestCase().run()
Martin v. Löwis1c63f6e2002-02-28 15:24:47 +0000151
152if __name__=='__main__':
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000153 support.Verbose = 1
Guido van Rossumd8faa362007-04-27 19:54:29 +0000154 test_main()