Skip to content

Commit

Permalink
create variable browsers_support_ch
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdual committed Mar 13, 2024
1 parent 4842a8f commit a1bc57a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/ua_generator/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion src/ua_generator/headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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')
Expand All @@ -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())
Expand Down
15 changes: 8 additions & 7 deletions tests/test_client_hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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')

Expand All @@ -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"'))

Expand Down
9 changes: 5 additions & 4 deletions tests/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand Down

0 comments on commit a1bc57a

Please sign in to comment.