|
3 | 3 | from pathlib import Path |
4 | 4 | from unittest.mock import patch |
5 | 5 |
|
| 6 | +import pytest |
6 | 7 | import uvicorn |
7 | 8 | from typer.testing import CliRunner |
8 | 9 |
|
@@ -424,6 +425,54 @@ def test_run_env_vars_and_args() -> None: |
424 | 425 | assert "Documentation at http://0.0.0.0:8080/docs" in result.output |
425 | 426 |
|
426 | 427 |
|
| 428 | +@pytest.mark.parametrize("command", ["dev", "run"]) |
| 429 | +@pytest.mark.parametrize( |
| 430 | + "public_url", |
| 431 | + [ |
| 432 | + "https://myapp.example.com", |
| 433 | + "https://myapp.example.com/", |
| 434 | + "https://myapp.example.com/subpath/", |
| 435 | + ], |
| 436 | +) |
| 437 | +def test_public_url_env_var(command: str, public_url: str) -> None: |
| 438 | + with changing_dir(assets_path): |
| 439 | + with patch.object(uvicorn, "run") as mock_run: |
| 440 | + result = runner.invoke( |
| 441 | + app, |
| 442 | + [ |
| 443 | + command, |
| 444 | + "single_file_app.py", |
| 445 | + "--host", |
| 446 | + "0.0.0.0", |
| 447 | + ], |
| 448 | + env={"FASTAPI_PUBLIC_URL": public_url}, |
| 449 | + ) |
| 450 | + assert result.exit_code == 0, result.output |
| 451 | + assert mock_run.called |
| 452 | + assert mock_run.call_args |
| 453 | + assert mock_run.call_args.kwargs == { |
| 454 | + "app": "single_file_app:app", |
| 455 | + "host": "0.0.0.0", |
| 456 | + "port": 8000, |
| 457 | + "reload": True if command == "dev" else False, |
| 458 | + "reload_dirs": None, |
| 459 | + "workers": None, |
| 460 | + "root_path": "", |
| 461 | + "proxy_headers": True, |
| 462 | + "forwarded_allow_ips": None, |
| 463 | + "log_config": get_uvicorn_log_config(), |
| 464 | + } |
| 465 | + |
| 466 | + assert "Using import string: single_file_app:app" in result.output |
| 467 | + assert ( |
| 468 | + f"Starting {'development' if command == 'dev' else 'production'} server 🚀" |
| 469 | + in result.output |
| 470 | + ) |
| 471 | + expected_url_base = public_url.rstrip("/") |
| 472 | + assert f"Server started at {expected_url_base}" in result.output |
| 473 | + assert f"Documentation at {expected_url_base}/docs" in result.output |
| 474 | + |
| 475 | + |
427 | 476 | def test_run_error() -> None: |
428 | 477 | with changing_dir(assets_path): |
429 | 478 | result = runner.invoke(app, ["run", "non_existing_file.py"]) |
|
0 commit comments