summaryrefslogtreecommitdiffstats
path: root/tests/conftest.py
blob: a42ccbb5b89a2ce2e1360541b806106adf78c4ca (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
import socket

import pytest
from pytest_httpbin import certs
from pytest_httpbin.serve import Server as PyTestHttpBinServer

from .utils import (  # noqa
    HTTPBIN_WITH_CHUNKED_SUPPORT_DOMAIN,
    HTTPBIN_WITH_CHUNKED_SUPPORT,
    REMOTE_HTTPBIN_DOMAIN,
    IS_PYOPENSSL,
    mock_env
)
from .utils.plugins_cli import (  # noqa
    broken_plugin,
    dummy_plugin,
    dummy_plugins,
    httpie_plugins,
    httpie_plugins_success,
    interface,
)
from .utils.http_server import http_server, localhost_http_server  # noqa


# Patch to support `url = str(server)` in addition to `url = server + '/foo'`.
PyTestHttpBinServer.__str__ = lambda self: self.url


@pytest.fixture(scope='function', autouse=True)
def httpbin_add_ca_bundle(monkeypatch):
    """
    Make pytest-httpbin's CA trusted by default.

    (Same as `httpbin_ca_bundle`, just auto-used.).

    """
    monkeypatch.setenv('REQUESTS_CA_BUNDLE', certs.where())


@pytest.fixture(scope='function')
def httpbin_secure_untrusted(monkeypatch, httpbin_secure):
    """
    Like the `httpbin_secure` fixture, but without the
    make-CA-trusted-by-default.

    """
    monkeypatch.delenv('REQUESTS_CA_BUNDLE')
    return httpbin_secure


@pytest.fixture(scope='session')
def _httpbin_with_chunked_support_available():
    try:
        socket.gethostbyname(HTTPBIN_WITH_CHUNKED_SUPPORT_DOMAIN)
        return True
    except OSError:
        return False


@pytest.fixture(scope='function')
def httpbin_with_chunked_support(_httpbin_with_chunked_support_available):
    if _httpbin_with_chunked_support_available:
        return HTTPBIN_WITH_CHUNKED_SUPPORT
    pytest.skip(f'{HTTPBIN_WITH_CHUNKED_SUPPORT_DOMAIN} not resolvable')


@pytest.fixture(scope='session')
def _remote_httpbin_available():
    try:
        socket.gethostbyname(REMOTE_HTTPBIN_DOMAIN)
        return True
    except OSError:
        return False


@pytest.fixture
def remote_httpbin(_remote_httpbin_available):

    if _remote_httpbin_available:
        class Server(str):
            """Look like `pytest_httpbin.serve.Server` but only provide URL info."""
            @property
            def url(self):
                return self

        return Server('http://' + REMOTE_HTTPBIN_DOMAIN)
    pytest.skip(f'{REMOTE_HTTPBIN_DOMAIN} not resolvable')


@pytest.fixture(autouse=True, scope='session')
def pyopenssl_inject():
    """
    Injects `pyOpenSSL` module to make sure `requests` will use it.
    <https://github.com/psf/requests/pull/5443#issuecomment-645740394>
    """
    if IS_PYOPENSSL:
        try:
            import urllib3.contrib.pyopenssl
            urllib3.contrib.pyopenssl.inject_into_urllib3()
        except ModuleNotFoundError:
            pytest.fail('Missing "pyopenssl" module.')

    yield