File: test_http.py

package info (click to toggle)
streamlink 8.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,564 kB
  • sloc: python: 51,188; sh: 184; makefile: 152
file content (89 lines) | stat: -rw-r--r-- 2,869 bytes parent folder | download | duplicates (2)
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
from __future__ import annotations

from typing import TYPE_CHECKING
from unittest.mock import Mock, call

import pytest

from streamlink.plugins.http import HTTPStreamPlugin
from streamlink.stream.http import HTTPStream
from tests.plugins import PluginCanHandleUrl


if TYPE_CHECKING:
    from streamlink.session import Streamlink


class TestPluginCanHandleUrlHTTPStreamPlugin(PluginCanHandleUrl):
    __plugin__ = HTTPStreamPlugin

    should_match_groups = [
        # explicit HTTPStream URLs
        ("httpstream://example.com/foo", {"url": "example.com/foo"}),
        ("httpstream://http://example.com/foo", {"url": "http://example.com/foo"}),
        ("httpstream://https://example.com/foo", {"url": "https://example.com/foo"}),
        # optional parameters
        ("httpstream://example.com/foo abc=def", {"url": "example.com/foo", "params": "abc=def"}),
        ("httpstream://http://example.com/foo abc=def", {"url": "http://example.com/foo", "params": "abc=def"}),
        ("httpstream://https://example.com/foo abc=def", {"url": "https://example.com/foo", "params": "abc=def"}),
    ]

    should_not_match = [
        # missing parameters
        "httpstream://example.com/foo ",
    ]


@pytest.mark.parametrize(
    ("url", "expected"),
    [
        ("httpstream://example.com/foo", "https://example.com/foo"),
        ("httpstream://http://example.com/foo", "http://example.com/foo"),
        ("httpstream://https://example.com/foo", "https://example.com/foo"),
    ],
)
def test_get_streams(
    monkeypatch: pytest.MonkeyPatch,
    session: Streamlink,
    url: str,
    expected: str,
):
    mock_httpstream_init = Mock(return_value=None)
    monkeypatch.setattr("streamlink.stream.http.HTTPStream.__init__", mock_httpstream_init)

    plugin = HTTPStreamPlugin(session, url)
    result = plugin.streams()
    result.pop("worst", None)
    result.pop("best", None)

    assert list(result.keys()) == ["live"]
    assert isinstance(result["live"], HTTPStream)
    assert mock_httpstream_init.call_args_list == [call(session, expected)]


def test_parameters(monkeypatch: pytest.MonkeyPatch, session: Streamlink):
    mock_httpstream_init = Mock(return_value=None)
    monkeypatch.setattr("streamlink.stream.http.HTTPStream.__init__", mock_httpstream_init)

    plugin = HTTPStreamPlugin(
        session,
        (
            "httpstream://example.com/foo"
            + " auth=('foo', 'bar')"
            + " verify=False"
            + " referer=https://example2.com/bar"
            + " params={'key': 'a value'}"
        ),
    )
    plugin.streams()

    assert mock_httpstream_init.call_args_list == [
        call(
            session,
            "https://example.com/foo",
            auth=("foo", "bar"),
            verify=False,
            referer="https://example2.com/bar",
            params={"key": "a value"},
        ),
    ]