mirror of https://github.com/tiangolo/fastapi.git
🎨 Auto format
This commit is contained in:
parent
698138d113
commit
7cd6cb6bfb
|
|
@ -1,15 +1,14 @@
|
||||||
import pytest
|
|
||||||
from fastapi.testclient import TestClient
|
|
||||||
from fastapi import FastAPI, Request
|
from fastapi import FastAPI, Request
|
||||||
from fastapi.route_middleware import route_middleware, verify_jwt, log_route
|
from fastapi.route_middleware import log_route, route_middleware, verify_jwt
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
@app.post("/secure")
|
@app.post("/secure")
|
||||||
@route_middleware(verify_jwt, log_route)
|
@route_middleware(verify_jwt, log_route)
|
||||||
async def secure_route(req: Request, is_true: bool):
|
async def secure_route(req: Request, is_true: bool):
|
||||||
|
return {"status": "ok", "is_true": is_true, "user": req.user}
|
||||||
return {"status": "ok", "is_true": is_true,"user":req.user}
|
|
||||||
|
|
||||||
@app.post("/open")
|
@app.post("/open")
|
||||||
async def open_route(is_true: bool):
|
async def open_route(is_true: bool):
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from fastapi import Request
|
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
|
|
||||||
def route_middleware(*middlewares:Callable):
|
from fastapi import Request
|
||||||
def decorator(route_func:Callable):
|
|
||||||
|
|
||||||
|
def route_middleware(*middlewares: Callable):
|
||||||
|
def decorator(route_func: Callable):
|
||||||
@wraps(route_func)
|
@wraps(route_func)
|
||||||
async def wrapper(*args, **kwargs):
|
async def wrapper(*args, **kwargs):
|
||||||
req = kwargs.get("req")
|
req = kwargs.get("req")
|
||||||
|
|
@ -18,17 +20,19 @@ def route_middleware(*middlewares:Callable):
|
||||||
return await route_func(*args, **kwargs)
|
return await route_func(*args, **kwargs)
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
return decorator
|
|
||||||
|
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
# Example middlewares
|
# Example middlewares
|
||||||
async def verify_jwt(req: Request):
|
async def verify_jwt(req: Request):
|
||||||
# just a mock
|
# just a mock
|
||||||
if not (req.query_params.get("is_true") == "true"):
|
if not (req.query_params.get("is_true") == "true"):
|
||||||
req.user={"name":"xyz","admin":True}
|
req.user = {"name": "xyz", "admin": True}
|
||||||
from fastapi import HTTPException, status
|
from fastapi import HTTPException, status
|
||||||
|
|
||||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Invalid JWT")
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Invalid JWT")
|
||||||
|
|
||||||
|
|
||||||
def log_route(req: Request):
|
def log_route(req: Request):
|
||||||
print(f"[LOG] Path: {req.url.path}")
|
print(f"[LOG] Path: {req.url.path}")
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,34 @@
|
||||||
import pytest
|
|
||||||
from fastapi.testclient import TestClient
|
|
||||||
from fastapi import FastAPI, Request
|
from fastapi import FastAPI, Request
|
||||||
from fastapi.route_middleware import route_middleware, verify_jwt, log_route
|
from fastapi.route_middleware import log_route, route_middleware, verify_jwt
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
@app.post("/secure")
|
@app.post("/secure")
|
||||||
@route_middleware(verify_jwt, log_route)
|
@route_middleware(verify_jwt, log_route)
|
||||||
async def secure_route(req: Request, is_true: bool):
|
async def secure_route(req: Request, is_true: bool):
|
||||||
return {"status": "ok", "is_true": is_true,"user":req.user}
|
return {"status": "ok", "is_true": is_true, "user": req.user}
|
||||||
|
|
||||||
|
|
||||||
@app.post("/open")
|
@app.post("/open")
|
||||||
async def open_route(is_true: bool):
|
async def open_route(is_true: bool):
|
||||||
return {"status": "open", "is_true": is_true}
|
return {"status": "open", "is_true": is_true}
|
||||||
|
|
||||||
|
|
||||||
client = TestClient(app)
|
client = TestClient(app)
|
||||||
|
|
||||||
|
|
||||||
def test_secure_route_pass():
|
def test_secure_route_pass():
|
||||||
response = client.post("/secure?is_true=true")
|
response = client.post("/secure?is_true=true")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
def test_secure_route_fail():
|
def test_secure_route_fail():
|
||||||
response = client.post("/secure?is_true=false")
|
response = client.post("/secure?is_true=false")
|
||||||
assert response.status_code == 403
|
assert response.status_code == 403
|
||||||
|
|
||||||
|
|
||||||
def test_open_route():
|
def test_open_route():
|
||||||
response = client.post("/open?is_true=false")
|
response = client.post("/open?is_true=false")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue