fastapi/tests/test_starlette_urlconvertors.py
Sebastián Ramírez 0ac9b3ee5c
Re-export utils from Starlette (#1064)
*  Re-export main features used from Starlette to simplify developer's code

* ♻️ Refactor Starlette exports

* ♻️ Refactor tutorial examples to use re-exported utils from Starlette

* 📝 Add examples for all middlewares

* 📝 Add new docs for middlewares

* 📝 Add examples for custom responses

* 📝 Extend docs for custom responses

* 📝 Update docs and add notes explaining re-exports from Starlette everywhere

* 🍱 Update screenshot for HTTP status

* 🔧 Update MkDocs config with new content

* ♻️ Refactor tests to use re-exported utils from Starlette

*  Re-export WebSocketDisconnect from Starlette for tests

*  Add extra tests for extra re-exported middleware

*  Add tests for re-exported responses from Starlette

*  Add docs about mounting WSGI apps

*  Add Flask as a dependency to test WSGIMiddleware

*  Test WSGIMiddleware example
2020-03-01 21:49:20 +01:00

51 lines
1.3 KiB
Python

from fastapi import FastAPI, Path
from fastapi.testclient import TestClient
app = FastAPI()
@app.get("/int/{param:int}")
def int_convertor(param: int = Path(...)):
return {"int": param}
@app.get("/float/{param:float}")
def float_convertor(param: float = Path(...)):
return {"float": param}
@app.get("/path/{param:path}")
def path_convertor(param: str = Path(...)):
return {"path": param}
client = TestClient(app)
def test_route_converters_int():
# Test integer conversion
response = client.get("/int/5")
assert response.status_code == 200
assert response.json() == {"int": 5}
assert app.url_path_for("int_convertor", param=5) == "/int/5"
def test_route_converters_float():
# Test float conversion
response = client.get("/float/25.5")
assert response.status_code == 200
assert response.json() == {"float": 25.5}
assert app.url_path_for("float_convertor", param=25.5) == "/float/25.5"
def test_route_converters_path():
# Test path conversion
response = client.get("/path/some/example")
assert response.status_code == 200
assert response.json() == {"path": "some/example"}
def test_url_path_for_path_convertor():
assert (
app.url_path_for("path_convertor", param="some/example") == "/path/some/example"
)