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