|
4 | 4 | from unittest.mock import patch
|
5 | 5 |
|
6 | 6 | import uvicorn
|
7 |
| -from fastapi_cli.cli import app |
| 7 | +from fastapi_cli.cli import DEFAULT_DOCS_URL, app |
8 | 8 | from typer.testing import CliRunner
|
9 | 9 |
|
10 | 10 | from tests.utils import changing_dir
|
@@ -221,3 +221,55 @@ def test_script() -> None:
|
221 | 221 | encoding="utf-8",
|
222 | 222 | )
|
223 | 223 | assert "Usage" in result.stdout
|
| 224 | + |
| 225 | + |
| 226 | +def test_app_with_docs_url_set_should_show_correctly_url_in_stdout() -> None: |
| 227 | + with changing_dir(assets_path): |
| 228 | + with patch.object(uvicorn, "run") as mock_run: |
| 229 | + result = runner.invoke(app, ["dev", "with_docs_url_set.py"]) |
| 230 | + assert result.exit_code == 0, result.output |
| 231 | + assert mock_run.called |
| 232 | + assert mock_run.call_args |
| 233 | + assert mock_run.call_args.kwargs == { |
| 234 | + "app": "with_docs_url_set:app", |
| 235 | + "host": "127.0.0.1", |
| 236 | + "port": 8000, |
| 237 | + "reload": True, |
| 238 | + "workers": None, |
| 239 | + "root_path": "", |
| 240 | + "proxy_headers": True, |
| 241 | + } |
| 242 | + assert "Using import string with_docs_url_set:app" in result.output |
| 243 | + assert ( |
| 244 | + "╭────────── FastAPI CLI - Development mode ───────────╮" in result.output |
| 245 | + ) |
| 246 | + assert "│ Serving at: http://127.0.0.1:8000" in result.output |
| 247 | + assert "│ API docs: http://127.0.0.1:8000/any-other-path" in result.output |
| 248 | + assert "│ Running in development mode, for production use:" in result.output |
| 249 | + assert "│ fastapi run" in result.output |
| 250 | + |
| 251 | + |
| 252 | +def test_app_without_docs_url_set_should_show_default_url_in_stdout() -> None: |
| 253 | + with changing_dir(assets_path): |
| 254 | + with patch.object(uvicorn, "run") as mock_run: |
| 255 | + result = runner.invoke(app, ["dev", "without_docs_url_set.py"]) |
| 256 | + assert result.exit_code == 0, result.output |
| 257 | + assert mock_run.called |
| 258 | + assert mock_run.call_args |
| 259 | + assert mock_run.call_args.kwargs == { |
| 260 | + "app": "without_docs_url_set:app", |
| 261 | + "host": "127.0.0.1", |
| 262 | + "port": 8000, |
| 263 | + "reload": True, |
| 264 | + "workers": None, |
| 265 | + "root_path": "", |
| 266 | + "proxy_headers": True, |
| 267 | + } |
| 268 | + assert "Using import string without_docs_url_set:app" in result.output |
| 269 | + assert ( |
| 270 | + "╭────────── FastAPI CLI - Development mode ───────────╮" in result.output |
| 271 | + ) |
| 272 | + assert "│ Serving at: http://127.0.0.1:8000" in result.output |
| 273 | + assert f"│ API docs: http://127.0.0.1:8000{DEFAULT_DOCS_URL}" in result.output |
| 274 | + assert "│ Running in development mode, for production use:" in result.output |
| 275 | + assert "│ fastapi run" in result.output |
0 commit comments