File: test_can_handle_url.py

package info (click to toggle)
streamlink 8.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,568 kB
  • sloc: python: 51,299; sh: 184; makefile: 152
file content (86 lines) | stat: -rw-r--r-- 2,547 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
from __future__ import annotations

import re
from typing import TYPE_CHECKING

import pytest
import requests_mock as rm

import streamlink_cli.main
from streamlink.plugin import Plugin, pluginmatcher


if TYPE_CHECKING:
    from streamlink.session import Streamlink


@pytest.fixture(autouse=True)
def _plugins(session: Streamlink):
    @pluginmatcher(re.compile(r"http://exists$"))
    class FakePlugin(Plugin):
        def _get_streams(self):  # pragma: no cover
            pass

    session.plugins.update({"plugin": FakePlugin})


@pytest.mark.parametrize(
    ("argv", "exit_code"),
    [
        pytest.param(
            ["--can-handle-url", "http://aborted"],
            130,
            id="aborted",
        ),
        pytest.param(
            ["--can-handle-url", "http://exists"],
            0,
            id="exists",
        ),
        pytest.param(
            ["--can-handle-url", "http://exists-redirect"],
            0,
            id="exists-redirect",
        ),
        pytest.param(
            ["--can-handle-url", "http://missing"],
            1,
            id="missing",
        ),
        pytest.param(
            ["--can-handle-url", "http://missing-redirect"],
            1,
            id="missing-redirect",
        ),
        pytest.param(
            ["--can-handle-url-no-redirect", "http://exists"],
            0,
            id="noredirect-exists",
        ),
        pytest.param(
            ["--can-handle-url-no-redirect", "http://exists-redirect"],
            1,
            id="noredirect-exists-redirect",
        ),
        pytest.param(
            ["--can-handle-url-no-redirect", "http://missing"],
            1,
            id="noredirect-missing",
        ),
        pytest.param(
            ["--can-handle-url-no-redirect", "http://missing-redirect"],
            1,
            id="noredirect-missing-redirect",
        ),
    ],
    indirect=["argv"],
)
def test_can_handle_url(requests_mock: rm.Mocker, session: Streamlink, argv: list, exit_code: int):
    requests_mock.request(rm.ANY, "http://aborted", exc=KeyboardInterrupt)  # type: ignore[arg-type]
    requests_mock.request(rm.ANY, "http://exists", content=b"")
    requests_mock.request(rm.ANY, "http://exists-redirect", status_code=301, headers={"Location": "http://exists"})
    requests_mock.request(rm.ANY, "http://missing-redirect", status_code=301, headers={"Location": "http://missing"})

    with pytest.raises(SystemExit) as exc_info:
        streamlink_cli.main.main()
    assert exc_info.value.code == exit_code