Skip to content

Commit b030c7b

Browse files
committed
Add a way to add a reference into user-agent.
Indeed, before this patch, it wasn't possible to add a reference to the user-agent without manually giving a custom one. From now on, one can use the `user-agent.reference` to add a reference to the user agent. When set, the reference will then be set to: {user-agent}; +reference
1 parent 372a91a commit b030c7b

File tree

8 files changed

+136
-16
lines changed

8 files changed

+136
-16
lines changed

PyFunceble/cli/entry_points/pyfunceble/cli.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ def get_configured_value(
103103
else:
104104
result = PyFunceble.facility.ConfigLoader.get_configured_value(entry)
105105

106+
if isinstance(result, str) and "%" in result:
107+
result = result.replace("%", "%%")
108+
106109
if negate:
107110
result = not result
108111

@@ -587,6 +590,18 @@ def get_test_control_group_data() -> List[Tuple[List[str], dict]]:
587590
"get the latest (automatically) for you.",
588591
},
589592
),
593+
(
594+
[
595+
"--user-agent-reference",
596+
],
597+
{
598+
"dest": "user_agent.reference",
599+
"type": str,
600+
"help": "Sets the reference to append to the user agent.\n\n"
601+
"This is useful when you want to add a reference to the "
602+
"user agent. %s" % get_configured_value("user_agent.reference"),
603+
},
604+
),
590605
(
591606
[
592607
"-vsc",
@@ -833,15 +848,6 @@ def get_output_control_group_data() -> List[Tuple[List[str], dict]]:
833848
"default": "all",
834849
},
835850
),
836-
(
837-
["--display-datetime"],
838-
{
839-
"dest": "cli_testing.display_mode.datetime",
840-
"action": "store_true",
841-
"help": "Activates or disables the display of the datetime of the\n"
842-
"test. %s" % get_configured_value("cli_testing.display_mode.datetime"),
843-
},
844-
),
845851
(
846852
["--display-datetime-fmt"],
847853
{

PyFunceble/data/infrastructure/.PyFunceble_production.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,17 @@ user_agent:
629629
# CLI Argument: -ua | --user-agent
630630
custom: null
631631

632+
# Set the reference to add to the User-Agent.
633+
# This is useful when you want to add a reference (e.g. a link) to the User-Agent.
634+
#
635+
# The reference will be added at the end of the User-Agent in the following format:
636+
# {user_agent}; +{reference}
637+
#
638+
# When set to `null`, no reference will be added.
639+
#
640+
# CLI Argument: --user-agent-reference
641+
reference: null
642+
632643
proxy:
633644
# Provides everything related to the proxy usage and configuration.
634645
#

PyFunceble/dataset/user_agent.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,30 @@ def is_supported(self, browser_short_name: str, platform: str) -> bool:
196196
and bool(self[browser_short_name.lower()][platform.lower()])
197197
)
198198

199+
def format_user_agent(
200+
self, user_agent: str, *, reference: Optional[str] = None
201+
) -> str:
202+
"""
203+
Given a user agent and a reference, it returns the formatted user agent
204+
that we have to use.
205+
206+
:param user_agent:
207+
The user agent to format.
208+
:param reference:
209+
The reference to append to the user agent.
210+
211+
:return:
212+
The formatted user agent.
213+
"""
214+
215+
user_agent = user_agent.strip()
216+
217+
if reference:
218+
if user_agent.endswith(";"):
219+
return f"{user_agent} +{reference}"
220+
return f"{user_agent}; +{reference}"
221+
return user_agent
222+
199223
def get_latest(self) -> str:
200224
"""
201225
Provides the latest user agent for the given platform.
@@ -205,12 +229,19 @@ def get_latest(self) -> str:
205229
(if exists).
206230
"""
207231

232+
reference = None
233+
208234
if PyFunceble.storage.CONFIGURATION:
235+
reference = PyFunceble.storage.CONFIGURATION.user_agent.reference
236+
209237
if (
210238
PyFunceble.storage.CONFIGURATION.user_agent
211239
and PyFunceble.storage.CONFIGURATION.user_agent.custom
212240
):
213-
return PyFunceble.storage.CONFIGURATION.user_agent.custom
241+
return self.format_user_agent(
242+
PyFunceble.storage.CONFIGURATION.user_agent.custom,
243+
reference=reference,
244+
)
214245

215246
self.set_preferred(
216247
PyFunceble.storage.CONFIGURATION.user_agent.browser,
@@ -220,6 +251,9 @@ def get_latest(self) -> str:
220251
result = self[self.preferred_browser][self.preferred_platform]
221252

222253
if isinstance(result, (list, tuple)):
223-
return secrets.choice(result)
254+
return self.format_user_agent(
255+
secrets.choice(result),
256+
reference=reference,
257+
)
224258

225-
return result
259+
return self.format_user_agent(result, reference=reference)

PyFunceble/storage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
from PyFunceble.storage_facility import get_config_directory
6161

6262
PROJECT_NAME: str = "PyFunceble"
63-
PROJECT_VERSION: str = "4.3.0a11.dev (Blue Duckling: Tulip)"
63+
PROJECT_VERSION: str = "4.3.0a12.dev (Blue Duckling: Tulip)"
6464

6565
DISTRIBUTED_CONFIGURATION_FILENAME: str = ".PyFunceble_production.yaml"
6666

docs/use/configuration/parameters/user-agent.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ user_agent:
3030
#
3131
# CLI Argument: -ua | --user-agent
3232
custom: null
33+
34+
# Set the reference to add to the User-Agent.
35+
# This is useful when you want to add a reference (e.g. a link) to the User-Agent.
36+
#
37+
# The reference will be added at the end of the User-Agent in the following format:
38+
# {user_agent}; +{reference}
39+
#
40+
# When set to `null`, no reference will be added.
41+
#
42+
# CLI Argument: --user-agent-reference
43+
reference: null
3344
```
3445
3546
## `browser`

tests/dataset/test_user_agent.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,35 @@ def test_get_latest_from_config(self) -> None:
507507

508508
self.assertEqual(expected, actual)
509509

510+
def test_get_latest_from_config_with_reference(self) -> None:
511+
"""
512+
Tests the method which let us get the latest user agent known based
513+
on the settings from the configuration file.
514+
515+
We also ensure that the reference is correctly appended.
516+
"""
517+
518+
given_browser = "firefox"
519+
given_platform = "win10"
520+
given_reference = "https://example.org"
521+
522+
self.config_loader.custom_config = {
523+
"user_agent": {
524+
"platform": given_platform,
525+
"browser": given_browser,
526+
"reference": given_reference,
527+
}
528+
}
529+
self.config_loader.start()
530+
531+
expected = (
532+
self.our_dataset[given_browser][given_platform] + "; +" + given_reference
533+
)
534+
535+
actual = self.user_agent_dataset.get_latest()
536+
537+
self.assertEqual(expected, actual)
538+
510539
def test_get_latest_from_config_custom(self) -> None:
511540
"""
512541
Tests the method which let us get the latest user agent known based
@@ -522,6 +551,28 @@ def test_get_latest_from_config_custom(self) -> None:
522551

523552
self.assertEqual(expected, actual)
524553

554+
def test_get_latest_from_custom_config_with_reference(self) -> None:
555+
"""
556+
Tests the method which let us get the latest user agent known based
557+
on the settings from the configuration file.
558+
559+
In this test, we check the case that the given reference is set.
560+
"""
561+
562+
self.config_loader.custom_config = {
563+
"user_agent": {
564+
"custom": "Hello, World!",
565+
"reference": "https://example.org",
566+
}
567+
}
568+
self.config_loader.start()
569+
570+
expected = "Hello, World!; +https://example.org"
571+
572+
actual = self.user_agent_dataset.get_latest()
573+
574+
self.assertEqual(expected, actual)
575+
525576

526577
if __name__ == "__main__":
527578
unittest.main()

tests/pyf_test_dataset.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,9 @@
378378
},
379379
"platform": {
380380
"push": False,
381-
"preferred_data_origin": "frequent",
381+
"preferred_status_origin": "frequent",
382+
"checker_priority": ["none"],
383+
"checker_exclude": ["none"],
382384
},
383385
"debug": {"active": False, "level": "info"},
384386
"dns": {
@@ -462,6 +464,11 @@
462464
},
463465
"proxy": {"global": {"http": None, "https": None}, "rules": []},
464466
"share_logs": False,
465-
"user_agent": {"browser": "chrome", "custom": None, "platform": "linux"},
467+
"user_agent": {
468+
"browser": "chrome",
469+
"custom": None,
470+
"platform": "linux",
471+
"reference": None,
472+
},
466473
"verify_ssl_certificate": False,
467474
}

version.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
current_version: '4.3.0a11.dev (Blue Duckling: Tulip)'
1+
current_version: '4.3.0a12.dev (Blue Duckling: Tulip)'
22
deprecated:
33
- 3.0.21
44
- 3.1.20

0 commit comments

Comments
 (0)