Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 1 | import io |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 2 | import unittest |
| 3 | import urllib.robotparser |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Martin v. Löwis | 1c63f6e | 2002-02-28 15:24:47 +0000 | [diff] [blame] | 5 | |
| 6 | class RobotTestCase(unittest.TestCase): |
| 7 | def __init__(self, index, parser, url, good, agent): |
| 8 | unittest.TestCase.__init__(self) |
| 9 | if good: |
| 10 | self.str = "RobotTest(%d, good, %s)" % (index, url) |
| 11 | else: |
| 12 | self.str = "RobotTest(%d, bad, %s)" % (index, url) |
| 13 | self.parser = parser |
| 14 | self.url = url |
| 15 | self.good = good |
| 16 | self.agent = agent |
| 17 | |
| 18 | def runTest(self): |
| 19 | if isinstance(self.url, tuple): |
| 20 | agent, url = self.url |
| 21 | else: |
| 22 | url = self.url |
| 23 | agent = self.agent |
| 24 | if self.good: |
| 25 | self.failUnless(self.parser.can_fetch(agent, url)) |
| 26 | else: |
| 27 | self.failIf(self.parser.can_fetch(agent, url)) |
| 28 | |
| 29 | def __str__(self): |
| 30 | return self.str |
| 31 | |
| 32 | tests = unittest.TestSuite() |
| 33 | |
| 34 | def RobotTest(index, robots_txt, good_urls, bad_urls, |
| 35 | agent="test_robotparser"): |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 37 | lines = io.StringIO(robots_txt).readlines() |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 38 | parser = urllib.robotparser.RobotFileParser() |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 39 | parser.parse(lines) |
| 40 | for url in good_urls: |
| 41 | tests.addTest(RobotTestCase(index, parser, url, 1, agent)) |
| 42 | for url in bad_urls: |
| 43 | tests.addTest(RobotTestCase(index, parser, url, 0, agent)) |
Martin v. Löwis | 1c63f6e | 2002-02-28 15:24:47 +0000 | [diff] [blame] | 44 | |
| 45 | # Examples from http://www.robotstxt.org/wc/norobots.html (fetched 2002) |
| 46 | |
| 47 | # 1. |
| 48 | doc = """ |
| 49 | User-agent: * |
| 50 | Disallow: /cyberworld/map/ # This is an infinite virtual URL space |
| 51 | Disallow: /tmp/ # these will soon disappear |
| 52 | Disallow: /foo.html |
| 53 | """ |
| 54 | |
| 55 | good = ['/','/test.html'] |
| 56 | bad = ['/cyberworld/map/index.html','/tmp/xxx','/foo.html'] |
| 57 | |
| 58 | RobotTest(1, doc, good, bad) |
| 59 | |
| 60 | # 2. |
| 61 | doc = """ |
| 62 | # robots.txt for http://www.example.com/ |
| 63 | |
| 64 | User-agent: * |
| 65 | Disallow: /cyberworld/map/ # This is an infinite virtual URL space |
| 66 | |
| 67 | # Cybermapper knows where to go. |
| 68 | User-agent: cybermapper |
| 69 | Disallow: |
| 70 | |
| 71 | """ |
| 72 | |
| 73 | good = ['/','/test.html',('cybermapper','/cyberworld/map/index.html')] |
| 74 | bad = ['/cyberworld/map/index.html'] |
| 75 | |
| 76 | RobotTest(2, doc, good, bad) |
| 77 | |
| 78 | # 3. |
| 79 | doc = """ |
| 80 | # go away |
| 81 | User-agent: * |
| 82 | Disallow: / |
| 83 | """ |
| 84 | |
| 85 | good = [] |
| 86 | bad = ['/cyberworld/map/index.html','/','/tmp/'] |
| 87 | |
| 88 | RobotTest(3, doc, good, bad) |
| 89 | |
| 90 | # Examples from http://www.robotstxt.org/wc/norobots-rfc.html (fetched 2002) |
| 91 | |
| 92 | # 4. |
| 93 | doc = """ |
| 94 | User-agent: figtree |
| 95 | Disallow: /tmp |
| 96 | Disallow: /a%3cd.html |
| 97 | Disallow: /a%2fb.html |
| 98 | Disallow: /%7ejoe/index.html |
| 99 | """ |
| 100 | |
| 101 | good = [] # XFAIL '/a/b.html' |
| 102 | bad = ['/tmp','/tmp.html','/tmp/a.html', |
| 103 | '/a%3cd.html','/a%3Cd.html','/a%2fb.html', |
| 104 | '/~joe/index.html' |
| 105 | ] |
| 106 | |
| 107 | RobotTest(4, doc, good, bad, 'figtree') |
| 108 | RobotTest(5, doc, good, bad, 'FigTree Robot libwww-perl/5.04') |
| 109 | |
| 110 | # 6. |
| 111 | doc = """ |
| 112 | User-agent: * |
| 113 | Disallow: /tmp/ |
| 114 | Disallow: /a%3Cd.html |
| 115 | Disallow: /a/b.html |
| 116 | Disallow: /%7ejoe/index.html |
| 117 | """ |
| 118 | |
| 119 | good = ['/tmp',] # XFAIL: '/a%2fb.html' |
| 120 | bad = ['/tmp/','/tmp/a.html', |
| 121 | '/a%3cd.html','/a%3Cd.html',"/a/b.html", |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 122 | '/%7Ejoe/index.html'] |
Martin v. Löwis | 1c63f6e | 2002-02-28 15:24:47 +0000 | [diff] [blame] | 123 | |
| 124 | RobotTest(6, doc, good, bad) |
| 125 | |
| 126 | # From bug report #523041 |
| 127 | |
| 128 | # 7. |
| 129 | doc = """ |
| 130 | User-Agent: * |
| 131 | Disallow: /. |
| 132 | """ |
| 133 | |
| 134 | good = ['/foo.html'] |
| 135 | bad = [] # Bug report says "/" should be denied, but that is not in the RFC |
| 136 | |
| 137 | RobotTest(7, doc, good, bad) |
| 138 | |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 139 | # From Google: http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=40364 |
| 140 | |
| 141 | # 8. |
| 142 | doc = """ |
| 143 | User-agent: Googlebot |
| 144 | Allow: /folder1/myfile.html |
| 145 | Disallow: /folder1/ |
| 146 | """ |
| 147 | |
| 148 | good = ['/folder1/myfile.html'] |
| 149 | bad = ['/folder1/anotherfile.html'] |
| 150 | |
| 151 | RobotTest(8, doc, good, bad, agent="Googlebot") |
| 152 | |
| 153 | # 9. This file is incorrect because "Googlebot" is a substring of |
| 154 | # "Googlebot-Mobile", so test 10 works just like test 9. |
| 155 | doc = """ |
| 156 | User-agent: Googlebot |
| 157 | Disallow: / |
| 158 | |
| 159 | User-agent: Googlebot-Mobile |
| 160 | Allow: / |
| 161 | """ |
| 162 | |
| 163 | good = [] |
| 164 | bad = ['/something.jpg'] |
| 165 | |
| 166 | RobotTest(9, doc, good, bad, agent="Googlebot") |
| 167 | |
| 168 | good = [] |
| 169 | bad = ['/something.jpg'] |
| 170 | |
| 171 | RobotTest(10, doc, good, bad, agent="Googlebot-Mobile") |
| 172 | |
| 173 | # 11. Get the order correct. |
| 174 | doc = """ |
| 175 | User-agent: Googlebot-Mobile |
| 176 | Allow: / |
| 177 | |
| 178 | User-agent: Googlebot |
| 179 | Disallow: / |
| 180 | """ |
| 181 | |
| 182 | good = [] |
| 183 | bad = ['/something.jpg'] |
| 184 | |
| 185 | RobotTest(11, doc, good, bad, agent="Googlebot") |
| 186 | |
| 187 | good = ['/something.jpg'] |
| 188 | bad = [] |
| 189 | |
| 190 | RobotTest(12, doc, good, bad, agent="Googlebot-Mobile") |
| 191 | |
| 192 | |
| 193 | # 13. Google also got the order wrong in #8. You need to specify the |
| 194 | # URLs from more specific to more general. |
| 195 | doc = """ |
| 196 | User-agent: Googlebot |
| 197 | Allow: /folder1/myfile.html |
| 198 | Disallow: /folder1/ |
| 199 | """ |
| 200 | |
| 201 | good = ['/folder1/myfile.html'] |
| 202 | bad = ['/folder1/anotherfile.html'] |
| 203 | |
| 204 | RobotTest(13, doc, good, bad, agent="googlebot") |
| 205 | |
| 206 | |
| 207 | |
Jeremy Hylton | 73fd46d | 2008-07-18 20:59:44 +0000 | [diff] [blame] | 208 | class NetworkTestCase(unittest.TestCase): |
| 209 | |
| 210 | def testPasswordProtectedSite(self): |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 211 | if not support.is_resource_enabled('network'): |
| 212 | return |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 213 | # whole site is password-protected. |
| 214 | url = 'http://mueblesmoraleda.com' |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 215 | parser = urllib.robotparser.RobotFileParser() |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 216 | parser.set_url(url) |
| 217 | parser.read() |
| 218 | self.assertEqual(parser.can_fetch("*", url+"/robots.txt"), False) |
| 219 | |
Jeremy Hylton | 73fd46d | 2008-07-18 20:59:44 +0000 | [diff] [blame] | 220 | def testPythonOrg(self): |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 221 | if not support.is_resource_enabled('network'): |
| 222 | return |
Jeremy Hylton | 73fd46d | 2008-07-18 20:59:44 +0000 | [diff] [blame] | 223 | parser = urllib.robotparser.RobotFileParser( |
| 224 | "http://www.python.org/robots.txt") |
| 225 | parser.read() |
| 226 | self.assertTrue(parser.can_fetch("*", |
| 227 | "http://www.python.org/robots.txt")) |
| 228 | |
Martin v. Löwis | 1c63f6e | 2002-02-28 15:24:47 +0000 | [diff] [blame] | 229 | def test_main(): |
Jeremy Hylton | 73fd46d | 2008-07-18 20:59:44 +0000 | [diff] [blame] | 230 | support.run_unittest(NetworkTestCase) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 231 | support.run_unittest(tests) |
Martin v. Löwis | 1c63f6e | 2002-02-28 15:24:47 +0000 | [diff] [blame] | 232 | |
| 233 | if __name__=='__main__': |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 234 | support.verbose = 1 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 235 | test_main() |