import pytest
from eggfetch.compat.httpx import Client, AsyncClient, Timeout
from eggfetch.compat.httpx._transports import HTTPTransport, AsyncHTTPTransport
from eggfetch.compat.httpx._client import (
_convert_socket_option,
_validate_protocol_options,
_validate_transport_options,
)
class TestProtocolValidation:
def test_http1_true_http2_false_ok(self):
client = Client(http1=True, http2=False)
assert client._http1 is True
assert client._http2 is False
def test_http1_true_http2_true_ok(self):
client = Client(http1=True, http2=True)
assert client._http1 is True
assert client._http2 is True
def test_http1_false_http2_true_ok(self):
client = Client(http1=False, http2=True)
assert client._http1 is False
assert client._http2 is True
def test_http1_false_http2_false_raises(self):
with pytest.raises(ValueError, match="At least one of http1 or http2"):
Client(http1=False, http2=False)
def test_async_client_protocol_validation(self):
with pytest.raises(ValueError, match="At least one of http1 or http2"):
AsyncClient(http1=False, http2=False)
def test_async_client_h2_only_ok(self):
client = AsyncClient(http1=False, http2=True)
assert client._http1 is False
assert client._http2 is True
def test_validate_protocol_direct(self):
_validate_protocol_options(True, False) _validate_protocol_options(True, True) _validate_protocol_options(False, True)
def test_validate_protocol_both_false(self):
with pytest.raises(ValueError):
_validate_protocol_options(False, False)
def test_transport_h2_only_ok(self):
transport = HTTPTransport(http1=False, http2=True)
assert transport._http1 is False
assert transport._http2 is True
def test_async_transport_h2_only_ok(self):
transport = AsyncHTTPTransport(http1=False, http2=True)
assert transport._http1 is False
assert transport._http2 is True
class TestTransportOptionsAccepted:
def test_socket_option_bytearray_is_losslessly_converted(self):
value = bytearray(b"\x01\x00\x00\x00")
assert _convert_socket_option((1, 2, value)) == (1, 2, bytes(value))
def test_uds_accepted(self):
transport = HTTPTransport(uds="/tmp/test.sock")
assert transport._uds == "/tmp/test.sock"
def test_local_address_accepted(self):
transport = HTTPTransport(local_address="127.0.0.1")
assert transport._local_address == "127.0.0.1"
def test_socket_options_accepted(self):
opts = [(6, 1, b"\x01\x00\x00\x00")] transport = HTTPTransport(socket_options=opts)
assert transport._socket_options == opts
def test_async_uds_accepted(self):
transport = AsyncHTTPTransport(uds="/tmp/test.sock")
assert transport._uds == "/tmp/test.sock"
def test_async_local_address_accepted(self):
transport = AsyncHTTPTransport(local_address="127.0.0.1")
assert transport._local_address == "127.0.0.1"
def test_async_socket_options_accepted(self):
opts = [(6, 1, b"\x01\x00\x00\x00")] transport = AsyncHTTPTransport(socket_options=opts)
assert transport._socket_options == opts
def test_default_none_values_accepted(self):
transport = HTTPTransport(
uds=None,
local_address=None,
socket_options=None,
)
assert transport._uds is None
assert transport._local_address is None
assert transport._socket_options is None
def test_async_default_none_values_accepted(self):
transport = AsyncHTTPTransport(
uds=None,
local_address=None,
socket_options=None,
)
assert transport._uds is None
assert transport._local_address is None
assert transport._socket_options is None
def test_validate_transport_options_direct(self):
_validate_transport_options() _validate_transport_options(uds=None, local_address=None,
socket_options=None)
def test_validate_transport_options_uds(self):
_validate_transport_options(uds="/tmp/test.sock")
def test_validate_transport_options_local_address(self):
_validate_transport_options(local_address="127.0.0.1")
def test_validate_transport_options_socket_options(self):
_validate_transport_options(socket_options=[(6, 1, b"\x01")])
def test_validate_transport_options_invalid_local_address(self):
with pytest.raises(ValueError, match="invalid local_address"):
_validate_transport_options(local_address="bad-format")
def test_validate_transport_options_invalid_socket_options_type(self):
with pytest.raises(TypeError, match="list of tuples"):
_validate_transport_options(socket_options="not-a-list")
def test_four_tuple_socket_option_is_bounded(self):
with pytest.raises(ValueError, match="four-element"):
HTTPTransport(
socket_options=[(6, 1, b"\x01", 0)],
)
def test_validate_transport_options_invalid_socket_option_triple(self):
with pytest.raises(ValueError, match="triples"):
_validate_transport_options(socket_options=[(1, 2)])