Skip to content

Commit 642f7fd

Browse files
committed
fix test run error
1 parent 4903ed1 commit 642f7fd

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

src/fastapi_cli/cli.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import asyncio
2+
from enum import Enum
13
from logging import getLogger
24
from pathlib import Path
35
from typing import Any, Union
@@ -16,6 +18,14 @@
1618

1719
app = typer.Typer(rich_markup_mode="rich")
1820

21+
22+
class WSProtocolType(str, Enum):
23+
auto = "auto"
24+
none = "none"
25+
websockets = "websockets"
26+
wsproto = "wsproto"
27+
28+
1929
setup_logging()
2030
logger = getLogger(__name__)
2131

@@ -60,6 +70,12 @@ def _run(
6070
command: str,
6171
app: Union[str, None] = None,
6272
proxy_headers: bool = False,
73+
ws: type[asyncio.Protocol] | WSProtocolType = "auto",
74+
ws_max_size: int = 16777216,
75+
ws_max_queue: int = 32,
76+
ws_ping_interval: float = 20.0,
77+
ws_ping_timeout: float = 20.0,
78+
ws_per_message_deflate: bool = True,
6379
) -> None:
6480
try:
6581
use_uvicorn_app = get_import_string(path=path, app_name=app)
@@ -97,6 +113,12 @@ def _run(
97113
workers=workers,
98114
root_path=root_path,
99115
proxy_headers=proxy_headers,
116+
ws=ws,
117+
ws_max_size=ws_max_size,
118+
ws_max_queue=ws_max_queue,
119+
ws_ping_interval=ws_ping_interval,
120+
ws_ping_timeout=ws_ping_timeout,
121+
ws_per_message_deflate=ws_per_message_deflate,
100122
)
101123

102124

@@ -145,6 +167,32 @@ def dev(
145167
help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info."
146168
),
147169
] = True,
170+
ws: Annotated[
171+
WSProtocolType,
172+
typer.Option(
173+
help="The WebSocket protocol.", case_sensitive=False, show_choices=True
174+
),
175+
] = WSProtocolType.auto,
176+
ws_max_size: Annotated[
177+
int,
178+
typer.Option(help="WebSocket max size message in bytes."),
179+
] = 16777216,
180+
ws_max_queue: Annotated[
181+
int,
182+
typer.Option(help="The maximum length of the WebSocket message queue."),
183+
] = 32,
184+
ws_ping_interval: Annotated[
185+
float,
186+
typer.Option(help="WebSocket ping interval in seconds."),
187+
] = 20.0,
188+
ws_ping_timeout: Annotated[
189+
float,
190+
typer.Option(help="WebSocket ping timeout in seconds."),
191+
] = 20.0,
192+
ws_per_message_deflate: Annotated[
193+
bool,
194+
typer.Option(help="WebSocket per-message-deflate compression"),
195+
] = True,
148196
) -> Any:
149197
"""
150198
Run a [bold]FastAPI[/bold] app in [yellow]development[/yellow] mode. 🧪
@@ -180,6 +228,12 @@ def dev(
180228
app=app,
181229
command="dev",
182230
proxy_headers=proxy_headers,
231+
ws=ws,
232+
ws_max_size=ws_max_size,
233+
ws_max_queue=ws_max_queue,
234+
ws_ping_interval=ws_ping_interval,
235+
ws_ping_timeout=ws_ping_timeout,
236+
ws_per_message_deflate=ws_per_message_deflate,
183237
)
184238

185239

@@ -234,6 +288,32 @@ def run(
234288
help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info."
235289
),
236290
] = True,
291+
ws: Annotated[
292+
WSProtocolType,
293+
typer.Option(
294+
help="The WebSocket protocol.", case_sensitive=False, show_choices=True
295+
),
296+
] = WSProtocolType.auto,
297+
ws_max_size: Annotated[
298+
int,
299+
typer.Option(help="WebSocket max size message in bytes."),
300+
] = 16777216,
301+
ws_max_queue: Annotated[
302+
int,
303+
typer.Option(help="The maximum length of the WebSocket message queue."),
304+
] = 32,
305+
ws_ping_interval: Annotated[
306+
float,
307+
typer.Option(help="WebSocket ping interval in seconds."),
308+
] = 20.0,
309+
ws_ping_timeout: Annotated[
310+
float,
311+
typer.Option(help="WebSocket ping timeout in seconds."),
312+
] = 20.0,
313+
ws_per_message_deflate: Annotated[
314+
bool,
315+
typer.Option(help="WebSocket per-message-deflate compression"),
316+
] = True,
237317
) -> Any:
238318
"""
239319
Run a [bold]FastAPI[/bold] app in [green]production[/green] mode. 🚀
@@ -270,6 +350,12 @@ def run(
270350
app=app,
271351
command="run",
272352
proxy_headers=proxy_headers,
353+
ws=ws,
354+
ws_max_size=ws_max_size,
355+
ws_max_queue=ws_max_queue,
356+
ws_ping_interval=ws_ping_interval,
357+
ws_ping_timeout=ws_ping_timeout,
358+
ws_per_message_deflate=ws_per_message_deflate,
273359
)
274360

275361

tests/test_cli.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ def test_dev() -> None:
2929
"workers": None,
3030
"root_path": "",
3131
"proxy_headers": True,
32+
"ws": "auto",
33+
"ws_max_size": 16777216,
34+
"ws_max_queue": 32,
35+
"ws_ping_interval": 20.0,
36+
"ws_ping_timeout": 20.0,
37+
"ws_per_message_deflate": True,
3238
}
3339
assert "Using import string single_file_app:app" in result.output
3440
assert (
@@ -58,6 +64,8 @@ def test_dev_args() -> None:
5864
"--app",
5965
"api",
6066
"--no-proxy-headers",
67+
"--ws",
68+
"auto",
6169
],
6270
)
6371
assert result.exit_code == 0, result.output
@@ -71,6 +79,12 @@ def test_dev_args() -> None:
7179
"workers": None,
7280
"root_path": "/api",
7381
"proxy_headers": False,
82+
"ws": "auto",
83+
"ws_max_size": 16777216,
84+
"ws_max_queue": 32,
85+
"ws_ping_interval": 20.0,
86+
"ws_ping_timeout": 20.0,
87+
"ws_per_message_deflate": True,
7488
}
7589
assert "Using import string single_file_app:api" in result.output
7690
assert (
@@ -97,6 +111,12 @@ def test_run() -> None:
97111
"workers": None,
98112
"root_path": "",
99113
"proxy_headers": True,
114+
"ws": "auto",
115+
"ws_max_size": 16777216,
116+
"ws_max_queue": 32,
117+
"ws_ping_interval": 20.0,
118+
"ws_ping_timeout": 20.0,
119+
"ws_per_message_deflate": True,
100120
}
101121
assert "Using import string single_file_app:app" in result.output
102122
assert (
@@ -128,6 +148,8 @@ def test_run_args() -> None:
128148
"--app",
129149
"api",
130150
"--no-proxy-headers",
151+
"--ws",
152+
"websockets",
131153
],
132154
)
133155
assert result.exit_code == 0, result.output
@@ -141,6 +163,12 @@ def test_run_args() -> None:
141163
"workers": 2,
142164
"root_path": "/api",
143165
"proxy_headers": False,
166+
"ws": "websockets",
167+
"ws_max_size": 16777216,
168+
"ws_max_queue": 32,
169+
"ws_ping_interval": 20.0,
170+
"ws_ping_timeout": 20.0,
171+
"ws_per_message_deflate": True,
144172
}
145173
assert "Using import string single_file_app:api" in result.output
146174
assert (

0 commit comments

Comments
 (0)