示例#1
0
    def _test_fetch_use_fake_user_agent(self):
        """Test `use_fake_user_agent` argument of http.fetch."""
        # Existing headers
        r = http.fetch(self.get_httpbin_url('/status/200'),
                       headers={'user-agent': 'EXISTING'})
        self.assertEqual(r.headers['user-agent'], 'EXISTING')

        # Argument value changes
        r = http.fetch(self.get_httpbin_url('/status/200'),
                       use_fake_user_agent=True)
        self.assertNotEqual(r.headers['user-agent'], http.user_agent())
        r = http.fetch(self.get_httpbin_url('/status/200'),
                       use_fake_user_agent=False)
        self.assertEqual(r.headers['user-agent'], http.user_agent())
        r = http.fetch(self.get_httpbin_url('/status/200'),
                       use_fake_user_agent='ARBITRARY')
        self.assertEqual(r.headers['user-agent'], 'ARBITRARY')

        # Manually overridden domains
        config.fake_user_agent_exceptions = {
            self.get_httpbin_hostname(): 'OVERRIDDEN'
        }
        r = http.fetch(self.get_httpbin_url('/status/200'),
                       use_fake_user_agent=False)
        self.assertEqual(r.headers['user-agent'], 'OVERRIDDEN')
示例#2
0
 def test_version(self):
     old_cache = pywikibot.version.cache
     try:
         pywikibot.version.cache = None
         http.user_agent(format_string='version does not appear')
         self.assertIsNone(pywikibot.version.cache)
         pywikibot.version.cache = {'rev': 'dummy'}
         self.assertEqual(http.user_agent(format_string='{version} does appear'),
                          'dummy does appear')
         self.assertIsNotNone(pywikibot.version.cache)
     finally:
         pywikibot.version.cache = old_cache
示例#3
0
 def test_version(self):
     old_cache = pywikibot.version.cache
     try:
         pywikibot.version.cache = None
         http.user_agent(format_string='version does not appear')
         self.assertIsNone(pywikibot.version.cache)
         pywikibot.version.cache = {'rev': 'dummy'}
         self.assertEqual(
             http.user_agent(format_string='{version} does appear'),
             'dummy does appear')
         self.assertIsNotNone(pywikibot.version.cache)
     finally:
         pywikibot.version.cache = old_cache
示例#4
0
    def _test_config_settings(self):
        """Test if method honours configuration toggle."""
        # ON: True and None in config are considered turned on.
        config.fake_user_agent = True
        self.assertNotEqual(http.get_fake_user_agent(), http.user_agent())
        config.fake_user_agent = None
        self.assertNotEqual(http.get_fake_user_agent(), http.user_agent())

        # OFF: All other values won't make it return random UA.
        config.fake_user_agent = False
        self.assertEqual(http.get_fake_user_agent(), http.user_agent())
        config.fake_user_agent = 'ARBITRARY'
        self.assertEqual(http.get_fake_user_agent(), 'ARBITRARY')
示例#5
0
 def test_default_user_agent(self):
     """Config defined format string test."""
     self.assertTrue(http.user_agent().startswith(
         pywikibot.calledModuleName()))
     self.assertIn('Pywikibot/' + pywikibot.__release__, http.user_agent())
     self.assertNotIn('  ', http.user_agent())
     self.assertNotIn('()', http.user_agent())
     self.assertNotIn('(;', http.user_agent())
     self.assertNotIn(';)', http.user_agent())
     self.assertIn('httplib2/', http.user_agent())
     self.assertIn('Python/' + str(sys.version_info[0]), http.user_agent())
 def test_default_user_agent(self):
     """Config defined format string test."""
     self.assertTrue(http.user_agent().startswith(
         pywikibot.calledModuleName()))
     self.assertIn('Pywikibot/' + pywikibot.__release__, http.user_agent())
     self.assertNotIn('  ', http.user_agent())
     self.assertNotIn('()', http.user_agent())
     self.assertNotIn('(;', http.user_agent())
     self.assertNotIn(';)', http.user_agent())
     self.assertIn('requests/', http.user_agent())
     self.assertIn('Python/' + str(PYTHON_VERSION[0]), http.user_agent())
示例#7
0
 def test_default_user_agent(self):
     """Config defined format string test."""
     self.assertTrue(http.user_agent().startswith(
         pywikibot.calledModuleName()))
     self.assertIn('Pywikibot/' + pywikibot.__release__, http.user_agent())
     self.assertNotIn('  ', http.user_agent())
     self.assertNotIn('()', http.user_agent())
     self.assertNotIn('(;', http.user_agent())
     self.assertNotIn(';)', http.user_agent())
     self.assertIn('requests/', http.user_agent())
     self.assertIn('Python/' + str(PYTHON_VERSION[0]), http.user_agent())
示例#8
0
 def test_default_user_agent(self):
     """Config defined format string test."""
     self.assertTrue(http.user_agent().startswith(
         pywikibot.calledModuleName()))
     self.assertIn('Pywikibot/' + pywikibot.__release__, http.user_agent())
     self.assertNotIn('  ', http.user_agent())
     self.assertNotIn('()', http.user_agent())
     self.assertNotIn('(;', http.user_agent())
     self.assertNotIn(';)', http.user_agent())
     self.assertIn('httplib2/', http.user_agent())
     self.assertIn('Python/' + str(sys.version_info[0]), http.user_agent())
示例#9
0
    def test_user_agent(self):
        self.assertEqual('', http.user_agent(format_string='  '))
        self.assertEqual('', http.user_agent(format_string=' '))
        self.assertEqual('a', http.user_agent(format_string=' a '))

        # if there is no site, these can't have a value
        self.assertEqual('', http.user_agent(format_string='{username}'))
        self.assertEqual('', http.user_agent(format_string='{family}'))
        self.assertEqual('', http.user_agent(format_string='{lang}'))

        self.assertEqual('Pywikibot/' + pywikibot.__release__,
                         http.user_agent(format_string='{pwb}'))
        self.assertNotIn(' ', http.user_agent(format_string=' {pwb} '))

        self.assertIn('Pywikibot/' + pywikibot.__release__,
                      http.user_agent(format_string='SVN/1.7.5 {pwb}'))
示例#10
0
    def test_user_agent(self):
        self.assertEqual('', http.user_agent(format_string='  '))
        self.assertEqual('', http.user_agent(format_string=' '))
        self.assertEqual('a', http.user_agent(format_string=' a '))

        # if there is no site, these can't have a value
        self.assertEqual('', http.user_agent(format_string='{username}'))
        self.assertEqual('', http.user_agent(format_string='{family}'))
        self.assertEqual('', http.user_agent(format_string='{lang}'))

        self.assertEqual('Pywikibot/' + pywikibot.__release__,
                         http.user_agent(format_string='{pwb}'))
        self.assertNotIn(' ', http.user_agent(format_string=' {pwb} '))

        self.assertIn('Pywikibot/' + pywikibot.__release__,
                      http.user_agent(format_string='SVN/1.7.5 {pwb}'))
示例#11
0
    def _test_fetch_use_fake_user_agent(self):
        """Test `use_fake_user_agent` argument of http.fetch."""
        # Existing headers
        r = http.fetch(
            self.get_httpbin_url('/status/200'), headers={'user-agent': 'EXISTING'})
        self.assertEqual(r.headers['user-agent'], 'EXISTING')

        # Argument value changes
        r = http.fetch(self.get_httpbin_url('/status/200'), use_fake_user_agent=True)
        self.assertNotEqual(r.headers['user-agent'], http.user_agent())
        r = http.fetch(self.get_httpbin_url('/status/200'), use_fake_user_agent=False)
        self.assertEqual(r.headers['user-agent'], http.user_agent())
        r = http.fetch(
            self.get_httpbin_url('/status/200'), use_fake_user_agent='ARBITRARY')
        self.assertEqual(r.headers['user-agent'], 'ARBITRARY')

        # Manually overridden domains
        config.fake_user_agent_exceptions = {self.get_httpbin_hostname(): 'OVERRIDDEN'}
        r = http.fetch(
            self.get_httpbin_url('/status/200'), use_fake_user_agent=False)
        self.assertEqual(r.headers['user-agent'], 'OVERRIDDEN')
示例#12
0
    def _test_fetch_use_fake_user_agent(self):
        """Test `use_fake_user_agent` argument of http.fetch."""
        # Existing headers
        r = http.fetch(
            self.get_httpbin_url('/status/200'),
            headers={'user-agent': 'EXISTING'})
        self.assertEqual(r.request.headers['user-agent'], 'EXISTING')

        # Argument value changes
        r = http.fetch(self.get_httpbin_url('/status/200'),
                       use_fake_user_agent=True)
        self.assertNotEqual(r.request.headers['user-agent'], http.user_agent())
        r = http.fetch(self.get_httpbin_url('/status/200'),
                       use_fake_user_agent=False)
        self.assertEqual(r.request.headers['user-agent'], http.user_agent())
        r = http.fetch(
            self.get_httpbin_url('/status/200'),
            use_fake_user_agent='ARBITRARY')
        self.assertEqual(r.request.headers['user-agent'], 'ARBITRARY')

        # Empty value
        with self.assertRaisesRegex(
                ValueError,
                'Invalid parameter: use_fake_user_agent'):
            http.fetch(self.get_httpbin_url('/status/200'),
                       use_fake_user_agent='')

        # Parameter wrongly set to None
        with self.assertRaisesRegex(
                ValueError,
                'Invalid parameter: use_fake_user_agent'):
            http.fetch(self.get_httpbin_url('/status/200'),
                       use_fake_user_agent=None)

        # Manually overridden domains
        config.fake_user_agent_exceptions = {
            self.get_httpbin_hostname(): 'OVERRIDDEN'}
        r = http.fetch(
            self.get_httpbin_url('/status/200'), use_fake_user_agent=False)
        self.assertEqual(r.request.headers['user-agent'], 'OVERRIDDEN')
示例#13
0
 def __init__(self, **kwargs: Any) -> None:
     """Initializer."""
     super().__init__(**kwargs)
     self.nu_session = requests.Session()
     self.nu_session.headers['user-agent'] = user_agent(
         kwargs['site'],
         '{script_product} ({script_comments}) {http_backend} {python}',
     )
     self.nu_session.params = {'level': 'error', 'out': 'json'}
     self.templates = get_redirects({
         pywikibot.Page(self.site, 'Invalid SVG', ns=10),
         pywikibot.Page(self.site, 'Valid SVG', ns=10),
     })
    def test_user_agent(self):
        """Test different variants of user agents."""
        x = self.get_site()

        x._userinfo = {'name': 'foo'}
        x._username = ('foo', None)

        self.assertEqual('Pywikibot/' + pywikibot.__release__,
                         user_agent(x, format_string='{pwb}'))

        self.assertEqual(x.family.name,
                         user_agent(x, format_string='{family}'))
        self.assertEqual(x.code,
                         user_agent(x, format_string='{lang}'))
        self.assertEqual(x.family.name + ' ' + x.code,
                         user_agent(x, format_string='{family} {lang}'))

        self.assertEqual(x.username(),
                         user_agent(x, format_string='{username}'))

        x._userinfo = {'name': u'!'}
        x._username = (u'!', None)

        self.assertEqual('!', user_agent(x, format_string='{username}'))

        x._userinfo = {'name': u'foo bar'}
        x._username = (u'foo bar', None)

        self.assertEqual('foo_bar', user_agent(x, format_string='{username}'))

        old_config = '{script}/{version} Pywikibot/2.0 (User:{username})'

        pywikibot.version.getversiondict()
        script_value = pywikibot.calledModuleName() + '/' + pywikibot.version.cache['rev']

        self.assertEqual(script_value + ' Pywikibot/2.0 (User:foo_bar)',
                         user_agent(x, format_string=old_config))

        x._userinfo = {'name': u'⁂'}
        x._username = (u'⁂', None)

        self.assertEqual('%E2%81%82',
                         user_agent(x, format_string='{username}'))

        x._userinfo = {'name': u'127.0.0.1'}
        x._username = (None, None)

        self.assertEqual('Foo', user_agent(x, format_string='Foo {username}'))
        self.assertEqual('Foo (' + x.family.name + ':' + x.code + ')',
                         user_agent(x, format_string='Foo ({script_comments})'))
示例#15
0
    def test_user_agent(self):
        x = self.get_site()

        x._userinfo = {'name': 'foo'}
        x._username = ('foo', None)

        self.assertEqual('Pywikibot/' + pywikibot.__release__,
                         user_agent(x, format_string='{pwb}'))

        self.assertEqual(x.family.name, user_agent(x,
                                                   format_string='{family}'))
        self.assertEqual(x.code, user_agent(x, format_string='{lang}'))
        self.assertEqual(x.family.name + ' ' + x.code,
                         user_agent(x, format_string='{family} {lang}'))

        self.assertEqual(x.username(), user_agent(x,
                                                  format_string='{username}'))

        x._userinfo = {'name': u'!'}
        x._username = (u'!', None)

        self.assertEqual('!', user_agent(x, format_string='{username}'))

        x._userinfo = {'name': u'foo bar'}
        x._username = (u'foo bar', None)

        self.assertEqual('foo_bar', user_agent(x, format_string='{username}'))

        old_config = '{script}/{version} Pywikibot/2.0 (User:{username})'

        pywikibot.version.getversiondict()
        script_value = pywikibot.calledModuleName(
        ) + '/' + pywikibot.version.cache['rev']

        self.assertEqual(script_value + ' Pywikibot/2.0 (User:foo_bar)',
                         user_agent(x, format_string=old_config))

        x._userinfo = {'name': u'⁂'}
        x._username = (u'⁂', None)

        self.assertEqual('%E2%81%82', user_agent(x,
                                                 format_string='{username}'))

        x._userinfo = {'name': u'127.0.0.1'}
        x._username = (None, None)

        self.assertEqual('Foo', user_agent(x, format_string='Foo {username}'))
        self.assertEqual(
            'Foo (' + x.family.name + ':' + x.code + ')',
            user_agent(x, format_string='Foo ({script_comments})'))
示例#16
0
    def test_user_agent(self):
        """Test different variants of user agents."""
        x = self.get_site()

        x._userinfo = {'name': 'foo'}
        x._username = '******'

        self.assertEqual('Pywikibot/' + pywikibot.__version__,
                         user_agent(x, format_string='{pwb}'))

        self.assertEqual(x.family.name, user_agent(x,
                                                   format_string='{family}'))
        self.assertEqual(x.code, user_agent(x, format_string='{lang}'))
        self.assertEqual(x.family.name + ' ' + x.code,
                         user_agent(x, format_string='{family} {lang}'))

        self.assertEqual(x.username(), user_agent(x,
                                                  format_string='{username}'))

        x._userinfo = {'name': '!'}
        x._username = '******'

        self.assertEqual('!', user_agent(x, format_string='{username}'))

        x._userinfo = {'name': 'foo bar'}
        x._username = '******'

        self.assertEqual('foo_bar', user_agent(x, format_string='{username}'))

        old_config = '{script}/{version} Pywikibot/6.0 (User:{username})'

        script_value = (pywikibot.calledModuleName() + '/' +
                        pywikibot.version.getversiondict()['rev'])

        self.assertEqual(script_value + ' Pywikibot/6.0 (User:foo_bar)',
                         user_agent(x, format_string=old_config))

        x._userinfo = {'name': '⁂'}
        x._username = '******'

        self.assertEqual('%E2%81%82', user_agent(x,
                                                 format_string='{username}'))

        x._userinfo = {'name': '127.0.0.1'}
        x._username = None

        self.assertEqual('Foo', user_agent(x, format_string='Foo {username}'))
        self.assertEqual(
            'Foo (' + x.family.name + ':' + x.code + ')',
            user_agent(x, format_string='Foo ({script_comments})'))