jenkins-bot has submitted this change and it was merged.
Change subject: Add fake user agent test case ......................................................................
Add fake user agent test case
This test case will check if fake UA generation can be toggled on/off with the config file, and if the method generates a random UA every time. Tests repeat for the two UA-generating modules, browseragents and fake_useragent.
Bug: T124005 Change-Id: Id9b6ff2d62917f84cfabb4af115cc7a887e42762 --- M tests/http_tests.py 1 file changed, 52 insertions(+), 1 deletion(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/tests/http_tests.py b/tests/http_tests.py index 35d705f..05df31b 100644 --- a/tests/http_tests.py +++ b/tests/http_tests.py @@ -24,7 +24,7 @@ )
from tests import join_images_path -from tests.aspects import unittest, TestCase, DeprecationTestCase +from tests.aspects import unittest, TestCase, DeprecationTestCase, require_modules
class HttpTestCase(TestCase): @@ -285,6 +285,57 @@ self.assertIn('Python/' + str(PYTHON_VERSION[0]), http.user_agent())
+class FakeUserAgentTestCase(TestCase): + + """Test the generation of fake user agents. + + If the method cannot import either browseragents or fake_useragent, the + default user agent will be returned, causing tests to fail. Therefore tests + will skip if neither is present. + """ + + net = False + + def setUp(self): + """Set up unit test.""" + self.orig_fake_user_agent = config.fake_user_agent + + def tearDown(self): + """Tear down unit test.""" + config.fake_user_agent = self.orig_fake_user_agent + + def _test_fake_user_agent_config(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 = 'ArbitraryValue' + self.assertEqual(http.get_fake_user_agent(), 'ArbitraryValue') + + def _test_fake_user_agent_randomness(self): + """Test if user agent returns are randomized.""" + config.fake_user_agent = True + self.assertNotEqual(http.get_fake_user_agent(), http.get_fake_user_agent()) + + @require_modules('browseragents') + def test_with_browseragents(self): + """Test fake user agent generation with browseragents module.""" + self._test_fake_user_agent_config() + self._test_fake_user_agent_randomness() + + @require_modules('fake_useragent') + def test_with_fake_useragent(self): + """Test fake user agent generation with fake_useragent module.""" + self._test_fake_user_agent_config() + self._test_fake_user_agent_randomness() + + class CharsetTestCase(TestCase):
"""Test that HttpRequest correct handles the charsets given."""