From a1bc57a2fc516f7e46caef0497ca8bc6f92964a3 Mon Sep 17 00:00:00 2001 From: iamdual Date: Thu, 14 Mar 2024 00:49:10 +0300 Subject: [PATCH] create variable browsers_support_ch --- src/ua_generator/data/__init__.py | 5 +++-- src/ua_generator/headers.py | 6 +++++- tests/test_client_hints.py | 15 ++++++++------- tests/test_headers.py | 9 +++++---- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/ua_generator/data/__init__.py b/src/ua_generator/data/__init__.py index 1576bac..7ab060b 100644 --- a/src/ua_generator/data/__init__.py +++ b/src/ua_generator/data/__init__.py @@ -7,7 +7,8 @@ devices = ('desktop', 'mobile') platforms = ('windows', 'macos', 'ios', 'linux', 'android') -platforms_desktop = ('windows', 'macos', 'linux') -platforms_mobile = ('ios', 'android') +platforms_desktop = ('windows', 'macos', 'linux') # Platforms on desktop devices +platforms_mobile = ('ios', 'android') # Platforms on mobile devices browsers = ('chrome', 'edge', 'firefox', 'safari') +browsers_support_ch = ('chrome', 'edge') # Browsers that support Client Hints diff --git a/src/ua_generator/headers.py b/src/ua_generator/headers.py index 5eb4c99..7502000 100644 --- a/src/ua_generator/headers.py +++ b/src/ua_generator/headers.py @@ -3,6 +3,7 @@ Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual) License: Apache License 2.0 """ +from .data import browsers_support_ch from .data.generator import Generator from .client_hints import ClientHints @@ -23,7 +24,7 @@ def reset(self): } # https://developer.mozilla.org/en-US/docs/Web/HTTP/Client_hints#low_entropy_hints - if self.__generator.browser in ('chrome', 'edge'): + if self.__generator.browser in browsers_support_ch: self.add('sec-ch-ua') self.add('sec-ch-ua-mobile') self.add('sec-ch-ua-platform') @@ -50,6 +51,9 @@ def add(self, key: str): def accept_ch(self, val: str): self.reset() + if self.__generator.browser not in browsers_support_ch: + return + requested_hints = val.split(',') for hint in requested_hints: self.add(hint.strip().lower()) diff --git a/tests/test_client_hints.py b/tests/test_client_hints.py index ee933b7..ee2c937 100644 --- a/tests/test_client_hints.py +++ b/tests/test_client_hints.py @@ -6,24 +6,25 @@ import unittest import src.ua_generator as ua_generator +from src.ua_generator.data import browsers_support_ch class TestClientHints(unittest.TestCase): def test_ch_platform(self): for i in range(0, 100): - ua = ua_generator.generate(browser=('chrome', 'edge'), platform='macos') + ua = ua_generator.generate(browser=browsers_support_ch, platform='macos') self.assertIsNotNone(ua.ch) self.assertEqual(ua.ch.platform, '"macOS"') def test_ch_platform_2(self): for i in range(0, 100): - ua = ua_generator.generate(browser=('chrome', 'edge'), platform='linux') + ua = ua_generator.generate(browser=browsers_support_ch, platform='linux') self.assertIsNotNone(ua.ch) self.assertEqual(ua.ch.platform, '"Linux"') def test_ch_platform_version(self): for i in range(0, 100): - ua = ua_generator.generate(browser=('chrome', 'edge')) + ua = ua_generator.generate(browser=browsers_support_ch) self.assertIsNotNone(ua.ch) self.assertTrue(type(ua.ch.platform_version) is str) self.assertTrue(len(ua.ch.platform_version) > 0) @@ -37,13 +38,13 @@ def test_ch_platform_version_windows(self): def test_ch_mobile(self): for i in range(0, 100): - ua = ua_generator.generate(browser=('chrome', 'edge'), platform='android') + ua = ua_generator.generate(browser=browsers_support_ch, platform='android') self.assertIsNotNone(ua.ch) self.assertEqual(ua.ch.mobile, '?1') def test_ch_non_mobile(self): for i in range(0, 100): - ua = ua_generator.generate(browser=('chrome', 'edge'), platform='windows') + ua = ua_generator.generate(browser=browsers_support_ch, platform='windows') self.assertIsNotNone(ua.ch) self.assertEqual(ua.ch.mobile, '?0') @@ -65,13 +66,13 @@ def test_ch_brands_full_version_list(self): def test_ch_bitness(self): for i in range(0, 100): - ua = ua_generator.generate(browser=('chrome', 'edge')) + ua = ua_generator.generate(browser=browsers_support_ch) self.assertIsNotNone(ua.ch) self.assertIn(ua.ch.bitness, ('"32"', '"64"')) def test_ch_architecture(self): for i in range(0, 100): - ua = ua_generator.generate(browser=('chrome', 'edge')) + ua = ua_generator.generate(browser=browsers_support_ch) self.assertIsNotNone(ua.ch) self.assertIn(ua.ch.architecture, ('"arm"', '"x86"')) diff --git a/tests/test_headers.py b/tests/test_headers.py index 002f2e1..5c65dbf 100644 --- a/tests/test_headers.py +++ b/tests/test_headers.py @@ -6,6 +6,7 @@ import unittest import src.ua_generator as ua_generator +from src.ua_generator.data import browsers_support_ch class TestHeaders(unittest.TestCase): @@ -17,7 +18,7 @@ def test_default(self): def test_client_hints(self): for i in range(0, 100): - ua = ua_generator.generate(browser=('chrome', 'edge')) + ua = ua_generator.generate(browser=browsers_support_ch) self.assertIsNotNone(ua.headers) self.assertTrue('sec-ch-ua' in ua.headers.get()) self.assertTrue('sec-ch-ua-mobile' in ua.headers.get()) @@ -33,7 +34,7 @@ def test_client_hints_not_exists(self): def test_accept_ch(self): for i in range(0, 100): - ua = ua_generator.generate(browser=('chrome', 'edge')) + ua = ua_generator.generate(browser=browsers_support_ch) ua.headers.accept_ch('Sec-CH-UA-Platform-Version, Sec-CH-UA-Full-Version-List') self.assertTrue('sec-ch-ua-platform-version' in ua.headers.get()) @@ -47,7 +48,7 @@ def test_accept_ch(self): def test_reset(self): for i in range(0, 100): - ua = ua_generator.generate(browser=('chrome', 'edge')) + ua = ua_generator.generate(browser=browsers_support_ch) ua.headers.add('sec-ch-ua-bitness') self.assertTrue('user-agent' in ua.headers.get()) @@ -61,7 +62,7 @@ def test_reset(self): def test_accept_ch_not_exists(self): for i in range(0, 100): - ua = ua_generator.generate(browser=('chrome', 'edge')) + ua = ua_generator.generate(browser=browsers_support_ch) ua.headers.accept_ch('Sec-CH-Example') self.assertFalse('sec-ch-ua-platform-version' in ua.headers.get()) self.assertFalse('sec-ch-ua-full-version-list' in ua.headers.get())