committed by
GitHub
7 changed files with 345 additions and 17 deletions
@ -0,0 +1,23 @@ |
|||
import json |
|||
|
|||
from fastapi import Depends, FastAPI, HTTPException |
|||
|
|||
app = FastAPI() |
|||
|
|||
|
|||
data = { |
|||
"plumbus": {"description": "Freshly pickled plumbus", "owner": "Morty"}, |
|||
"portal-gun": {"description": "Gun to create portals", "owner": "Rick"}, |
|||
} |
|||
|
|||
|
|||
def set_username(): |
|||
response = yield |
|||
response.headers["X-Username"] = json.loads(response.body)["owner"] |
|||
|
|||
|
|||
@app.get("/items/{item_id}", dependencies=[Depends(set_username)]) |
|||
def get_item(item_id: str): |
|||
if item_id not in data: |
|||
raise HTTPException(status_code=404, detail="Item not found") |
|||
return data[item_id] |
@ -0,0 +1,73 @@ |
|||
from typing import AsyncGenerator, Generator |
|||
|
|||
from fastapi import Depends, FastAPI, Response |
|||
from fastapi.testclient import TestClient |
|||
|
|||
app = FastAPI() |
|||
|
|||
|
|||
def dependency_gen(value: str) -> Generator[str, None, None]: |
|||
response: Response = yield |
|||
assert isinstance(response, Response) |
|||
assert response.status_code == 200 |
|||
assert response.body == b'"response_get_dependency_gen"' |
|||
response.headers["X-Test"] = value |
|||
|
|||
|
|||
async def dependency_async_gen(value: str) -> AsyncGenerator[str, None]: |
|||
response = yield |
|||
assert isinstance(response, Response) |
|||
assert response.status_code == 200 |
|||
assert response.body == b'"response_get_dependency_async_gen"' |
|||
response.headers["X-Test"] = value |
|||
|
|||
|
|||
async def sub_dependency_async_gen(value: str) -> AsyncGenerator[str, None]: |
|||
response = yield |
|||
assert isinstance(response, Response) |
|||
response.status_code = 201 |
|||
assert response.body == b'"response_get_sub_dependency_async_gen"' |
|||
response.headers["X-Test"] = value |
|||
|
|||
|
|||
async def parent_dependency(result=Depends(sub_dependency_async_gen)): |
|||
return result |
|||
|
|||
|
|||
@app.get("/dependency-gen", dependencies=[Depends(dependency_gen)]) |
|||
async def get_dependency_gen(): |
|||
return "response_get_dependency_gen" |
|||
|
|||
|
|||
@app.get("/dependency-async-gen", dependencies=[Depends(dependency_async_gen)]) |
|||
async def get_dependency_async_gen(): |
|||
return "response_get_dependency_async_gen" |
|||
|
|||
|
|||
@app.get("/sub-dependency-gen", dependencies=[Depends(parent_dependency)]) |
|||
async def get_sub_dependency_gen(): |
|||
return "response_get_sub_dependency_async_gen" |
|||
|
|||
|
|||
client = TestClient(app) |
|||
|
|||
|
|||
def test_dependency_gen(): |
|||
response = client.get("/dependency-gen", params={"value": "test"}) |
|||
assert response.status_code == 200 |
|||
assert response.content == b'"response_get_dependency_gen"' |
|||
assert response.headers["X-Test"] == "test" |
|||
|
|||
|
|||
def test_dependency_async_gen(): |
|||
response = client.get("/dependency-async-gen", params={"value": "test"}) |
|||
assert response.status_code == 200 |
|||
assert response.content == b'"response_get_dependency_async_gen"' |
|||
assert response.headers["X-Test"] == "test" |
|||
|
|||
|
|||
def test_sub_dependency_gen(): |
|||
response = client.get("/sub-dependency-gen", params={"value": "test"}) |
|||
assert response.status_code == 201 |
|||
assert response.content == b'"response_get_sub_dependency_async_gen"' |
|||
assert response.headers["X-Test"] == "test" |
@ -0,0 +1,22 @@ |
|||
from fastapi.testclient import TestClient |
|||
|
|||
from docs_src.dependencies.tutorial008e import app |
|||
|
|||
client = TestClient(app) |
|||
|
|||
|
|||
def test_get_no_item(): |
|||
response = client.get("/items/foo") |
|||
assert response.status_code == 404, response.text |
|||
assert response.json() == {"detail": "Item not found"} |
|||
assert "X-Username" not in response.headers |
|||
|
|||
|
|||
def test_get(): |
|||
response = client.get("/items/plumbus") |
|||
assert response.status_code == 200, response.text |
|||
assert response.json() == { |
|||
"description": "Freshly pickled plumbus", |
|||
"owner": "Morty", |
|||
} |
|||
assert response.headers["X-Username"] == "Morty" |
Loading…
Reference in new issue