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