| 1 |
import pytest |
| 2 |
from fastapi import APIRouter, FastAPI |
| 3 |
from fastapi.exceptions import FastAPIError |
| 4 |
from fastapi.testclient import TestClient |
| 5 |
|
| 6 |
app = FastAPI() |
| 7 |
|
| 8 |
router = APIRouter() |
| 9 |
|
| 10 |
|
| 11 |
@router.get("") |
| 12 |
def get_empty(): |
| 13 |
return ["OK"] |
| 14 |
|
| 15 |
|
| 16 |
app.include_router(router, prefix="/prefix") |
| 17 |
|
| 18 |
|
| 19 |
client = TestClient(app) |
| 20 |
|
| 21 |
|
| 22 |
def test_use_empty(): |
| 23 |
with client: |
| 24 |
response = client.get("/prefix") |
| 25 |
assert response.status_code == 200, response.text |
| 26 |
assert response.json() == ["OK"] |
| 27 |
|
| 28 |
response = client.get("/prefix/") |
| 29 |
assert response.status_code == 200, response.text |
| 30 |
assert response.json() == ["OK"] |
| 31 |
|
| 32 |
|
| 33 |
def test_include_empty(): |
| 34 |
# if both include and router.path are empty - it should raise exception |
| 35 |
with pytest.raises(FastAPIError): |
| 36 |
app.include_router(router) |