-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from iamdual/headers
Header generation
- Loading branch information
Showing
5 changed files
with
155 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
Random User-Agent | ||
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual) | ||
License: Apache License 2.0 | ||
""" | ||
from .data.generator import Generator | ||
from .client_hints import ClientHints | ||
|
||
|
||
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-CH | ||
class Headers: | ||
|
||
def __init__(self, gen: Generator, ch: ClientHints): | ||
self.__generator = gen | ||
self.__client_hints = ch | ||
self.__headers = { | ||
'user-agent': gen.user_agent, | ||
} | ||
|
||
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Client_hints#low_entropy_hints | ||
if self.__generator.browser in ('chrome', 'edge'): | ||
self.add('sec-ch-ua') | ||
self.add('sec-ch-ua-mobile') | ||
self.add('sec-ch-ua-platform') | ||
|
||
def add(self, key: str): | ||
if key == 'sec-ch-ua': | ||
self.__headers[key] = self.__client_hints.brands | ||
elif key == 'sec-ch-ua-full-version-list': | ||
self.__headers[key] = self.__client_hints.brands_full_version_list | ||
if key == 'sec-ch-ua-platform': | ||
self.__headers[key] = self.__client_hints.platform | ||
elif key == 'sec-ch-ua-platform-version': | ||
self.__headers[key] = self.__client_hints.platform_version | ||
elif key == 'sec-ch-ua-mobile': | ||
self.__headers[key] = self.__client_hints.mobile | ||
|
||
def accept_ch(self, val: str): | ||
if self.__generator.browser not in ('chrome', 'edge'): | ||
return | ||
|
||
requested_hints = val.split(',') | ||
for hint in requested_hints: | ||
self.add(hint.strip().lower()) | ||
|
||
def get(self): | ||
return self.__headers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
""" | ||
Random User-Agent | ||
Copyright: 2022 Ekin Karadeniz (github.com/iamdual) | ||
License: Apache License 2.0 | ||
""" | ||
import unittest | ||
|
||
import src.ua_generator as ua_generator | ||
|
||
|
||
class TestHeaders(unittest.TestCase): | ||
def test_default(self): | ||
for i in range(0, 100): | ||
ua = ua_generator.generate() | ||
self.assertIsNotNone(ua.headers) | ||
self.assertTrue('user-agent' in ua.headers.get()) | ||
|
||
def test_client_hints(self): | ||
for i in range(0, 100): | ||
ua = ua_generator.generate(browser=('chrome', 'edge')) | ||
self.assertIsNotNone(ua.headers) | ||
self.assertTrue('sec-ch-ua' in ua.headers.get()) | ||
self.assertTrue('sec-ch-ua-mobile' in ua.headers.get()) | ||
self.assertTrue('sec-ch-ua-platform' in ua.headers.get()) | ||
|
||
def test_client_hints_not_exists(self): | ||
for i in range(0, 100): | ||
ua = ua_generator.generate(browser='firefox') | ||
self.assertIsNotNone(ua.headers) | ||
self.assertFalse('sec-ch-ua' in ua.headers.get()) | ||
self.assertFalse('sec-ch-ua-mobile' in ua.headers.get()) | ||
self.assertFalse('sec-ch-ua-platform' in ua.headers.get()) | ||
|
||
def test_accept_ch(self): | ||
for i in range(0, 100): | ||
ua = ua_generator.generate(browser=('chrome', 'edge')) | ||
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()) | ||
self.assertTrue('sec-ch-ua-full-version-list' in ua.headers.get()) | ||
|
||
def test_accept_ch_not_exists(self): | ||
for i in range(0, 100): | ||
ua = ua_generator.generate(browser=('chrome', 'edge')) | ||
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()) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |