1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
From: bastimeyer <mail@bastimeyer.de>
Date: Mon, 16 Feb 2026 12:47:32 +0100
Subject: [PATCH] tests.utils: monkeypatch detect_encoding
Reduce the run time of tests by a bit by not doing
an expensive encoding check that we expect to fail.
Origin: https://github.com/streamlink/streamlink/commit/7c6bca193ad9bf78c8110c7d4c94f276ad1656ca
Author: bastimeyer <mail@bastimeyer.de>
---
tests/utils/test_parse.py | 32 ++++++++++++--------------------
1 file changed, 12 insertions(+), 20 deletions(-)
diff --git a/tests/utils/test_parse.py b/tests/utils/test_parse.py
index cda5610..b1dc34d 100644
--- a/tests/utils/test_parse.py
+++ b/tests/utils/test_parse.py
@@ -1,5 +1,3 @@
-from contextlib import nullcontext
-
import pytest
from lxml.etree import Element
@@ -9,9 +7,6 @@ from streamlink.plugin.api.validate import xml_element
from streamlink.utils.parse import parse_html, parse_json, parse_qsd, parse_xml
-does_not_raise = nullcontext()
-
-
class TestUtilsParse:
def test_parse_json(self):
assert parse_json("{}") == {}
@@ -148,44 +143,41 @@ class TestUtilsParse:
assert tree.xpath(".//body/text()") == [expected]
@pytest.mark.parametrize(
- ("content", "expected", "raises"),
+ ("content", "expected"),
[
pytest.param(
"""<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html><html><body>ä?></body></html>""",
"ä?>",
- does_not_raise,
id="string",
),
pytest.param(
b"""<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html><html><body>\xc3\xa4?></body></html>""",
"ä?>",
- does_not_raise,
id="bytes-utf-8",
),
pytest.param(
b"""<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html><html><body>\xe4?></body></html>""",
"ä?>",
- does_not_raise,
id="bytes-iso-8859-1",
),
- pytest.param(
- b"""<?xml version="1.0" encoding="\x00\xff\xff\x00"?><!DOCTYPE html><html><body>\xc3\xa4?></body></html>""",
- "",
- pytest.raises(PluginError, match=r"^Unable to detect encoding of HTML payload$"),
- id="bytes-invalid",
- ),
pytest.param(
b"""<?xml version="1.0"?><!DOCTYPE html><html><body>\xc3\xa4?></body></html>""",
"ä?>",
- does_not_raise,
id="bytes-unknown",
),
],
)
- def test_parse_html_xhtml5(self, content: bytes | str, expected: str, raises: nullcontext):
- with raises:
- tree = parse_html(content)
- assert tree.xpath(".//body/text()") == [expected]
+ def test_parse_html_xhtml5(self, content: bytes | str, expected: str):
+ tree = parse_html(content)
+ assert tree.xpath(".//body/text()") == [expected]
+
+ def test_parse_html_xhtml5_unknown_encoding(self, monkeypatch: pytest.MonkeyPatch):
+ # monkeypatch the result, so we don't have to do an expensive lookup that we know will fail
+ monkeypatch.setattr("streamlink.utils.parse.detect_encoding", lambda *_: dict(encoding=None))
+ with pytest.raises(PluginError, match=r"^Unable to detect encoding of HTML payload$"):
+ parse_html(
+ b"""<?xml version="1.0" encoding="\x00\xff\xff\x00"?><!DOCTYPE html><html><body>\xc3\xa4?></body></html>""",
+ )
def test_parse_qsd(self):
assert parse_qsd("test=1&foo=bar", schema=validate.Schema({"test": str, "foo": "bar"})) == {"test": "1", "foo": "bar"}
|