Skip to content

Commit

Permalink
[test] Add HTTP proxy tests (#9578)
Browse files Browse the repository at this point in the history
Also fixes HTTPS proxies for curl_cffi

Authored by: coletdjnz
  • Loading branch information
coletdjnz committed May 10, 2024
1 parent 98d71d8 commit 3c7a287
Show file tree
Hide file tree
Showing 6 changed files with 596 additions and 180 deletions.
50 changes: 44 additions & 6 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import functools
import inspect

import pytest
Expand All @@ -10,17 +9,56 @@

@pytest.fixture
def handler(request):
RH_KEY = request.param
RH_KEY = getattr(request, 'param', None)
if not RH_KEY:
return
if inspect.isclass(RH_KEY) and issubclass(RH_KEY, RequestHandler):
handler = RH_KEY
elif RH_KEY in _REQUEST_HANDLERS:
handler = _REQUEST_HANDLERS[RH_KEY]
else:
pytest.skip(f'{RH_KEY} request handler is not available')

return functools.partial(handler, logger=FakeLogger)
class HandlerWrapper(handler):
RH_KEY = handler.RH_KEY

def __init__(self, *args, **kwargs):
super().__init__(logger=FakeLogger, *args, **kwargs)

def validate_and_send(rh, req):
rh.validate(req)
return rh.send(req)
return HandlerWrapper


@pytest.fixture(autouse=True)
def skip_handler(request, handler):
"""usage: pytest.mark.skip_handler('my_handler', 'reason')"""
for marker in request.node.iter_markers('skip_handler'):
if marker.args[0] == handler.RH_KEY:
pytest.skip(marker.args[1] if len(marker.args) > 1 else '')


@pytest.fixture(autouse=True)
def skip_handler_if(request, handler):
"""usage: pytest.mark.skip_handler_if('my_handler', lambda request: True, 'reason')"""
for marker in request.node.iter_markers('skip_handler_if'):
if marker.args[0] == handler.RH_KEY and marker.args[1](request):
pytest.skip(marker.args[2] if len(marker.args) > 2 else '')


@pytest.fixture(autouse=True)
def skip_handlers_if(request, handler):
"""usage: pytest.mark.skip_handlers_if(lambda request, handler: True, 'reason')"""
for marker in request.node.iter_markers('skip_handlers_if'):
if handler and marker.args[0](request, handler):
pytest.skip(marker.args[1] if len(marker.args) > 1 else '')


def pytest_configure(config):
config.addinivalue_line(
"markers", "skip_handler(handler): skip test for the given handler",
)
config.addinivalue_line(
"markers", "skip_handler_if(handler): skip test for the given handler if condition is true"
)
config.addinivalue_line(
"markers", "skip_handlers_if(handler): skip test for handlers when the condition is true"
)
5 changes: 5 additions & 0 deletions test/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,8 @@ def http_server_port(httpd):
def verify_address_availability(address):
if find_available_port(address) is None:
pytest.skip(f'Unable to bind to source address {address} (address may not exist)')


def validate_and_send(rh, req):
rh.validate(req)
return rh.send(req)

0 comments on commit 3c7a287

Please sign in to comment.