Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / lib / python2.4 / test / test_robotparser.py
CommitLineData
920dae64
AT
1import unittest, StringIO, robotparser
2from test import test_support
3
4class RobotTestCase(unittest.TestCase):
5 def __init__(self, index, parser, url, good, agent):
6 unittest.TestCase.__init__(self)
7 if good:
8 self.str = "RobotTest(%d, good, %s)" % (index, url)
9 else:
10 self.str = "RobotTest(%d, bad, %s)" % (index, url)
11 self.parser = parser
12 self.url = url
13 self.good = good
14 self.agent = agent
15
16 def runTest(self):
17 if isinstance(self.url, tuple):
18 agent, url = self.url
19 else:
20 url = self.url
21 agent = self.agent
22 if self.good:
23 self.failUnless(self.parser.can_fetch(agent, url))
24 else:
25 self.failIf(self.parser.can_fetch(agent, url))
26
27 def __str__(self):
28 return self.str
29
30tests = unittest.TestSuite()
31
32def RobotTest(index, robots_txt, good_urls, bad_urls,
33 agent="test_robotparser"):
34
35 lines = StringIO.StringIO(robots_txt).readlines()
36 parser = robotparser.RobotFileParser()
37 parser.parse(lines)
38 for url in good_urls:
39 tests.addTest(RobotTestCase(index, parser, url, 1, agent))
40 for url in bad_urls:
41 tests.addTest(RobotTestCase(index, parser, url, 0, agent))
42
43# Examples from http://www.robotstxt.org/wc/norobots.html (fetched 2002)
44
45# 1.
46doc = """
47User-agent: *
48Disallow: /cyberworld/map/ # This is an infinite virtual URL space
49Disallow: /tmp/ # these will soon disappear
50Disallow: /foo.html
51"""
52
53good = ['/','/test.html']
54bad = ['/cyberworld/map/index.html','/tmp/xxx','/foo.html']
55
56RobotTest(1, doc, good, bad)
57
58# 2.
59doc = """
60# robots.txt for http://www.example.com/
61
62User-agent: *
63Disallow: /cyberworld/map/ # This is an infinite virtual URL space
64
65# Cybermapper knows where to go.
66User-agent: cybermapper
67Disallow:
68
69"""
70
71good = ['/','/test.html',('cybermapper','/cyberworld/map/index.html')]
72bad = ['/cyberworld/map/index.html']
73
74RobotTest(2, doc, good, bad)
75
76# 3.
77doc = """
78# go away
79User-agent: *
80Disallow: /
81"""
82
83good = []
84bad = ['/cyberworld/map/index.html','/','/tmp/']
85
86RobotTest(3, doc, good, bad)
87
88# Examples from http://www.robotstxt.org/wc/norobots-rfc.html (fetched 2002)
89
90# 4.
91doc = """
92User-agent: figtree
93Disallow: /tmp
94Disallow: /a%3cd.html
95Disallow: /a%2fb.html
96Disallow: /%7ejoe/index.html
97"""
98
99good = [] # XFAIL '/a/b.html'
100bad = ['/tmp','/tmp.html','/tmp/a.html',
101 '/a%3cd.html','/a%3Cd.html','/a%2fb.html',
102 '/~joe/index.html'
103 ]
104
105RobotTest(4, doc, good, bad, 'figtree')
106RobotTest(5, doc, good, bad, 'FigTree Robot libwww-perl/5.04')
107
108# 6.
109doc = """
110User-agent: *
111Disallow: /tmp/
112Disallow: /a%3Cd.html
113Disallow: /a/b.html
114Disallow: /%7ejoe/index.html
115"""
116
117good = ['/tmp',] # XFAIL: '/a%2fb.html'
118bad = ['/tmp/','/tmp/a.html',
119 '/a%3cd.html','/a%3Cd.html',"/a/b.html",
120 '/%7Ejoe/index.html']
121
122RobotTest(6, doc, good, bad)
123
124# From bug report #523041
125
126# 7.
127doc = """
128User-Agent: *
129Disallow: /.
130"""
131
132good = ['/foo.html']
133bad = [] # Bug report says "/" should be denied, but that is not in the RFC
134
135RobotTest(7, doc, good, bad)
136
137def test_main():
138 test_support.run_suite(tests)
139
140if __name__=='__main__':
141 test_support.Verbose = 1
142 test_support.run_suite(tests)