Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Sep 16, 2024
1 parent d451af4 commit 8e3bd96
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 37 deletions.
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ dependencies = [
# NOTE: 👇
# The versions of `black`, `ruff`, `codespell`, must be consistent with the `.pre-commit-config.yaml`.
# Don't edit them manually, use `pre-commit run ver_sync` instead.
"black==24.3.0",
"ruff==0.3.4",
"codespell==2.2.6",
"black==24.8.0",
"ruff==0.6.5",
"codespell==2.3.0",
# Don't write comments on these lines, because they will be removed by `pre-commit run ver_sync`.
# NOTE: 👆

Expand Down
4 changes: 2 additions & 2 deletions src/fastapi_proxy_lib/core/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,10 +490,10 @@ async def send_request_to_target( # pyright: ignore [reportIncompatibleMethodOv

# httpx.stream()
# refer to: https://www.python-httpx.org/api/#helper-functions
client_request_headers: "HeaderTypes" = _change_client_header(
client_request_headers: HeaderTypes = _change_client_header(
headers=websocket.headers, target_url=target_url
)
client_request_params: "QueryParamTypes" = websocket.query_params
client_request_params: QueryParamTypes = websocket.query_params

# TODO: 是否可以不检查http版本?
check_result = check_http_version(websocket.scope, SUPPORTED_WS_HTTP_VERSIONS)
Expand Down
19 changes: 10 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
import pytest
import uvicorn
from asgi_lifespan import LifespanManager
from typing_extensions import ParamSpec

from fastapi_proxy_lib.fastapi.app import (
forward_http_app,
reverse_http_app,
reverse_ws_app,
)
from typing_extensions import ParamSpec

from .app.echo_http_app import get_app as get_http_test_app
from .app.echo_ws_app import get_app as get_ws_test_app
Expand Down Expand Up @@ -69,13 +70,13 @@ def __call__( # noqa: D102


# https://anyio.readthedocs.io/en/stable/testing.html#specifying-the-backends-to-run-on
@pytest.fixture()
@pytest.fixture
def anyio_backend() -> Literal["asyncio"]:
"""Specify the async backend for `pytest.mark.anyio`."""
return "asyncio"


@pytest.fixture()
@pytest.fixture
async def lifespan_manager() -> AsyncIterator[LifespanManagerFixture]:
"""Fixture for asgi lifespan manager.
Expand Down Expand Up @@ -104,7 +105,7 @@ async def _lifespan_manager(app: ASGIApp) -> ASGIApp:
# 所以这里明确要求每个fixture的作用域都是"function",不要共享 TestAppDataclass


@pytest.fixture()
@pytest.fixture
async def echo_http_test_model(
lifespan_manager: LifespanManagerFixture,
) -> LifeAppDataclass4Test:
Expand All @@ -120,7 +121,7 @@ def LifeAppDataclass4Test.request(): Get the latest original http request from t
return LifeAppDataclass4Test(app=life_app, request_dict=app_dataclass.request_dict)


@pytest.fixture()
@pytest.fixture
async def echo_ws_test_model(
lifespan_manager: LifespanManagerFixture,
) -> LifeAppDataclass4Test:
Expand Down Expand Up @@ -158,7 +159,7 @@ async def wappered_app_fct(*args: _P.args, **kwargs: _P.kwargs) -> ASGIApp:
return wappered_app_fct


@pytest.fixture()
@pytest.fixture
def forward_http_app_fct(
lifespan_manager: LifespanManagerFixture,
): # -> AppFactoryFixture
Expand All @@ -169,7 +170,7 @@ def forward_http_app_fct(
return _app_fct_life_wapper(forward_http_app, lifespan_manager)


@pytest.fixture()
@pytest.fixture
def reverse_http_app_fct(
lifespan_manager: LifespanManagerFixture,
): # -> AppFactoryFixture
Expand All @@ -180,7 +181,7 @@ def reverse_http_app_fct(
return _app_fct_life_wapper(reverse_http_app, lifespan_manager)


@pytest.fixture()
@pytest.fixture
def reverse_ws_app_fct(
lifespan_manager: LifespanManagerFixture,
): # -> AppFactoryFixture
Expand All @@ -191,7 +192,7 @@ def reverse_ws_app_fct(
return _app_fct_life_wapper(reverse_ws_app, lifespan_manager)


@pytest.fixture()
@pytest.fixture
async def uvicorn_server_fixture() -> AsyncIterator[UvicornServerFixture]:
"""Fixture for UvicornServer.
Expand Down
5 changes: 3 additions & 2 deletions tests/test_core_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import httpx
import pytest
from fastapi import APIRouter, FastAPI
from starlette.responses import JSONResponse

from fastapi_proxy_lib.core._tool import (
BaseURLError,
ErrMsg,
Expand All @@ -13,7 +15,6 @@
from fastapi_proxy_lib.core.http import ReverseHttpProxy
from fastapi_proxy_lib.fastapi.app import forward_http_app, reverse_http_app
from fastapi_proxy_lib.fastapi.router import RouterHelper
from starlette.responses import JSONResponse

from .tool import DEFAULT_URL

Expand Down Expand Up @@ -42,7 +43,7 @@ def test_base_url_cheking_when_init() -> None:
assert check_base_url("http://www.echo.com/?p=1#foo") == "http://www.echo.com/"


@pytest.mark.anyio()
@pytest.mark.anyio
async def test_func_return_err_msg_response() -> None:
"""Test `fastapi_proxy_lib.core._tool.return_err_msg_response()`."""

Expand Down
12 changes: 8 additions & 4 deletions tests/test_docs_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ def test_forward_http_proxy() -> None:
from typing import AsyncIterator

from fastapi import FastAPI
from fastapi_proxy_lib.core.http import ForwardHttpProxy
from fastapi_proxy_lib.core.tool import default_proxy_filter
from httpx import AsyncClient
from starlette.requests import Request

from fastapi_proxy_lib.core.http import ForwardHttpProxy
from fastapi_proxy_lib.core.tool import default_proxy_filter

proxy = ForwardHttpProxy(AsyncClient(), proxy_filter=default_proxy_filter)

@asynccontextmanager
Expand All @@ -37,10 +38,11 @@ def test_reverse_http_proxy() -> None:
from typing import AsyncIterator

from fastapi import FastAPI
from fastapi_proxy_lib.core.http import ReverseHttpProxy
from httpx import AsyncClient
from starlette.requests import Request

from fastapi_proxy_lib.core.http import ReverseHttpProxy

proxy = ReverseHttpProxy(AsyncClient(), base_url="http://www.example.com/")

@asynccontextmanager
Expand Down Expand Up @@ -75,10 +77,11 @@ def test_reverse_ws_proxy() -> None:
from typing import AsyncIterator

from fastapi import FastAPI
from fastapi_proxy_lib.core.websocket import ReverseWebSocketProxy
from httpx import AsyncClient
from starlette.websockets import WebSocket

from fastapi_proxy_lib.core.websocket import ReverseWebSocketProxy

proxy = ReverseWebSocketProxy(AsyncClient(), base_url="ws://echo.websocket.events/")

@asynccontextmanager
Expand All @@ -101,6 +104,7 @@ async def _(websocket: WebSocket, path: str = ""):
def test_router_helper() -> None:
"""测试 RouterHelper 中的例子."""
from fastapi import APIRouter, FastAPI

from fastapi_proxy_lib.core.http import ForwardHttpProxy, ReverseHttpProxy
from fastapi_proxy_lib.core.tool import default_proxy_filter
from fastapi_proxy_lib.core.websocket import ReverseWebSocketProxy
Expand Down
25 changes: 13 additions & 12 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

import httpx
import pytest
from fastapi_proxy_lib.core.tool import default_proxy_filter
from typing_extensions import override

from fastapi_proxy_lib.core.tool import default_proxy_filter

from .conftest import AppFactoryFixture, LifeAppDataclass4Test
from .tool import (
DEFAULT_URL,
Expand All @@ -24,7 +25,7 @@ class TestReverseHttpProxy(AbstractTestProxy):
"""For testing reverse http proxy."""

@override
@pytest.fixture()
@pytest.fixture
async def tool_4_test_fixture( # pyright: ignore[reportIncompatibleMethodOverride]
self,
echo_http_test_model: LifeAppDataclass4Test,
Expand Down Expand Up @@ -54,7 +55,7 @@ async def tool_4_test_fixture( # pyright: ignore[reportIncompatibleMethodOverri
proxy_server_base_url=DEFAULT_PROXY_SERVER_BASE_URL,
)

@pytest.mark.anyio()
@pytest.mark.anyio
async def test_all_request_methods(
self, tool_4_test_fixture: Tool4TestFixture
) -> None:
Expand All @@ -75,7 +76,7 @@ async def test_all_request_methods(
)
assert all(resp.is_success for resp in resp_lst)

@pytest.mark.anyio()
@pytest.mark.anyio
async def test_if_the_header_is_properly_handled(
self, tool_4_test_fixture: Tool4TestFixture
) -> None:
Expand Down Expand Up @@ -153,7 +154,7 @@ async def test_if_the_header_is_properly_handled(
)
assert "close" in proxy_resp.headers["connection"]

@pytest.mark.anyio()
@pytest.mark.anyio
async def test_if_the_proxy_forwarding_is_correct(
self, tool_4_test_fixture: Tool4TestFixture
) -> None:
Expand Down Expand Up @@ -184,7 +185,7 @@ async def test_if_the_proxy_forwarding_is_correct(
)
assert r.content.decode("utf-8") == file_str

@pytest.mark.anyio()
@pytest.mark.anyio
async def test_bad_url_request(
self,
reverse_http_app_fct: AppFactoryFixture,
Expand All @@ -205,7 +206,7 @@ async def test_bad_url_request(
assert r.status_code == 502
check_if_err_resp_is_from_px_serv(r)

@pytest.mark.anyio()
@pytest.mark.anyio
async def test_cookie_leakage(
self,
tool_4_test_fixture: Tool4TestFixture,
Expand Down Expand Up @@ -244,7 +245,7 @@ async def test_cookie_leakage(
class TestForwardHttpProxy(AbstractTestProxy):
"""For testing forward http proxy."""

@pytest.fixture()
@pytest.fixture
async def tool_4_test_fixture( # pyright: ignore[reportIncompatibleMethodOverride]
self,
echo_http_test_model: LifeAppDataclass4Test,
Expand Down Expand Up @@ -273,7 +274,7 @@ async def tool_4_test_fixture( # pyright: ignore[reportIncompatibleMethodOverri
proxy_server_base_url=DEFAULT_PROXY_SERVER_BASE_URL,
)

@pytest.mark.anyio()
@pytest.mark.anyio
async def test_all_request_methods(
self, tool_4_test_fixture: Tool4TestFixture
) -> None:
Expand All @@ -297,7 +298,7 @@ async def test_all_request_methods(
)
assert all(resp.is_success for resp in resp_lst)

@pytest.mark.anyio()
@pytest.mark.anyio
async def test_bad_url_request(
self,
forward_http_app_fct: AppFactoryFixture,
Expand Down Expand Up @@ -332,7 +333,7 @@ async def test_bad_url_request(
assert r.status_code == 403
check_if_err_resp_is_from_px_serv(r)

@pytest.mark.anyio()
@pytest.mark.anyio
async def test_500_proxy_server_internal_error(
self,
forward_http_app_fct: AppFactoryFixture,
Expand Down Expand Up @@ -365,7 +366,7 @@ async def connect_error_mock_handler(
assert r.status_code == 500
check_if_err_resp_is_from_px_serv(r)

@pytest.mark.anyio()
@pytest.mark.anyio
async def test_denial_http2(
self,
forward_http_app_fct: AppFactoryFixture,
Expand Down
11 changes: 6 additions & 5 deletions tests/test_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
import httpx_ws
import pytest
import uvicorn
from fastapi_proxy_lib.fastapi.app import reverse_ws_app as get_reverse_ws_app
from httpx_ws import aconnect_ws
from starlette import websockets as starlette_websockets_module
from typing_extensions import override

from fastapi_proxy_lib.fastapi.app import reverse_ws_app as get_reverse_ws_app

from .app.echo_ws_app import get_app as get_ws_test_app
from .app.tool import UvicornServer
from .conftest import UvicornServerFixture
Expand Down Expand Up @@ -150,7 +151,7 @@ async def tool_4_test_fixture( # pyright: ignore[reportIncompatibleMethodOverri
proxy_server_base_url=proxy_server_base_url,
)

@pytest.mark.anyio()
@pytest.mark.anyio
async def test_ws_proxy(self, tool_4_test_fixture: Tool4TestFixture) -> None:
"""测试websocket代理."""
proxy_server_base_url = tool_4_test_fixture.proxy_server_base_url
Expand Down Expand Up @@ -208,7 +209,7 @@ async def test_ws_proxy(self, tool_4_test_fixture: Tool4TestFixture) -> None:
# NOTE: 这个测试不放在 `test_target_server_shutdown_abnormally` 来做
# 是因为这里已经有现成的target server,放在这里测试可以节省启动服务器时间

aconnect_ws_subprocess_queue: "Queue[str]" = Queue()
aconnect_ws_subprocess_queue: Queue[str] = Queue()

kwargs_async_client = {"proxies": NO_PROXIES}
kwargs_aconnect_ws = {"url": proxy_server_base_url + "do_nothing"}
Expand Down Expand Up @@ -247,7 +248,7 @@ async def test_ws_proxy(self, tool_4_test_fixture: Tool4TestFixture) -> None:

# FIXME: 调查为什么收到关闭代码需要40s
@pytest.mark.timeout(60)
@pytest.mark.anyio()
@pytest.mark.anyio
@pytest.mark.parametrize("ws_backend", WS_BACKENDS_NEED_BE_TESTED)
async def test_target_server_shutdown_abnormally(
self, ws_backend: Literal["websockets", "wsproto"]
Expand All @@ -256,7 +257,7 @@ async def test_target_server_shutdown_abnormally(
需要在 60s 内向客户端发送 1011 关闭代码.
"""
subprocess_queue: "Queue[str]" = Queue()
subprocess_queue: Queue[str] = Queue()

target_ws_server_subprocess = Process(
target=_subprocess_run_echo_ws_uvicorn_server,
Expand Down

0 comments on commit 8e3bd96

Please sign in to comment.