Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_robotparser.py

    r2 r391  
    11import unittest, StringIO, robotparser
    22from test import test_support
     3from urllib2 import urlopen, HTTPError
    34
    45class RobotTestCase(unittest.TestCase):
     
    2122            agent = self.agent
    2223        if self.good:
    23             self.failUnless(self.parser.can_fetch(agent, url))
     24            self.assertTrue(self.parser.can_fetch(agent, url))
    2425        else:
    25             self.failIf(self.parser.can_fetch(agent, url))
     26            self.assertFalse(self.parser.can_fetch(agent, url))
    2627
    2728    def __str__(self):
     
    203204
    204205
    205 
    206 class TestCase(unittest.TestCase):
    207     def runTest(self):
     206# 14. For issue #6325 (query string support)
     207doc = """
     208User-agent: *
     209Disallow: /some/path?name=value
     210"""
     211
     212good = ['/some/path']
     213bad = ['/some/path?name=value']
     214
     215RobotTest(14, doc, good, bad)
     216
     217# 15. For issue #4108 (obey first * entry)
     218doc = """
     219User-agent: *
     220Disallow: /some/path
     221
     222User-agent: *
     223Disallow: /another/path
     224"""
     225
     226good = ['/another/path']
     227bad = ['/some/path']
     228
     229RobotTest(15, doc, good, bad)
     230
     231# 16. Empty query (issue #17403). Normalizing the url first.
     232doc = """
     233User-agent: *
     234Allow: /some/path?
     235Disallow: /another/path?
     236"""
     237
     238good = ['/some/path?']
     239bad = ['/another/path?']
     240
     241RobotTest(16, doc, good, bad)
     242
     243
     244class NetworkTestCase(unittest.TestCase):
     245
     246    def testPasswordProtectedSite(self):
    208247        test_support.requires('network')
    209         # whole site is password-protected.
    210         url = 'http://mueblesmoraleda.com'
    211         parser = robotparser.RobotFileParser()
    212         parser.set_url(url)
    213         parser.read()
    214         self.assertEqual(parser.can_fetch("*", url+"/robots.txt"), False)
     248        with test_support.transient_internet('mueblesmoraleda.com'):
     249            url = 'http://mueblesmoraleda.com'
     250            robots_url = url + "/robots.txt"
     251            # First check the URL is usable for our purposes, since the
     252            # test site is a bit flaky.
     253            try:
     254                urlopen(robots_url)
     255            except HTTPError as e:
     256                if e.code not in {401, 403}:
     257                    self.skipTest(
     258                        "%r should return a 401 or 403 HTTP error, not %r"
     259                        % (robots_url, e.code))
     260            else:
     261                self.skipTest(
     262                    "%r should return a 401 or 403 HTTP error, not succeed"
     263                    % (robots_url))
     264            parser = robotparser.RobotFileParser()
     265            parser.set_url(url)
     266            try:
     267                parser.read()
     268            except IOError:
     269                self.skipTest('%s is unavailable' % url)
     270            self.assertEqual(parser.can_fetch("*", robots_url), False)
     271
     272    def testPythonOrg(self):
     273        test_support.requires('network')
     274        with test_support.transient_internet('www.python.org'):
     275            parser = robotparser.RobotFileParser(
     276                "http://www.python.org/robots.txt")
     277            parser.read()
     278            self.assertTrue(
     279                parser.can_fetch("*", "http://www.python.org/robots.txt"))
     280
    215281
    216282def test_main():
    217283    test_support.run_unittest(tests)
    218     TestCase().run()
     284    test_support.run_unittest(NetworkTestCase)
    219285
    220286if __name__=='__main__':
Note: See TracChangeset for help on using the changeset viewer.